在我的应用程序中在哪里打开和在哪里关闭SessionFactory [英] Where to open and where to close SessionFactory in my Application

查看:105
本文介绍了在我的应用程序中在哪里打开和在哪里关闭SessionFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发jsf应用程序,并使用hibernate作为后端.我想创建会话工厂,并在整个应用程序中关闭它.我正在使用util类创建Session工厂.

I am developing jsf application and using hibernate as back end. I want create session factory and close it once through out application. I am creating Session factory with util class.

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil
{
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static
{
    try
    {
        Configuration configuration = new Configuration().configure();

        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }
    catch (HibernateException he)
    {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
}

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

 public static void closeFactory() {
    if (sessionFactory != null) {
        try {
            sessionFactory.close();
        } catch (HibernateException ignored) {
            log.error("Couldn't close SessionFactory", ignored);
        }
    }
}

在我的DAO类的每种方法中,我都在打开会话工厂并关闭它.因此,在我只能打开/关闭会话工厂一次的应用程序中. 预先感谢.

In my every method of DAO classes I am opening session factory and closing it. So where i can open/close the session factory only once for application. Thanks in advance.

推荐答案

在我的应用程序中何处打开和关闭SessionFactory

Where to open and where to close SessionFactory in my Application

您正在使用单例会话工厂对象.因此,您可以使用类名称调用getSessionFactory()方法.因此您确实需要每次为会话工厂创建对象.

You are using singleton session factory object. So you can call the getSessionFactory() method with class name. So that you do need to create object for session factory every time.

您的DAO类方法应该是这样

Your DAO class methods should like this

DAO类中的方法

public boolean createUser(NewUserDTO newUserDTO) {
    try {
        sessionFactory = DBUtils.getSessionFactory();
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        session.save(newUserDTO);
        session.getTransaction().commit();
    } catch (RuntimeException runtimeException) {
        LOGGER.error(runtimeException);
        transaction.rollback();
        return false;
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return true;
}

并且必须为每种方法关闭会话.这样就不会为每个类都创建会话工厂.

And session must be closed for every method. So that you are not creating session factory for every class.

这篇关于在我的应用程序中在哪里打开和在哪里关闭SessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆