简体   繁体   中英

Commit Transaction in Java Class Design

I am looking for a best strategy in Java class design. I am using JSF 2.1 and basically I have a class which places an order and retrieves from a web service.

  • I want to commit the transaction or rollback if there is an error using this class.

What is the best approach in-terms of class design I can take to address this. Do I need to use nested class design? I have at-least 15 methods that has to work in coordination to make sure that even if one fails, the transaction is rolled back. Any guidance is appreciated.

JSF is a view technology. If you're doing things right, it shouldn't be involved in transactions at all.

JSF view should talk to a controller. The controller should have a reference to a service, which is the object that knows about units of work and transactions. The service orchestrates model objects, persistence objects, and other services to fulfill the use case.

The logic to acquire database connections and commit or rollback a transaction belongs in the service.

If you're using aspect-oriented programming, it's easy to do. (That's how Spring does it.)

view--->controller--->service--+--->persistence
                               |
                               +--->model

It's called a layered design. The dependencies ought to follow the arrows as I drew them. Model and persistence don't know about service, controller, or view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM