Reputation: 1302
I'm looking to use @Transactional in spring boot, but after several attempts i cannot get the transaction working despite having an exception inside to rollback, so i'm missing somthing ?
AppConfig.java
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.geopro" })
@EnableJpaRepositories(basePackages = { "com.geopro.repositories" })
@EntityScan(basePackages = { "com.geopro.entities" })
@EnableTransactionManagement
public class AppConfig {
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
}
Metier.java
@Service("metier")
@Aspect
public class Metier {
@Transactional(readOnly = false , rollbackFor = Exception.class)
public void saveUpload(UploadData post) throws Exception {
try {
post.ADDEDDATA.savesync();
post.UPDATEDDATA.savesync();
} catch (Exception e) {
throw e ;
}
}
Upvotes: 1
Views: 9847
Reputation: 1302
The cause for my problem Transactions not working is mixing AspectJ advices and Transactions in the same class, so this option @EnableTransactionManagement(proxyTargetClass=true)
Forces the Transactions to work but, disables AspectJ advices even if you place them in a dedicated class.
So the best solution for this problem is put AspectJ advices in a dedicated Class, so Transactions and AspectJ advices will work like expected.
Upvotes: 2