I have a very simple web application that uses
Tomcat 5.0,
Hibernate 3.2,
hsqldb database, and I have the following properties in the
hibernate.cfg.xml:
<property name="current_session_context_class">org.hibernate.context.JTASessionContext</property>
<property name="hibernate.transaction.manager_lookup_class"> org.hibernate.transaction.JOTMTransactionManagerLookup
</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
</property>
I do not use EJBs (since I use Tomcat) so my hierarchy is:
JSF--> managed beans --> UserDAO.
I've managed to save in the database and retrieve data in the database. What I am doing is opening a
UserTransaction and then
getCurrentSession in the UserDAO. For example :
Code:
Context ctx = new InitialContext();
tx = (UserTransaction)ctx.lookup("java:comp/env/UserTransaction");
tx.begin();
session = HibernateUtil.getSessionFactory().getCurrentSession();
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("username", username));
criteria.add(Restrictions.eq("password", password));
List results = criteria.list();
session.close();
tx.commit();
What I want to ask is:
Should I also open a transaction, and a session in the method of the UserBean that calls the method in the DAO? What I mean is should I open a transaction, and a session in the methods in the UserBean or I am correct if I only do it in the UserDAO?
Any suggestions?? Thank you....