我们已经为JPA配置了Spring Data。服务事务方法不会因错误(例如DB ConstraintViolationException)而回滚。
我找到的最接近的是这个(Transaction not rolling back) Spring-data, JTA, JPA, Wildfly10,但我们没有任何XML配置,所有的配置都是基于Java的。
本质上,服务方法看起来像这样:没有捕获任何错误,抛出所有异常。
JpaConfig:
Spring Data和JPA是否存在任何事务回滚问题?
我找到的最接近的是这个(Transaction not rolling back) Spring-data, JTA, JPA, Wildfly10,但我们没有任何XML配置,所有的配置都是基于Java的。
本质上,服务方法看起来像这样:没有捕获任何错误,抛出所有异常。
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, readOnly = false)
public void insertEvent() throws Exception {
// Part 1
EventsT event = new EventsT();
// populate it..
eventsDAO.save(event);
// Part 2 - ERROR HAPPENS HERE (Constraint Violation Exception)
AnswersT answer = new AnswersT();
// populate it..
answersDAO.save(answer);
}
第二部分失败了。但是在错误和返回之后,我发现事件(第一部分)仍然存在于数据库中。
我们还尝试了各种@ Transactional的组合,但都没有成功:
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, readOnly = false)
@Transactional(readOnly = false)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ConstraintViolationException.class, readOnly = false)
Spring Data CRUD DAO 接口:
@Repository
public interface EventsDAO extends JpaRepository<EventsT, Integer> {
}
@Repository
public interface AnswersDAO extends JpaRepository<AnswersT, Integer> {
}
JpaConfig:
@Configuration
@EnableJpaRepositories(basePackages = "com.myapp.dao")
@PropertySource({ "file:${conf.dir}/myapp/db-connection.properties" })
public class JpaConfig {
@Value("${jdbc.datasource}")
private String dataSourceName;
@Bean
public Map<String, Object> jpaProperties() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("hibernate.dialect", PostgreSQL95Dialect.class.getName());
//props.put("hibernate.cache.provider_class", HashtableCacheProvider.class.getName());
return props;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
return hibernateJpaVendorAdapter;
}
@Bean
public PlatformTransactionManager transactionManager() throws NamingException {
return new JpaTransactionManager( entityManagerFactory().getObject() );
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource());
lef.setJpaPropertyMap(this.jpaProperties());
lef.setJpaVendorAdapter(this.jpaVendorAdapter());
lef.setPackagesToScan("com.myapp.domain", "com.myapp.dao");
return lef;
}
@Bean
public DataSource dataSource() throws NamingException {
return (DataSource) new JndiTemplate().lookup(dataSourceName);
}
}
Spring Data和JPA是否存在任何事务回滚问题?
org.springframework.transaction.annotation.Transactional
还是javax.transaction.Transactional
?而且insertEvent()
是在流程中第一个被注释为Transactional
的方法吗? - lzagkaretosorg.springframework.transaction.annotation.Transactional
。是的,insertEvent 方法如上所示被标记为 @Transactional。 - gene b.