I know it's a quite big topic, just want to know if there's any best practise I can follow.
Let me introduce the project architecture first.
The web application is clearly divided into three layers: presentation, business, and persistence.
In presentation layer, I use struts. In struts action classes, I call facades provided by business layer.
In business layer, I have a few facades represent a few modules. Calls between modules must call its facade. Within a module, there maybe a lot of classes implements business logic. All persistence related call would be forward to DAO interface, which is provided by persistence layer.
In persistence layer, there are a few DAO implementations represent their modules. In each DAO implementation, data accesses are encapsulated. No matter what persistence approach will be used, it's just a kind of implementation.
Say, if I use container-managed transaction. I�m not sure where the transaction starts and ends. Does it happen in facades? Or happen in individual classes in business layer? I know it cannot be in struts classes.
If we handle transaction by ourselves, where should we start and commit it? In struts action? Fa�ade? Or where?
I need your help, guys. I need to know what is the best practise and why?
The facade mentioned in the scenario, Is it a session facade ? If the answer is yes..
Regarding to the question, where the transaction starts in container manged transaction. The transaction will be started at the facade level.. If the required transaction attributes are being specified for the Session bean. This transaction can be propogated to the business classes and DAO classes..
It is advisable not to handle transactions by the developer and that is the responsibility of the container in this scenario..
A transaction can start either inside or outside the EJB container. If possible, you want the former (in the interest of keeping transactions small). In this case, the facade method(s) which must run in a transaction must have the correct tx attribute declared in the DD, normally Required. (This is for CMT beans, better in most cases than BMT beans.) If you need to rollback, you must call setRollbackOnly() or throw a system exception such as EJBException from your bean, POJO or DAO. Oh, and if you call setRollbackOnly() from a POJO or DAO, you will need to give them access to the bean's EJBContext.
You need a UserTransaction object for transactions started outside the EJB container, ie from your struts action classes. Check your container provider's documentation on how to get a UserTransaction, once you have it you must invoke its begin() method. You then call the bean method, which will run in the same tx if its tx attribute is Required or Mandatory.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Joined: Feb 27, 2002
thanks for your reply. I know what you guys mean. It's better let the container manage the transaction. I agree with that.
but, how about if I don't use ejb container? I don't have any ejb. and I use Jdo or Ojb such object-database mapping persistence. what should I do?
I still have web action -> facade -> business class -> DAO. I know I must have a transaction object to explicitly start and commit transaction. I just don't know when and where.