3
votes

I have a code block with this method:

@Transactional(noRollbackFor=Exception.class)
public synchronized Object saveData(){
    //...
    dao.insert(entity);
    //...
}

My dao class is marked ad Transactional

@Transactional
public class Dao {
   //...
   public <T> void insert(T obj) throws Exception {
    getSession().saveOrUpdate(obj);
   }
}

I need to prevent any rollback inside this block. However I got this:

2014-02-25 20:47:44 [WARN]SqlExceptionHelper:143    SQL Error: 1205, SQLState: 41000
2014-02-25 20:47:44 [ERROR]SqlExceptionHelper:144   Lock wait timeout exceeded; try restarting transaction
2014-02-25 20:47:44 [ERROR]BigliettiService:?   Transazione in errore
org.hibernate.exception.GenericJDBCException: Lock wait timeout exceeded; try restarting transaction
...
2014-02-25 20:47:44 [ERROR]JsonHandlerExceptionResolver:?   Transaction rolled back because it has been marked as rollback-only

Why at the end I found a Transaction rolled back?

2

2 Answers

4
votes

I had a similar issue just last week. In my case it was another @Transactional annotation from a different method that caused the transaction to be marked as rollback-only.

Did you check for that yet?

Edit: To clarify it a bit more, you didn't post your code from inside your saveData() method. What caused this error for me was calling another method with @Transactional (lacking the noRollback attribute) from inside my method, in your case saveData().

1
votes

I don't think you can do that. Your transaction is being propagated by default to the inner method and if this method rollbacks your transaction, the outer method can only be notified by a UnexpectedRollbackException that the rollback already happened.. and acknowledge it.

You can change the propagation config to start a new inner transaction but this won't help as it will still be commited independently before reaching the outer transaction.

If you can't set a NoRollback attribute at DAO level, I guess the best solution would be to remove the Transactional attributes from your DAOs and have a layer of facade services calling these DAOs and defining their own rollback strategies.