lundi 13 juin 2016

transactionTemplate doesn't work with mybatis but I don't know why

I'm trying to using transactionTemplate to manage my transaction. The code may list as below:

private Boolean isCleanSuccess(){
    return transactionTemplate.execute(new TransactionCallback<Boolean>() {
        @Override
        public Boolean doInTransaction(TransactionStatus transactionStatus) {
            String rmcName = "sync";
            StringBuilder sbSync = new StringBuilder();
            sbSync.append("delete from `t_sync_data_plan` WHERE 1=1");
            StringBuilder sbRmc = new StringBuilder();
            sbRmc.append("delete from `t_rmc_data` where `name`="")
                    .append(rmcName)
                    .append(""");
            try{
                jdbcTemplate.execute(sbSync.toString());
                jdbcTemplate.execute(sbRmc.toString());
                /**
                  * throw the exception to make it rollback
                  */
                // throw new RuntimeException();
                return true;
            }catch (Exception ex){
                LOG.error(ex);
                transactionStatus.setRollbackOnly();
                return false;
            }
        }
    });
}

When I deal with Dao by using jdbcTemplate, it works. However, it just can not rollback if I throw the Exception when I use myBatis. The XML code lists as below:

<!-- rest transaction -->
<bean id="restTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="restDataSource"/>
</bean>

<bean id="restTransactionTemplate" name="restTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="restTransactionManager"/>
</bean>

 <!--domain transacation-->
<bean id="domainTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="domainDataSource"/>
</bean>

<bean id="domainTransactionTemplate" name="domainTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager">
        <ref bean="domainTransactionManager"/>
    </property>
</bean>

<!-- multi-datasource config -->
<bean id="multipleDataSource" class="com.angho.rest.db.MultipleDataSource" init-method="init">
    <property name="defaultTargetDataSource" ref="restDataSource"/>
    <property name="targetDataSources">
        <map>
            <entry key="restDataSource" value-ref="restDataSource"/>
            <entry key="domainDataSource" value-ref="domainDataSource"/>
        </map>
    </property>
    <property name="baseDaoDir" value="com.angho.rest"/>
    <property name="dataSourceNameMap">
        <map>
            <entry key="dao" value="restDataSource"/>
            <entry key="dao2" value="domainDataSource"/>
        </map>
    </property>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="multipleDataSource" />
    <property name="configLocation" value="classpath:/mybatis-config.xml"/>
</bean>

I don't know why the rollback just works under jdbcTemplate but loses efficacy under mybatis. The engine of table is InnoDB. Could you please give me a hand?It has been puzzled me for nearly two weeks. Sorry for the inconvenience that my poor English brought.

Aucun commentaire:

Enregistrer un commentaire