我使用 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 注解有两个主要规则你必须牢记
- 如果带注释的方法未在中声明,@Transaction 将起作用 调用它的同一个类(默认 Spring Proxy AOP 为 True)。
- 如果在公共(public)方法上进行注释,@Transaction 将起作用。它将永远 如果该方法是私有(private)的、 protected 或包可见的,则被忽略。
那么你应该做什么
Delcare @Service 注解类中的公共(public) transactionalTest()
方法,然后在 MainHandler
类中调用它
关于java - 事务不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276095/