How to achieve transaction management in Spring
My methods are as below
@Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
@Override
public InwardDTO saveEntity(InwardDTO entity) throws Exception {
try {
costCalculation(entity);
InwardDTO dto = super.saveEntity(entity);
addStock(dto.getDetails(), dto.getId());
return dto;
} catch (Exception ex) {
throw ex;
}
}
private void addStock(Set<InwardDetailsDTO> argDetailsDTOSet, Long argInwardId) throws RuntimeException {
String SUBMODULE = getModuleNameForLog() + " [addStock()] ";
if (1 == 1) {
throw new RuntimeException("Test Case");
}
}
Logs are like,
2020-02-29 15:01:14.210 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
Hibernate: insert into tbl_inward_chemical (date, invoice_number, is_deleted, party_id, po_id, pre_inward_id_id, remark, slip, total_amount, total_weight) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into tbl_inward_details_chemical (inward_id, is_deleted, is_pass, party_moisture_id, party_ph_id, party_price, party_purity_id, party_solubility_id, party_weight, product_name_id, received_moisture_id, received_ph_id, received_price, received_purity_id, received_solubility_id, received_weight, total_price) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into tbl_inward_details_chemical (inward_id, is_deleted, is_pass, party_moisture_id, party_ph_id, party_price, party_purity_id, party_solubility_id, party_weight, product_name_id, received_moisture_id, received_ph_id, received_price, received_purity_id, received_solubility_id, received_weight, total_price) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor : Completing transaction for [com.alignedorg.chemical.inward.service.InwardService.saveEntity] after exception: java.lang.RuntimeException: Test Case
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.RuleBasedTransactionAttribute : Applying rules to determine whether transaction should rollback on java.lang.RuntimeException: Test Case
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.RuleBasedTransactionAttribute : Winning rollback rule is: RollbackRuleAttribute with pattern [java.lang.RuntimeException]
2020-02-29 15:01:14.222 ERROR 14504 --- [nio-8080-exec-2] c.a.core.utillity.log.ApplicationLogger : [ Inward Controller ] [SAVE] Test Case
java.lang.RuntimeException: Test Case
In this transactional is always commited before rollback of addStock Method ... In logs it is showing like transaction is rollback but entry is saved in DB...
In case the table storage engine is MyISAM: This engine does not support transactions. The correct one should be InnoDB.
If you are using automatic creation of tables from Hibernate: I would recommend writing your own migrations and using tool like Flyway do manage those. It's a bit more work but you will have total control over how your tables look like.
Spring transaction bean required list:
1.JdbcTemplate
@Bean(name = "jdbcTemplate")
public JdbcTemplate creatJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
2.DataSource
@Bean(name = "dataSource")
public DataSource creatDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
3.TransactionManager
@Bean(name = "transactionManager")
public PlatformTransactionManager creatTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
4.@EnableTransactionManagement above one @Configuration Class to activate TxManager
5.Use JdbcTemplate's instance(@Autowired) in DaoImplements to execute sql with query() || update() method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With