java - 事务不回滚

标签 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>

网络配置:

    @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 将起作用 调用它的同一个类(默认 Spring Proxy AOP 为 True)。
  2. 如果在公共(public)方法上进​​行注释,@Transaction 将起作用。它将永远 如果该方法是私有(private)的、 protected 或包可见的,则被忽略。

那么你应该做什么

Delcare @Service 注解类中的公共(public) transactionalTest() 方法,然后在 MainHandler 类中调用它

关于java - 事务不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276095/

相关文章:

java - Freemarker:以编程方式读取一些模板参数

java - 在 Android 上使用 Webdav 暂停/恢复上传请求

java - 在单独的注释中测试配置注释

java - 读取 Spring JPA Repository 中未提交的数据

java - 如何修复 LazyInitializationException - Spring 应用程序中没有 session ?

java - 如何使用 Jackson 读取 JSON 文件中的多个对象?

java - Autowiring 类为空

java - 无法编写JSON:无限递归(StackOverflowError)

Spring Batch - 如何使用 jobLauncherTestUtils 防止数据库提交

java - 是否可以创建一个方面,对最初用@Transaction注释的方法提供@AfterReturnin操作?