Transaction atomicity with Spring transactional annotation
Hi,
I am working the transaction atomicity using Spring . Below is what I am doing
public void runBatchJob() {
while (true) {
// generate work
doWork(unitOfWork);
}
}
@transactional(..,rollbackFor="RuntimeException.cl ass")
private void doWork(UnitOfWork work) {
dao.m1(data1);
dao.m2(data2);
dao.m3(data3);
dao.m4(data4);
}
where the DAO functions are defined:
@Transactional
public void m1(Data data) {
...
}
@Transactional
public void m2(Data data) {
...
}
@Transactional
public void m3(Data data) {
...
}
@Transactional
public void m4(Data data) {
...
}
In applicationContext.xml:
<tx:annotation-driven transaction-manager="jtaTransactionManager"/>
Here if the dao.m1,m2&m3 are successful and m4 is failed then we need to rollback the entire transaction we shouldnot commit the transactions for m1,m2&m3 as well. We are using JTATransactionManager which by default supports 2 phase commit. But still using the above the transaction rollback is not happening eventhough a RuntimeException has been thrown?
One more thing is how we can catch the RuntimeException and print the log for the above? whether we need to include this try catch block like
@transactional(..,rollbackFor="RuntimeException.cl ass")
private void doWork(UnitOfWork work) {
try{
dao.m1(data1);
dao.m2(data2);
dao.m3(data3);
dao.m4(data4);
}catch(RuntimeException e){
//log the details
}
Please clarify the above 2 problems.
Thanks.