fuzebest wrote:
No semantic is:
Code:
DAO1
openSession1
closeSession1
DAO2
openSession2
DAO2.1
(use session2)
closeSession2
DAO3
openSession3
closeSession3
only business objects passed between DAO calls, not session.
so i have to call save/update at the ends of DAO methods
The better way is to use single session in all DAOs, but if i implicitly call openSession()/session.close() what the point to use CMT tx, all nice declarative features are gone?
I want bean methods be clean of this low level persistance stuff as much as possible.
Usually, in this case, you should do
Code:
DAO1
openSession1
tx = s.beginTransaction();
tx.commit();
closeSession1
DAO2
openSession2
tx = s.beginTransaction();
DAO2.1
(use session2)
tx.commit();
closeSession2
DAO3
openSession3
tx = s.beginTransaction();
tx.commit();
closeSession3
Your code is then portable outside a container and the flush is called, the tx management is delegated to the CMT.
You can also explicitly call flush in the closeSession, if you don't want to use hibernate tx api.
Still, what you do is a bad practice (IMO), you lost a significant amount of power given by hibernate by not sharing the same session.
In my original case, You can use AOP, session filter, EJB generation or whatever to handle the openSession, closeSession declaratively.
I consider that session mgt and tx mgt are 2 different things, so I'm OK with having declarative tx and non declarative session mgt.
PS: you should have a look at the EJB3 spec for declarative session magagement.