gen*_* b. 5 spring jpa spring-data spring-data-jpa
我们为 JPA 配置了 Spring Data。服务事务方法不会因错误(例如 DB ConstraintViolationException)而回滚。
我能找到的最接近的是这个 (事务不回滚)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);
}
Run Code Online (Sandbox Code Playgroud)
第 2 部分失败。但是在错误并返回之后,我看到事件(第 1 部分)仍然填充在数据库中。
我们还尝试了@Transactional 的各种组合,但没有任何效果:
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, readOnly = false)
@Transactional(readOnly = false)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ConstraintViolationException.class, readOnly = false)
Run Code Online (Sandbox Code Playgroud)
Spring Data CRUD DAO 接口:
@Repository
public interface EventsDAO extends JpaRepository<EventsT, Integer> {
}
@Repository
public interface AnswersDAO extends JpaRepository<AnswersT, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
Jpa配置:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Data 和 JPA 是否存在任何事务回滚问题?
不管你信不信,我们已经修复了它。解决方案分为两部分:
1)@EnableTransactionManagement按照 ledniov 的描述添加到 JpaConfig,但仅此还不够;
2)同样在JpaConfig中entityManagerFactory(),将Service类包添加到如下setPackagesToScan。以前,域对象包存在,但服务对象包不存在。我们添加了"myapp.service"第二个包。
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource());
lef.setJpaPropertyMap(this.jpaProperties());
lef.setJpaVendorAdapter(this.jpaVendorAdapter());
lef.setPackagesToScan("myapp.domain", "myapp.service"); //NOTE: Service was missing
return lef;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3801 次 |
| 最近记录: |