2

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(Hibernate Recipes by Gary Mak),I created a generic base class and a specific subclass as below.

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);

            }

    }

While going through the posts in this forum ,I came across Ryan Stewart's advice 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..

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..

Hope somebody helps me with suggestions

sincerely,

Jim

Community
  • 1
  • 1
jimgardener
  • 637
  • 2
  • 10
  • 26
  • 2
    Use spring and be done with it. Otherwise you are ignoring a ready-made solution that is freely available, tested, and well documented, in favor of taking time to cobble together something that will be untested and undocumented. – Nathan Hughes Jul 21 '11 at 14:17
  • 1
    Nathan is spot on, assuming you can use spring. Aside though, do you understand what a transaction is? I'm not trying to condescend, but grokking this is important, especially if you intend to roll your own. Wikipedia's description is verbose but accurate: http://en.wikipedia.org/wiki/Database_transaction Briefly: a txn is one or more db queries which must all pass or be rolled back. Consider a bank transferring momney from one account to another. One count must be debited and the other credited. If one account is debited but the credit fails, you've sent money into "the void". – Taylor Jul 21 '11 at 14:31

1 Answers1

9

Normally transactions should not be handled in the DAO. They should be handled by the service layer. One service method may include multiple DAO calls that are all in the same transaction.

Spring (as well as other DI frameworks) allows you to do that by simply annotating your service methods with @Transactional. Without spring you can still do that manually in the service layer

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I have a question. I have 3 DAO classes for 3 different mysql schemas. All have different datasources, sessionFactories and transaction managers. I need to update tables from all 3 schemas under one transaction. Those dao calls are made from one service method. I am not able to find out how I can provide all 3 transaction managers to this service method. I am using spring & hibernate. Any idea ? – Jeevan Patil Mar 26 '13 at 12:24
  • that's worth of a separate question – Bozho Mar 26 '13 at 15:13
  • Thanks Bozho. Will it post as a separate question. – Jeevan Patil Mar 26 '13 at 15:26