如何在Spring JDBC中回滚事务?

时间:2017-05-02 17:54:34

标签: java mysql spring jdbc transactions

我想在我的方法中做几个插入,并且想要在出现任何问题时回滚所有插入。 这是我的jdbc bean定义

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans" 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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

这是我的方法

@Transactional
public void save(Map<String, String> properties, Long moduleId,
                 String language, String propertyFileName, String filePath) {

    KeyHolder keyHolder = new GeneratedKeyHolder();

    final String propertyFileQuery = "INSERT INTO property_file " +
            "(moduleId, languageId, propertyFileName,hash,path) " +
            "VALUES (?,?,?,?,?)";

    jdbcTemplate.update(connection -> {
        PreparedStatement ps =
                connection.prepareStatement(propertyFileQuery, new String[]{"id"});
        ps.setLong(1, moduleId);
        ps.setString(2, language);
        ps.setString(3, propertyFileName);
        ps.setString(4, "hash goes here");
        ps.setString(5, filePath);

        return ps;
    }, keyHolder);

    int x = 0 / 0;

    final String propertiesQuery = "INSERT INTO property_value " +
            "(propertyFileId, propKey, propValue) " +
            "VALUES (?,?,?)";

    properties.forEach((key, value) -> jdbcTemplate.update(
            propertiesQuery, keyHolder.getKey(), key, value
    ));

}

我希望它在遇到运行时异常0/0

之后回滚第一个插入

但是数据会进入数据库,并且不会被回滚。

我做错了什么?

3 个答案:

答案 0 :(得分:2)

找出原因。交易管理还可以,我在交易中无法完成工作的原因是因为我的MySQL引擎。它被设置为MyISAM。 正如MySQL网站中所引用

  

MyISAM不支持交易,可能永远不会。

刚刚将表从MyISAM更改为InnoDb,并使其正常运行。

答案 1 :(得分:0)

您可以将@Transactional设置为您的类,而不是将其与单个方法绑定,因为当您将其绑定到类时,所有操作都将参与单个全局事务,如果任何事务失败,您的操作将被回滚< / p>

答案 2 :(得分:0)

private PlatformTransactionManager transactionManager;

public void setDataSource(DataSource dataSource) {

    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public void setTransactionManager(PlatformTransactionManager transactionmanager) {
    this.transactionManager = transactionmanager;
}

并且必须使用

方法
    TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());

并且在jdbcTemplate.update之后你必须提交交易

        transactionManager.commit(status);

你也可以使用@Transactional(propagation=Propagation.REQUIRED) `

相关问题