springboot xml聲明式事務管理方案


在開發過程中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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM