在开发过程中springboot提供的常见的事务解决方案是使用注解方式实现。
使用注解
在启动类上添加注解
@EnableTransactionManagement
在需要事务控制的方法添加@Transactional注解
这种方式问题是,我们需要在方法上添加注解,这样处理起来特别麻烦。
我们可以使用XML方式配置,配置好后,方法不需要加注解,这样我们使用起来就不需要再管注解的事情。
1.添加transaction.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" propagation="SUPPORTS" read-only="true"></tx:method> <tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method> <tx:method name="select*" propagation="SUPPORTS" read-only="true"></tx:method> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"></tx:method> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution (* com.neo.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" order="0"/> </aop:config> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
这样我们只需只要在编写事务代码的时候遵循上面的规则,编写方法名称,就可以对事务进行拦截。
2.在启动类上引入此配置文件。
@SpringBootApplication @ImportResource("classpath:transaction.xml") @MapperScan({"com.neo.dao"}) public class DemoApplication {
这样springboot 就可以支持事务管理了。
3.测试事务是否生效
public void create(SaleOrder order){ orderDao.create(order); throw new RuntimeException("出错了") ; }
编写代码如下,在添加后抛出异常,发现数据并没有真正的插入。
注意事项:
使用事务需要引入:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency>
打印事务日志:
logging:
level:
com.neo.dao: debug
org.springframework.jdbc: debug
日志执行情况:
2018-10-16 23:17:17.702 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Creating new transaction with name [com.neo.service.SaleOrderService.create]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-Exception 2018-10-16 23:17:17.708 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] for JDBC transaction 2018-10-16 23:17:17.713 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] to manual commit 2018-10-16 23:17:17.754 DEBUG 9640 --- [nio-8000-exec-1] com.neo.dao.SaleOrderDao.create : ==> Preparing: INSERT INTO SALE_ORDER (ID_,NAME_,TOTAL_,CREATOR_,CREATE_TIME_) VALUES (?, ?, ?, ?, ?) 2018-10-16 23:17:17.782 DEBUG 9640 --- [nio-8000-exec-1] com.neo.dao.SaleOrderDao.create : ==> Parameters: 1539703037695(String), zyg(String), 33.0(Double), AA(String), null 2018-10-16 23:17:17.784 DEBUG 9640 --- [nio-8000-exec-1] com.neo.dao.SaleOrderDao.create : <== Updates: 1 2018-10-16 23:17:17.785 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Initiating transaction rollback 2018-10-16 23:17:17.785 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] 2018-10-16 23:17:17.786 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] after transaction 2018-10-16 23:17:17.787 DEBUG 9640 --- [nio-8000-exec-1] o.s.jdbc.datasource.DataSourceUtils : Returning JDBC Connection to DataSource 2018-10-16 23:17:17.797 ERROR 9640 --- [nio-8000-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/demo] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: 出错了] with root cause