我的应用程序已经长期运行的事务,因此我想在每一个方法的末尾选择session.close(),以确保连接对象不长的时间无限期持有.
当使用session.close()选项时,我可以看到Hibernate的会话对象和从session.connection()获得的相应Connection对象被正确销毁.但问题在于连接池.即使在关闭会话后,会话获得的连接也不会释放回连接池.其他请求等待从池中连接.
我在我的应用程序中使用JTA事务.在hibernate.cfg.xml中,我将connection.release_mode设置为auto(默认值),将connection.autocommit设置为true.
有人遇到过这个问题吗?请让我知道我在这里失踪了什么.
后续:这是我的hibernate配置文件详细信息:
<property name="connection.datasource">MXoraDS</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.release_mode">after_statement</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.JTASessionContext</property>
<property name="transaction.auto_close_session">true</property>
<property name="max_fetch_depth">2</property>
Run Code Online (Sandbox Code Playgroud)
我们在连接到Oracle DB的应用程序层使用JSF和EJB 2.1.在after_statement似乎没有释放到连接池.如果您需要更多详细信息,请与我们联系.
在版本4.3.0.Final的hibernate文档中,给出了以下代码片段来创建SessionFactory:
package org.hibernate.tutorial.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎已经过时,因为该方法buildSessionFactory()已被弃用.创建的正确方法是 …
以前我使用以下代码来配置sessionFactory,但是在将我的hibernate版本从4.2.1.Final升级到4.3.4.Final后,我无法使用以下代码检索sessionFactory,因为不推荐使用ServiceRegistryBuilder().
我使用此链接创建它,但提供的函数没有返回任何东西,因此它遇到预编译错误.
private static SessionFactory configureSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (HibernateException e) {
System.out.append("** Exception in SessionFactory **");
e.printStackTrace();
}
return sessionFactory;
}
static {
try {
sessionFactory = configureSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateUtil() {
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() throws HibernateException { …Run Code Online (Sandbox Code Playgroud)