【发布时间】:2014-01-07 16:05:56
【问题描述】:
我正在将我的 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();
}
【问题讨论】:
-
当我运行我的
main方法时,程序永远不会停止它只是继续运行,因为仍有会话打开。我像往常一样调用closeSessionFactory(),但现在它实际上并没有导致程序在执行后停止。 -
发布您在其中创建会话的代码。
-
首先使用 session.close() 关闭你的。
-
即使我先关闭会话也不会停止