1

I'm currently re-learning Hibernate, which I haven't used for some five or six years, and I'm trying to think back to the ways we used to use it at an old workplace (Hibernate in Action is on order...)

I'm not using spring in my application at present, and one thing that I'm wondering is how to best retrieve the SessionFactory in my application to share it across my persistence helpers - I seem to recall that you generally only want to use one. In my current codebase I'm choosing to do so like this in a class common to all my peristence 'helpers':

public abstract class AbstractHelper {

  private static final String CONFIG = "configuration.xml";
  private static SessionFactory sessionFactory = null;

  /**
   * @return SessionFactory the Hibernate SessionFactory.
   */
  public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
      Configuration configuration = new Configuration();
      configuration.configure(CONFIG);
      ServiceRegistryBuilder serviceRegistryBuilder = 
            new ServiceRegistryBuilder().applySettings(configuration.getProperties());
      sessionFactory = configuration.buildSessionFactory(
            serviceRegistryBuilder.buildServiceRegistry());
    }

    return sessionFactory;
  }

}

Does this even make sense to people, or should I be going about this in a different way for reasons of performance, etc?

2
  • 2
    To me it seems fine (as this is the way we used to do it in old ages). The only thing I would suggest is caring about threads. I would make the block which is building SessionFactory synchronised.
    – Ean V
    Sep 12, 2013 at 1:40
  • To be exact the whole if block (including null check).
    – Ean V
    Sep 13, 2013 at 5:34

1 Answer 1

2

There is a small chance you can get in trouble when your code is accessed from multiple threads. When two threads access getSessionFactory() one might create a SessionFactory and the other one might reach if(sessionFactory == null) before the first one is assigned to the variable.

The easiest way to fix this is to make getSessionFactory() synchronized. This might cause some locking-overhead and have an impact on performance.

For more sophisticated methods see for example here or do some googling on "Singleton pattern java".

2
  • Yep, thanks. As @Ean suggested earlier I have since synchronised the code - not on the method but using the class as a lock in a synchronized block on the code inside the null check.
    – f1dave
    Sep 12, 2013 at 8:18
  • 1
    The null-check has to be synchronized as well - otherwise the second thread in the above situation will already have entered the if-block before being stopped by the lock. Trying to avoid this will result in something like DoubleCheckLocking which has several strange difficulties as well....
    – piet.t
    Sep 12, 2013 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.