Nous avons configuré Spring Data pour JPA. Une méthode de transaction de service n'est pas annulée en cas d'erreur (par exemple, une DB ConstraintViolationException).
Le plus proche que j'ai pu trouver était ceci (Transaction not rolling back) Spring-data, JTA, JPA, Wildfly10 mais nous n'avons pas de configuration XML, toute notre configuration est basée sur Java.
Essentiellement, une méthode de service ressemble à ceci : aucune erreur n'est détectée, tout est lancé.
@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);
}
La partie 2 échoue. Mais après l'erreur et le retour, je vois que l'événement (partie 1) est toujours alimenté dans la base de données.
Nous avons également essayé diverses combinaisons de @Transactional, mais rien n'a fonctionné :
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, readOnly = false)
@Transactional(readOnly = false)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ConstraintViolationException.class, readOnly = false)
Interfaces 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);
}
}
Y a-t-il eu des problèmes de retour en arrière des transactions avec Spring Data et JPA ?