简体   繁体   中英

transactional doesn't rollback

I use spring 4.3.9.RELEASE

Some configs and code:

@Configuration
@EnableTransactionManagement
public class PersistenceContext {

    @Autowired
    private DbConfiguration dbConfiguration;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {

        final BoneCPDataSource dataSource = new BoneCPDataSource();

        dataSource.setDriverClass(dbConfiguration.getDriverClassName());
        dataSource.setJdbcUrl(dbConfiguration.getUrl());
        dataSource.setUsername(dbConfiguration.getUsername());
        dataSource.setPassword(dbConfiguration.getPassword());
        dataSource.setIdleConnectionTestPeriodInMinutes(dbConfiguration.getIdleConnectionTestPeriod());
        dataSource.setIdleMaxAgeInMinutes(dbConfiguration.getIdleMaxAgeInMinutes());
        dataSource.setMaxConnectionsPerPartition(dbConfiguration.getMaxConnectionsPerPartition());
        dataSource.setMinConnectionsPerPartition(dbConfiguration.getMinConnectionsPerPartition());
        dataSource.setPartitionCount(dbConfiguration.getPartitionCount());
        dataSource.setAcquireIncrement(dbConfiguration.getAcquireIncrement());
        dataSource.setStatementsCacheSize(dbConfiguration.getStatementsCacheSize());

        return dataSource;
    }

    @Bean(name = "txManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="ru.org.*"/>

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

    <mvc:annotation-driven />
</beans>

WebConfig:

    @Configuration
    @EnableScheduling
    @EnableWebMvc
    @ComponentScan({"ru.org.*"})
    @EnableTransactionManagement
    public class WebConfig extends WebMvcConfigurerAdapter {


        @Bean
        public MainHandler mainHandler() {
            return new MainHandler();
        }
    }

handler:

public class MainHandler extends AbstractUrlHandlerMapping {

    @Autowired
    private DataSource dataSource;

    protected Object getHandlerInternal(final HttpServletRequest request) throws Exception {
        transactionalTest();
    }

    @Transactional(transactionManager = "txManager")
    private void transactionalTest() {

        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.execute("INSERT INTO marks(score) VALUES (1);");
        jdbcTemplate.execute("IN3ERT INTO marks(score) VALUES (1);");
    }
}

It throws an exception but no rollback happened.

Also I tried to use rollbackFor -param and throws exactly that exception.

Any ideas?

There are two major rules of @Transaction annotation you must keep in mind

  1. @Transaction will work if the annotated method is not declared in the same class that invokes it (True for default Spring Proxy AOP).
  2. @Transaction will work if annotated on a public method. It will always be ignored if the method is private, protected or package-visible.

So what you should do

Delcare a public transactionalTest() method in a @Service annotated class and then call it in MainHandler class

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