关闭Hibernate 4.3中的SessionFactory

Thr*_*eaT 6 java hibernate

我正在将我的Hibernate升级到最新版本.随着我的旧,HibernateUtil.java我没有任何问题,但升级它时,SessionFactory似乎不再关闭.

这是我的 HibernateUtil.java课程:

import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (HibernateException ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void closeSessionFactory() {
        sessionFactory.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 HibernateUtil.java班:

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void closeSessionFactory() {
        sessionFactory.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">pass</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>
        <property name="format_sql">false</property>
        <property name="use_sql_comments">false</property>


        <!-- Use the thread as the context -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Use these files for mapping configuration -->
        <mapping resource="test/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我创建会话的代码:

public class Helper {

    Session session = null;

    public Helper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List getPeople(int id) {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

主要方法:

public static void main(String args[]) {
    Logger log = Logger.getLogger("org.hibernate");
    log.setLevel(Level.WARNING);

    Helper helper = new Helper();
    List<Person> people = helper.getPeople(1);

    for (int i = 0; i < people.size(); i++) {
        System.out.println("people " + i + ": " + people.get(i).getID());
    }

    HibernateUtil.closeSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*ane 8

你是对的,在Hibernate 4.3.x中似乎存在一个错误,其中由Hibernate的默认连接池产生的线程在关闭时不会被清除.我在这里提交了一个错误(请投票!):

https://hibernate.atlassian.net/browse/HHH-8896

在它修复之前,你有两个选择.您可以向HibernateUtil添加一个方法,并使用它强制连接池在应用程序执行结束时自行清理:

public static void stopConnectionProvider() {
    final SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
    ConnectionProvider connectionProvider = sessionFactoryImplementor.getConnectionProvider();
    if (Stoppable.class.isInstance(connectionProvider)) {
        ((Stoppable) connectionProvider).stop();
    }        
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但是它很丑陋,使用不赞成使用方法等等.更好的解决方案是使用"真正的"连接池,如c3p0,只需将以下属性添加到hibernate.cfg即可启用. XML:

<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
Run Code Online (Sandbox Code Playgroud)

请注意,如果您使用其他连接池,则应删除当前位于配置中的此连接池属性:

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
Run Code Online (Sandbox Code Playgroud)

编辑:要使用c3p0连接池,你还需要hibernate-c3p0依赖.来自Hibernate快照repo的4.3.0-SNAPSHOT的Maven示例:

<repositories>
  ...
  <repository>
    <id>hibernate-snapshots</id>
    <url>http://snapshots.jboss.org/maven2/</url>
  </repository>
  ...
</repositories>

<dependencies>
 ...
 <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-c3p0</artifactId>
   <version>4.3.0-SNAPSHOT</version>
 </dependency>
 ...
<dependencies>
Run Code Online (Sandbox Code Playgroud)