接口mysql
1 package spring.transaction; 2
3 public interface BookDao { 4
5 //根據書名獲取書的單價
6 double findBookPriceByName(String bookName); 7
8 //更新庫存數
9 void updateBookStock(String bookName); 10
11 //更新買家帳戶金額
12 void updateBuyerAmount(String buyerName,double price); 13
14 //更新賣家帳戶金額
15 void updateSellerAmount(String sellerName,double price); 16
17 }
jdbcTemplate操做mysql
1 package spring.transaction; 2
3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.jdbc.core.JdbcTemplate; 5 import org.springframework.stereotype.Repository; 6
7
8 @Repository(value = "BookDao") 9 public class BookDaoImpl implements BookDao { 10
11 @Autowired 12 private JdbcTemplate jdbcTemplate; 13
14
15 @Override 16 public double findBookPriceByName(String bookName) { 17 String sql = "SELECT `price` FROM `book` WHERE `name`=?"; 18 return jdbcTemplate.queryForObject(sql,double.class,bookName); 19 } 20
21 @Override 22 public void updateBookStock(String bookName) { 23 String check = "SELECT `stock` FROM `book` WHERE `name`=?"; 24 int result = jdbcTemplate.queryForObject(check,int.class,bookName); 25
26 if (result>0){ 27 String sql = "UPDATE `book` SET `stock`= `stock`-1 WHERE `name`=?"; 28 jdbcTemplate.update(sql,bookName); 29 }else { 30 throw new RuntimeException("庫存不足!"); 31 } 32
33
34 } 35
36 @Override 37 public void updateBuyerAmount(String buyerName, double price) { 38 String check = "SELECT `amount` FROM `buyer` WHERE `name`=?"; 39 double result = jdbcTemplate.queryForObject(check,double.class,buyerName); 40 if(result>price){ 41 String sql = "UPDATE `buyer` SET `amount`= `amount`-? WHERE `name`=?"; 42 jdbcTemplate.update(sql,price,buyerName); 43 }else { 44 throw new RuntimeException("餘額不足!"); 45 } 46
47 } 48
49 @Override 50 public void updateSellerAmount(String sellerName, double price) { 51 String sql = "UPDATE `seller` SET `amount`= `amount`+? WHERE `name`=?"; 52 jdbcTemplate.update(sql,price,sellerName); 53
54 } 55 }
註解事務spring
1 package spring.transaction; 2
3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5 import org.springframework.stereotype.Service; 6 import org.springframework.transaction.annotation.Isolation; 7 import org.springframework.transaction.annotation.Propagation; 8 import org.springframework.transaction.annotation.Transactional; 9
10 @Service(value = "BookService") 11 public class BookService { 12
13 @Autowired @Qualifier(value = "BookDao") 14 private BookDao dao; 15
16 /** 17 * 買書的交易過程方法 18 * @param bookName 書名 19 * @param buyerName 買家名 20 * @param sellerName 賣家名 21 * 22 */
23
24 /*
25 * 事務 26 * - propagation 指定事務的傳播行爲 27 * - 定義當前事務方法被另一個事務方法調用是時,如何使用事務 28 * - 默認是REQUIRED,也就是使用調用方法的事務 29 * -REQUIRES_NEW 使用新事務 30 * 31 * - isolation 指定事務的隔離級別 32 * - 經常使用級別 READ_COMMITTED 33 * 34 * - rollbackFor/noRollbackFor 定義要(不)執行回滾的異常 35 * 36 * - readOnly 指定事務是否只讀 37 * 38 * - timeout 指定強制回滾以前事務能夠佔用的時間,單位是秒 39 */
40 @Transactional(propagation = Propagation.REQUIRES_NEW, 41 isolation = Isolation.READ_COMMITTED, 42 rollbackFor = Exception.class, 43 readOnly = false, 44 timeout = 3) 45 public void action(String buyerName,String sellerName,String bookName) {//
46
47 // 超時致使的事務強制回滾 48 // try { 49 // Thread.sleep(5000); 50 // } catch (InterruptedException e) { 51 // e.printStackTrace(); 52 // } 53 //得到單價
54 double price = dao.findBookPriceByName(bookName); 55
56 //更新庫存
57 dao.updateBookStock(bookName); 58
59 //買家付款
60 dao.updateBuyerAmount(buyerName,price); 61
62 //賣家收款
63 dao.updateSellerAmount(sellerName,price); 64 } 65
66
67 }
接口sql
1 package spring.transaction; 2
6 public interface MulBookDao { 7 void mulAction( String buyerName, String sellerName,String... bookNameList); 8 }
實現類ide
1 package spring.transaction; 2
3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 import org.springframework.transaction.annotation.Transactional; 6
7
8
9 @Service("service") 10 public class MulBookDaoImpl implements MulBookDao { 11
12 @Autowired 13 private BookService bookService; 14
15
16 //購買多本數
17 @Transactional 18 @Override 19 public void mulAction(String buyerName, String sellerName,String... bookNameList) { 20
21 for (String bookName : bookNameList){ 22 bookService.action(buyerName,sellerName,bookName); 23 } 24 } 25 }
測試類測試
1 package spring.transaction; 2
3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7
8 public class TransactionTest { 9
10 private ApplicationContext ac; 11 private MulBookDaoImpl service; 12
13 @Before 14 public void init(){ 15 ac = new ClassPathXmlApplicationContext("classpath:transaction.xml"); 16 service = ac.getBean("service",MulBookDaoImpl.class); 17 } 18
19
20 @Test 21 public void test(){ 22
23 service.mulAction("Tom","LK","JAVA","C"); 24 } 25 }
配置文件spa
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p="http://www.springframework.org/schema/p"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xmlns:util="http://www.springframework.org/schema/util"
8 xmlns:tx="http://www.springframework.org/schema/tx"
9 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 12 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
14
15
16 <context:component-scan base-package="spring.transaction"/>
17
18 <!--自動爲匹配的類生成代理對象-->
19 <aop:aspectj-autoproxy proxy-target-class="true" />
20
21
22 <!--導入資源-->
23 <util:properties location="classpath:db.properties" local-override="true" id="db"/>
24 <!--配置C3P0數據源-->
25 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
26 <property name="driverClass" value="#{db.driverClass}"/>
27 <property name="jdbcUrl" value="#{db.jdbcUrl}"/>
28 <property name="user" value="#{db.user}"/>
29 <property name="password" value="#{db.password}"/>
30
31 <property name="initialPoolSize" value="#{db.initialPoolSize}"/>
32 <property name="maxPoolSize" value="#{db.maxPoolSize}"/>
33 </bean>
34
35 <!--配置jdbcTemplate的Bean-->
36 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
37 <property name="dataSource" ref="dataSource"/>
38 </bean>
39
40 <!--具名jdbc模版Bean-->
41 <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
42 <constructor-arg name="dataSource" value="#{dataSource}"/>
43 </bean>
44
45 <!--配置事務管理器-->
46 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
47 <property name="dataSource" value="#{dataSource}"/>
48 </bean>
49
50 <!--啓用事務註解-->
51 <tx:annotation-driven transaction-manager="transactionManager" />
52
53
54
55 </beans>