简体   繁体   English

Spring事务不会在@Transactional方法中的RuntimeException回滚

[英]Spring Transaction doesn't Rollback at RuntimeException in @Transactional method

I have this DB configuration: 我有此数据库配置:

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "com.mycompany")
public class DBConfiguration {

    @Bean(destroyMethod = "close")
    public javax.sql.DataSource dataSource() {
        DataSource ds = new DataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost/v2");
        ds.setUsername("java");
        ds.setPassword("mypass");
        ds.setInitialSize(5);
        ds.setMaxActive(10);
        ds.setMaxIdle(5);
        ds.setMinIdle(2);
        ds.setRemoveAbandoned(true);
        ds.setLogAbandoned(true);
        return ds;
    }

    @Bean
    public DataSourceTransactionManager txManager()
    {
        DataSourceTransactionManager tx= new DataSourceTransactionManager(dataSource());
        return tx;
    }

}

QUESTION UPDATED 问题已更新

I have some trouble to understand how @Transaction annotation works, please consider this scenario: 我在理解@Transaction注释的工作方式时遇到了一些麻烦,请考虑以下情形:

@Service
public class FirstService {
    @Transactional  //<--- this annotation seems to be mandatory for my rollback but I don't want it.
    public void test() throws Exception{
        secondService.insert();
    }
}

@Service
public class SecondService {
    @Transactional //<-- I would like to have only this method in transaction
    protected void insert() throws Exception{
        dao.insertEntity(new Entity()); //<<--- this is an SQL insert command
        throw new RuntimeException("Rollback test");
    }
}

Test code: 测试代码:

@RequestMapping("/test") @ResponseBody
    public void test() throws Exception{
        firstService.test();
    }

Dao: 道:

public void insertEntity(Entity e) {
        getJdbcTemplate().update(SQL_INSERT,e.getCode(),e.getName());       
    }

This test WORKS, thrown exception could rollback the transaction. 此测试有效,抛出异常可能回滚事务。

Why if I omit the @Transaction annotation on the firstService there is not rollback? 如果我在firstService上省略了@Transaction注释,为什么没有回滚?

Seems that from @Controller to @Service the txmanager looks for @Transaction annotation, from @Service to (another) @Service or @Component it doesn't look for it. 似乎从@Controller到@ Service,txmanager寻找@Transaction注解,从@Service到(另一个)@Service或@Component,它没有寻找它。

This works for me: 这对我有用:

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

Please update the spring version and maybe some logs or debug logs and see if there is an issue with Transaction 请更新春季版本,并可能更新一些日志或调试日志,然后查看事务是否存在问题

There were two error. 有两个错误。

First error was that I cannot nest two method (first not transactional, second transactional) in the same object then I have to separate it in two objects (as you can see in updated question). 第一个错误是我无法在同一个对象中嵌套两个方法(第一个不是事务性的,第二个事务性的),然后我不得不将其分离为两个对象(如您在更新的问题中所看到的)。

Second error was that @Transaction annotation shuold be applied to public methods not to private or protected. 第二个错误是@Transaction注释shuold应该应用于公共方法而不是私有方法或受保护方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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