为Hibernate 4.3返回SessionFactory Singleton对象

时间:2014-06-11 05:31:07

标签: java hibernate sessionfactory

我正在使用Hibernate 4.3。我试图从SessionFactory类中获取HibernateUtil对象。我试图只为整个应用程序获取一个SessionFactory对象,即应用程序的Singleton对象。

下面是我目前的代码,我想让它成为Singleton类。

HibernateUtil.java

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static StandardServiceRegistry serviceRegistry; 

    public static SessionFactory getSessionFactory() {
      Configuration configuration = new Configuration();
      configuration.configure();
      serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
      return sessionFactory;
  }

}

请为此建议或提供示例代码。 感谢。

3 个答案:

答案 0 :(得分:1)

只需应用单例模式

public class HibernateUtil {

  private static StandardServiceRegistry serviceRegistry; 
  private volatile static SessionFactory INSTANCE = null;

  public static SessionFactory getSessionFactory() {
    if(INSTANCE==null){
      createSessionFactory():
    }
    return sessionFactory;
  }

  private synchronized static void createSessionFactory() {
    if(INSTANCE!=null){return;}

    Configuration configuration = new Configuration();
    configuration.configure();

    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    INSTANCE = configuration.buildSessionFactory(serviceRegistry);
  }
}

答案 1 :(得分:1)

正如所建议的,我将HibernateUtil.java类设为Singleton。 找到以下代码,请建议/评论以确认代码的正确性。

以下是更新后的代码:

public class HibernateUtil {

private static HibernateUtil instance = null;

   private static SessionFactory sessionFactory;
   private static StandardServiceRegistry serviceRegistry;

   private HibernateUtil(){
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
   }

   public static HibernateUtil getInstance(){
        if(instance == null){
            instance  = new HibernateUtil();
        }
        return instance;
   }

   public static SessionFactory getSessionFactory() {
        return sessionFactory;
   }

}

如果代码不正确,请进行更正,并建议正确的方法。

答案 2 :(得分:1)

而不是将HibernateUtil设为singleton.Make SessionFactory为singleton。

 public class HibernateUtil {

       private static SessionFactory sessionFactory;

       static {

            Configuration configuration = new Configuration();
            configuration.configure();
            StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory=configuration.buildSessionFactory(serviceRegistry);
       }

       public static SessionFactory getSessionFactory() {
             return sessionFactory;
       }
    }

以上方法的优点如下

1.不同步代码(避免每次方法调用的锁定和解锁) 2.没有条件 3.sessionfactory是eagly instantiation。(静态块在课堂上只执行一次) 4.application starup本身你可以在配置文件中找到错误。

请建议/评论这个..

感谢