开发者

Rollback transactions in JAX-RS

开发者 https://www.devze.com 2023-03-05 00:47 出处:网络
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?

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号