These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Refactoring woes: Transaction Demarcation, Facades and DAO
PostPosted: Wed Jun 16, 2004 11:34 am 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
I'm currently refactoring our Hibernate DAO implementation and moving transaction demarcation up into our stateless session bean facades.

Currently one issue I am having is that previously my business objects (i.e. Session Beans or any other implementing object implementing our business interface) knew nothing of the Hibernate API. This was all magic encapsulated by the DAO.

However, once I move transaction demarcation into the business objects I have introduced this dependency because I need to retrieve my ThreadLocal Session, flush and close it, and handle HibernateExceptions.

Am I being anal retentive by not wanting to deal with this code in my business objects? Say I migrate to JDO one day (ya, sure... whatever) not only do I have to update my DAO implementation, now I have to update all my business interfaces as well.

Please deposit 2 cents here...

Regards,
Roll


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 12:14 pm 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
This is exactly the problem that Spring's transaction and resource management solves: DAO interfaces that abstract from the implementation technology, and generic transaction demarcation at the business facade level.

Basically, Spring will automatically associated resources like Hibernate Sessions with the current transaction, on first access - no custom ThreadLocals anymore. Business facades simply demarcate transaction, without worrying about those resources, and without being tied to the actual persistence technology.

DAOs simply fetch a resource do something with it, not caring about where it came from: When within a transaction, it will be the transactional resource (i.e. Hibernate Session); else, a new one will implicitly get created. Essentially, DAOs just implement the actual data access operations, via Spring's HibernateTemplate or SessionFactoryUtils.

Spring provides its own transaction demarcations means, but also supports JTA. The Hibernate DAO support classes will automatically adapt to the environment, i.e. associate resources with Spring transactions or JTA transactions. In the case of JTA, all you need to do is configure Hibernate's TransactionManagerLookup accordingly.

So provided that you use JTA, simply use Spring in a library style within your DAOs:

Code:
public class ProductDaoImpl implements ProductDao {

  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public List loadProductsByCategory(String category) throws DataAccessException {
    HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory);
    return hibernateTemplate.find("from test.Product product where product.category=?", category);
  }

}


Or, if you prefer to work directly on the Hibernate Session:

Code:
public class ProductDaoImpl implements ProductDao {

  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public List loadProductsByCategory(String category) throws DataAccessException {
    Session session = SessionFactoryUtils.getSession(this.sessionFactory, true);
    try {
      return session.find("from test.Product product where product.category=?", category, Hibernate.STRING);
    }
    catch (HibernateException ex) {
      throw SessionFactoryUtils.convertHibernateAccessException(ex);
    }
    finally {
      SessionFactoryUtils.closeSessionIfNecessary(this.sessionFactory);
    }
  }
}


DataAccessException is the root of Spring's unchecked DAO exception hierarchy. HibernateTemplate and SessionFactoryUtils.convertHibernateAccessException will automatically convert HibernateExceptions to appropriate DataAccessException subclasses.

Note that you just use either Spring's HibernateTemplate or SessionFactoryUtils within your DAO implementation; simply fetch the SessionFactory from wherever you hold it. With that usage style, your application does not use Spring for IoC configuration - still you can fully leverage Spring's transaction-scoped resource management!

If you're interested, have a look at the article at http://www.hibernate.org/110.html for further code-level introduction, and at the Petclinic sample app that comes with the Spring distribution for a working example that uses Spring's Hibernate support (with Spring's own transaction demarcation means).

Juergen


Top
 Profile  
 
 Post subject: Great.
PostPosted: Wed Jun 16, 2004 1:07 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
Thanks, Juergen.

I purchased Expert One-On-One... by Mr. Johnson when it first hit the stands. Many of the techniques he uses struck a chord in me then, however, I've yet to try Spring.

Given constraints on our current project I may have to postpone any spiking with Spring until the next release. I've got many questions which I'll likely post in the Spring lists/forums after scowering the docs.

Regards,
Roll


Top
 Profile  
 
 Post subject: Re: Great.
PostPosted: Wed Jun 16, 2004 2:05 pm 
Newbie

Joined: Fri Sep 26, 2003 4:29 pm
Posts: 16
Roll,

just to echo my support for using Spring. I was in your situation a few months ago. In fact I'd implemented a lot of the transaction stuff in aspectj to avoid the problems you mentioned, but I wasn't quite comfortable with using AspectJ, as I knew I'd be leaving the code for other people to maintain.

I moved to spring and my code was greatly simplifyed. I eliminated all transaction aspects, the ThreadLocal stuff and the OpenSessionInView code.

I guess I'm saying that, depending on where you are in your implementation, it might be quicker to go the spring route now. The examples given in the link are very straight forward and I was able to get the declarative transactions working in less than a morning.

Jonny

rollatwork wrote:
Thanks, Juergen.

I purchased Expert One-On-One... by Mr. Johnson when it first hit the stands. Many of the techniques he uses struck a chord in me then, however, I've yet to try Spring.

Given constraints on our current project I may have to postpone any spiking with Spring until the next release. I've got many questions which I'll likely post in the Spring lists/forums after scowering the docs.

Regards,
Roll


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.