一、编程式事务
二、声明式事务
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