事务不回滚

transactional doesn't rollback

我用的是spring4.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>

网络配置:

    @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 并准确抛出该异常。

有什么想法吗?

@Transaction 注解有两大规则你必须牢记

  1. @Transaction 将在注释方法未声明的情况下工作 调用它的相同 class(默认 Spring 代理 AOP 为真)。
  2. 如果在 public 方法上注释,
  3. @Transaction 将起作用。它会一直 如果该方法是私有的、受保护的或包可见的,则被忽略。

那么你应该怎么做

在@Service注解class中Delcare一个publictransactionalTest()方法,然后在MainHandlerclass

中调用它