springboot xml聲明式事務管理方案

在開發過程當中springboot提供的常見的事務解決方案是使用註解方式實現。java

使用註解spring

在啓動類上添加註解express

@EnableTransactionManagementjson

在須要事務控制的方法添加@Transactional註解springboot

這種方式問題是,咱們須要在方法上添加註解,這樣處理起來特別麻煩。app

咱們能夠使用XML方式配置,配置好後,方法不須要加註解,這樣咱們使用起來就不須要再管註解的事情。測試

1.添加transaction.xml ui

<?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>

 

這樣咱們只需只要在編寫事務代碼的時候遵循上面的規則,編寫方法名稱,就能夠對事務進行攔截。spa

2.在啓動類上引入此配置文件。debug

@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

 

也能夠單獨增長springboot的配置類,來使xml配置在springboot項目的生效

相關文章
相關標籤/搜索