I am working on a project that has Spring based Web Services and Spring Jdbc based persistence.
I have configured a DataSourceTransactionManager to manage the transactions and applied this to the service layer using a Pointcut.
Transaction propagation level is set REQUIRED.
The queries are issued through Spring provided JdbcTemplate.
The problem is that in case of multiple concurrent request to a service i get a MySQLTransactionRollbackException(“Deadlock found when trying to get lock; try restarting transaction”).
Apparently one of the transactions has obtained a lock which makes the second one fail.
My question is – How should configure Spring to delay the execution of the service until a lock is obtainable, instead of just giving up and throwing an exception?
I can’t even catch the exception and try to re-execute the query because I’ve applied transaction on service layer as an advice keeping my DAOs clean.
I’m hoping to get a declarative solution only (since I am an AOP fanatic & crusader against boilerplate code 🙂 ). But even programmatic solutions are welcome.
Thanks for your suggestions.
Update –
@ninjalj Yes it was actually a real deadlock. Turns out I was writing my test case incorrectly. Silly me 🙁
If I’m not mistaken, that indicates a real deadlock, so you should really retry the transaction. IIRC, a timeout waiting for a lock has “timeout” in the exception message.