简体   繁体   中英

How to rollback transaction in Spring JDBC?

I want to do several inserts in my method, and want to rollback all the inserts if anything goes wrong. Here's my jdbc bean definition

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

And here's my method

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

}

I want it to rollback the first insert, after it meets the runtime exception 0/0

But the data goes into the database, and doesn't get rolled back.

What am i doing wrong?

Found out the reason. The transaction management was ok, the reason I couldn't do my job in a transaction was because of my MySQL Engine. It was set to MyISAM . As cited in MySQL web site

MyISAM does not support transactions and probably never will.

Just changed the tables from MyISAM to InnoDb , and got it working.

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

private PlatformTransactionManager transactionManager;

public void setDataSource(DataSource dataSource) {

    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

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

and inside the method you have to use

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

and after jdbcTemplate.update you have to commit transaction

        transactionManager.commit(status);

you can also use @Transactional(propagation=Propagation.REQUIRED) `

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM