1

I have omitted many details from the code below just to keep it simple.
Is my implementation right?
Is there any downside of this implementation?
Is there a better way of implementing the same?

MyAction.java

class MyAction {
  public String execute() {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    try {

      UserLoginDAO uldao = new UserLoginDAOImpl();
      uldao.firstMethod(session);
      List l = uldao.secondMethod(session);
      tx.commit();

    } catch(HibernateException ex) {
      if(tx != null) tx.rollback();
    } finally {
      session.close();
    }
    //statements
  }
}

UserLoginDAOImpl.java

class UserLoginDAOImpl implements UserLoginDAO{
  public void firstMethod(session) {
    //statements
    session.save(something);
  }

  public List secondMethod(session) {
    //statements
    List lst = session.createQuery("something").list(); 
    //statements
  }
}

UserLoginDAO.java

interface UserLoginDAO {
  public void firstMethod(session);
  public List secondMethod(session);
}

1 Answers1

1

Is my implementation right?

This is off-topic here. BTW it seems fine.

Is there any downside of this implementation?

This implementation is called Open Session In View (OSIV) Pattern, often referred as Anti-Pattern.

There are pros and cons using it (also read this).

Is there a better way of implementing the same?

Better is subjective, hence saying which is better than which other would be primarily opinion based.

Leaving the "better" apart and answering just to "are there other ways to handle transactions": from JAVA EE 6, things are changed sensibly... if you are using it, then use Hibernate as JPA2 implementation (instead that as raw Hibernate) and take a look at EntityManager and container managed transaction in EJB3.1 (or CDI with Interceptor).

Community
  • 1
  • 1
Andrea Ligios
  • 46,329
  • 24
  • 102
  • 208
  • Sorry @Andrea for the delay. I was waiting for more answers. Yes, you have cleared my mind to some extent but since I am a novice, I found it hard to understand the links that you provided. I don't want to use EJB. I created a small test where OSIV worked successfully but I don't know if it will work successfully in the future. I am planning to build a blog. Could you please help me out here? – Maneet Singh Nov 18 '14 at 12:31
  • My goal is to create an ecommerce application and launch it. I am planning to create the blog so that I become familiar with the difficulties that I will face while creating the application. Blog is less complex. – Maneet Singh Nov 18 '14 at 12:50
  • Good thinking. Always start from small, then scale. Why you don't like EJB 3.1 ? Parsecs away from the ugly EJB 2.1 – Andrea Ligios Nov 18 '14 at 13:07
  • Take a look at this: http://stackoverflow.com/q/26733141/1654265. The caching part is not necessary, just the entity and the loader... if using JAVA EE >= 6. – Andrea Ligios Nov 18 '14 at 13:17
  • I took NetBean's tutorial on e commerce application [AffableBean](https://netbeans.org/kb/docs/javaee/ecommerce/intro.html). They use EJB. The problem is NetBeans does everything automatically and you don't understand what happens in the background. I looked for some tutorials on EJB. They explained how it works but none of them explained how to use it in a project. What do you say about this page? [Entity-Session](https://netbeans.org/kb/docs/javaee/ecommerce/entity-session.html). Do you have any link to good tutorials on EJB? – Maneet Singh Nov 18 '14 at 13:48
  • The page seems fine. A good approach that I'm starting right now (this month!) is ECB pattern, after having seen some videos and read some articles from Adam Bien... minimalism FTW – Andrea Ligios Nov 18 '14 at 13:54
  • P.S: about OSIV, this link from Bozho is also good: http://techblog.bozho.net/avoid-lazy-jpa-collections/ – Andrea Ligios Nov 18 '14 at 14:12