Note that there are some explanatory texts on larger screens.

plurals
  1. POimplementing a dao class properly to manage transactions
    primarykey
    data
    text
    <p>I am working on a java web application that calls database backend through hibernate.I use servlets,jsp and tomcat for test/deployment.Most books on java-ee suggested using Dao classes for database calls.As per examples given in books(<strong>Hibernate Recipes by Gary Mak</strong>),I created a generic base class and a specific subclass as below.</p> <pre><code>class BaseDao{ private Class persistentClass; public BaseDao(Class persistentClass) { super(); this.persistentClass = persistentClass; } public Object findById(Long id) { SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.openSession(); Object object = null; try { object = (Object) session.get(persistentClass, id); return object; } finally { session.close(); } } @Override public void saveOrUpdate(Object obj) { SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.saveOrUpdate(obj); tx.commit(); }catch(HibernateException e){ if (tx != null) { tx.rollback(); } throw e; }finally { session.close(); } } } class SaleOrderDao extends BaseDao{ public SaleOrderDao() { super(SaleOrder.class); } @Override public SaleOrder findSaleOrderById(Long saleOrderId){ SaleOrder so = (SaleOrder)findById(saleOrderId); return so; } @Override public void saveOrUpdateSaleOrder(SaleOrder so){ saveOrUpdate( so); } } </code></pre> <p>While going through the posts in this forum ,I came across <strong>Ryan Stewart's</strong> <a href="https://stackoverflow.com/questions/6763268/stalestateexception-while-deleting-through-hibernate/6769140#6769140">advice</a> that beginning and ending transactions in a dao method is not recommended..Sadly, my project does not use any web framework that supports transaction management..and I am limited to using jsp,servlets and a servlet container..</p> <p>Is there some way I can rewrite my dao implementations sothat transactions can be managed properly..I couldn't find anything in this regard from those books I read.. </p> <p>Hope somebody helps me with suggestions</p> <p>sincerely,</p> <p>Jim</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload