Rollback transactions in JAX-RS
I have a simple resource class that implements some POST method. How to rollback transaction if there开发者_如何学编程 was exceptions in my methods, and commit - if all is ok?
Is there a way to write this code once - not in every resource class that I have?
If you are using Spring, @Transactional will handle your scenario.
http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html
Using a dependency-injection will greatly simplify this. Using @Transactional
or similar annotations around the methods where you want to commit/rollback transactions.
If you have to do this manually, you have basically two options:
- do it manually for every operation
- use the proxy pattern/decorator pattern and proxy/decorate all your classes that require transactions. Then in the proxy/decorator start the transaction, delegate to the target, and commit it after it returns. (this is how DI frameworks do it)
精彩评论