我正在将我的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();
}
}
这是我的旧 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();
}
}
这是我的 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>
我在其中创建会话的代码:
public class Helper {
Session session = null;
public Helper() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public List getPeople(int id) {
...
}
}
主要方法:
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();
}
答案 0 :(得分: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();
}
}
这很有用,但它很难看,使用不赞成的方法等等。更好的解决方案就是使用“真正的”连接池,比如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>
请注意,如果您使用其他连接池,则应删除当前位于配置中的此连接池属性:
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
编辑:要使用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>
答案 1 :(得分:1)
Hibernate版本4.3.5似乎已经解决了这个问题。
sessionFactory.close();
在我的计划中有效。
答案 2 :(得分:0)
主应用程序终止时释放serviceRegistry。
public class Service {
private SessionFactory factory;
private ServiceRegistry serviceRegistry;
public void initialize() throws Exception{
Configuration configuration = new Configuration();
configuration.configure("com/jeecourse/config/hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
factory = configuration.buildSessionFactory(serviceRegistry);
}
public void close() throws Exception{
if(serviceRegistry!= null) {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}