事务不会回滚

时间:2017-07-24 08:49:58

标签: java spring spring-transactions transactional

我使用spring 4.3.9.RELEASE

一些配置和代码:

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

处理程序:

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

它抛出异常但没有发生回滚。

此外,我尝试使用 rollbackFor -param并完全抛出该异常。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您必须牢记@Transaction注释的两个主要规则

  1. 如果未声明带注释的方法,@ Transns将起作用 调用它的同一个类(默认的Spring Proxy AOP为True)。
  2. @Transaction如果在公共方法上注释,则会起作用。它将永远 如果方法是私有的,受保护的或包可见的,则被忽略。
  3. 那你应该做什么

    在@Service注释类中隐藏公共transactionalTest()方法,然后在MainHandler

    中调用它