一、編程式事務
二、聲明式事務
1、基于XML的事務
1.1 Spring配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<!-- 配置c3p0數據源,只是進行了最簡單的配置 --> < bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > < property name = "user" value = "root" ></ property > < property name = "password" value = "hss325730" ></ property > < property name = "jdbcUrl" value = "jdbc:mysql://127.0.0.1:3306/test" ></ property > < property name = "driverClass" value = "com.mysql.jdbc.Driver" ></ property > </ bean > <!-- 配置Spring的 JdbcTemplate --> < bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > < property name = "dataSource" ref = "dataSource" ></ property > </ bean > <!-- 配置Bean --> < bean id = "bookDao" class = "com.zhoujian.spring.transcation.xml.BookDaoImpl" > < property name = "tempate" ref = "jdbcTemplate" ></ property > </ bean > < bean id = "bookService" class = "com.zhoujian.spring.transcation.xml.service.impl.BookServiceImpl" > < property name = "dao" ref = "bookDao" ></ property > </ bean > < bean id = "batchBuy" class = "com.zhoujian.spring.transcation.xml.service.impl.BatchBuyImpl" > < property name = "service" ref = "bookService" ></ property > </ bean > <!-- 配置事務管理器 --> < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" ></ property > </ bean > <!-- 配置事務管理器屬性 并與事務管理器關聯--> < tx:advice id = "myAdvice" transaction-manager = "transactionManager" > < tx:attributes > <!-- 在這里一般都是使用通配符進行配置, 或者直接配置指定的方法 --> < tx:method name = "buy" propagation = "REQUIRES_NEW" /> < tx:method name = "get*" propagation = "REQUIRED" /> < tx:method name = "*" /> </ tx:attributes > </ tx:advice > <!-- 配置開啟事務的切入點,使用AOP進行切入點的配置,并與事務管理器屬性關聯起來 --> < aop:config > < aop:pointcut expression = "execution(* com.zhoujian.spring.transcation.xml.service.*.*(..))" id = "myPointcut" /> < aop:advisor advice-ref = "myAdvice" pointcut-ref = "myPointcut" /> </ aop:config > |
1.2 業務類
和下面注解方式使用的類一樣,不過是去掉了注解,我將所有的Service層放在一個包下,這樣便于AOP 切入點表達式的書寫
2、基于注解的事務
2.1、Sprin配置文件
1
2
3
4
5
6
7
8
9
10
|
<!-- 配置自動掃描 --> < context:component-scan base-package = "com.zhoujian.spring" ></ context:component-scan > <!-- 配置c3p0數據源,只是進行了最簡單的配置 --> < bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > < property name = "user" value = "root" ></ property > < property name = "password" value = "hss325730" ></ property > < property name = "jdbcUrl" value = "jdbc:mysql://127.0.0.1:3306/test" ></ property > < property name = "driverClass" value = "com.mysql.jdbc.Driver" ></ property > </ bean > |
1
2
3
4
|
<!-- 配置Spring的 JdbcTemplate --> < bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > < property name = "dataSource" ref = "dataSource" ></ property > </ bean > |
1
2
3
4
|
<!-- 配置事務管理器 --> < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" ></ property > </ bean > |
1
2
|
<!-- 啟動事務注解 --> < tx:annotation-driven transaction-manager = "transactionManager" /> |
2.2、業務類(Spring 在 Service 層上開啟事務)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.zhoujian.spring.transcation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service("bookService") public class BookServiceImpl implements BookService { @Autowired private BookDao dao; //這里是在指定方法上面開啟事務 @Transactional @Override public void buy(String userId, String bookId) { Integer price = dao.getBookPrice(bookId); dao.updateBookCount(bookId); dao.updateUserAccount(userId, price); } } |
2.3關于事務的屬性(事務隔離級別,事務傳播行為,事務異常控制,事務強制回滾時間控制,事務是否只讀)
一般情況下,不需要進行手動改變事務屬性,使用默認的就行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.zhoujian.spring.transcation; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service("batchBuy") public class BatchBuyImpl implements BatchBuy { @Autowired private BookService service; /** * 1、關于事務的傳播行為(當前事務方法調用另外的事務方法時,面對的事務怎么使用的問題), * 常用的有兩種(Propagation.REQUIRED, Propagation.REQUIRES_NEW) * Spring 默認使用 Propagation.REQUIRED,表示使用當前事務方法持有的事務 * * 例如: 事務方法A 調用事務方法B, * 如果事務的傳播行為使用Propagation.REQUIRED時,表示支持已經存在的事務, * 如果在調用A方法之前不存在任何事務,那么此時會創建一個新的事務,在這里則都使用方法A所持有的事務, * 對于該配置,如果B過程中發生異常需要回滾,那么A中所進行的所有數據庫操作也將同時被回滾, * 因為這兩個方法使用了同一個事務; * * * 如果事務的傳播行為使用Propagation.REQUIRES_NEW時, 表示將A方法所持有的事務掛起, * 使用B方法自己的事務,當B方法事務完成之后,A事務才被喚醒 */ @Transactional(propagation=Propagation.REQUIRED) @Override public void buy(String userId, List< String > bookIds) { for(String bookId : bookIds){ service.buy(userId, bookId); } } } |
3、測試類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.zhoujian.spring.transcation; import java.util.Arrays; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TranscationTest { private static ApplicationContext ac = null; private static BookDao dao = null; private static BookService service; private static BatchBuy buy; static{ ac = new ClassPathXmlApplicationContext("bean-transcation.xml"); dao = ac.getBean("bookDao", BookDao.class); service = ac.getBean("bookService", BookService.class); buy = ac.getBean("batchBuy", BatchBuy.class); } @Test public void test1(){ buy.buy("001", Arrays.asList("1","2")); } @Test public void test() { service.buy("001", "1"); } } |
以上這篇詳談Spring框架之事務管理就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/called-j/archive/2017/09/20/7565583.html