transactionTemplate不能与mybatis一起使用,但我不知道为什么

时间:2016-06-13 06:17:04

标签: java mysql spring mybatis spring-mybatis

我试图使用transactionTemplate来管理我的交易。代码可以列出如下:

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;
            }
        }
    });
}

当我使用jdbcTemplate处理Dao时,它可以工作。但是,如果我在使用myBatis时抛出异常,它就无法回滚。 XML代码列表如下:

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

我不知道为什么回滚在jdbcTemplate下工作但在mybatis下失效。表的引擎是InnoDB。 你能帮我一把吗?我已经困惑了将近两个星期了。 很抱歉我的英语不好带来了不便。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

...

@Value("${database.ip}")
private String databaseIp;

@Value("${database.user}")
private String user;

@Value("${database.password}")
private String password;

@Bean
public DataSource getMybatisDataSource() throws URISyntaxException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");

    dataSource.setUrl("jdbc:mysql://"+databaseIp+":3306/swords");
    dataSource.setUsername(user);
    dataSource.setPassword(password);

    return dataSource;
}

@Bean
public DataSourceTransactionManager transactionManager() throws URISyntaxException {
    return new DataSourceTransactionManager(getMybatisDataSource());
}

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(getMybatisDataSource());

    return sessionFactory.getObject();
}

这样你就可以简单地使用@Transacional。

更多信息: http://www.mybatis.org/spring/