spring-jooq交易无效

时间:2017-04-24 12:20:59

标签: spring spring-transactions jooq

在将spring与jooq集成时,事务声明回滚功能无法正常工作。我在application-context.xml中的设置在

之下
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="menu" viewBox="0 0 32 32"><path d="M4,10h24c1.104,0,2-0.896,2-2s-0.896-2-2-2H4C2.896,6,2,6.896,2,8S2.896,10,4,10z M28,14H4c-1.104,0-2,0.896-2,2  s0.896,2,2,2h24c1.104,0,2-0.896,2-2S29.104,14,28,14z M28,22H4c-1.104,0-2,0.896-2,2s0.896,2,2,2h24c1.104,0,2-0.896,2-2  S29.104,22,28,22z"/>
</symbol>
</svg>

然后我有一个标记有事务的方法,如下面的

<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="tx-jooqtest"/>
     <property name="dataSource" value="#{jooqtest.getDataSource()}"/>
</bean>

<bean id="jooqtest" class="com.vassarlabs.datasource.service.impl.VLDataSource">
    <property name="dataStoreName" value="jooqtest" />
</bean>

并且在insert2()方法中我抛出DataAccessExcpetion,但是事务没有回滚。是否有任何其他属性我必须在application-context xml中设置才能使其工作。

1 个答案:

答案 0 :(得分:1)

似乎在application-context.xml中缺少某些属性。为了让jooq认识到spring事务是与jooq相同的事务的一部分,我们必须创建一个连接提供者bean,它通过TransactionAwareDataSourceProxy引用dataSource。更新的代码和配置如下

 @Transactional(value = "tx-jooqtest" , rollbackFor = DataAccessException.class)                                           
     public void testTransaction() {
     DSLContext dslContext = null;
     dslContext = DSL.using(connProvider, SQLDialect.MYSQL);
     String sql = "insert into food (kind) values ( ? )";            
      dslContext.execute(sql ,new Object[]{"Its a whole next level kind "} 
    );                     
    insert2();}

平台应用程序的context.xml

    <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <qualifier value="tx-jooqtest"/>
         <property name="dataSource" value="#{jooqtest.getDataSource()}"/>
    </bean>

    <bean id="jooqtest" class="com.vassarlabs.datasource.service.impl.VLDataSource">
        <property name="dataStoreName" value="jooqtest" />
    </bean>
<bean id="transactionAwareDataSource"
        class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <constructor-arg value="#{jooqtest.getDataSource()}" />
    </bean>

    <bean class="org.jooq.impl.DataSourceConnectionProvider" name="connectionProvider">
        <constructor-arg ref="transactionAwareDataSource" />
    </bean>