使用hibernate的时候,程序里面,凡是HibernateSessionFactory.getSession();的地方,我最后都会HibernateSessionFactory.closeSession();
但是,跟踪也看到这个session的closed是true,实际上仍保持者和数据库的连接,从数据库Oracle的会话管理那里可以看到,这个session连接的状态为inative。
为什么会这样呢?在使用hibernate上有什么需要特别注意的地方才能够保证session的及时关闭呢?
目前项目运作时,总是不能及时释放连接,严重影响了系统运行,请高手指点~~谢谢我也试过session.close(), 每次连接数据库,都会生成一个inactive的session. 服务器越来越慢!!!急需解决这个问题!!!
但是,跟踪也看到这个session的closed是true,实际上仍保持者和数据库的连接,从数据库Oracle的会话管理那里可以看到,这个session连接的状态为inative。
为什么会这样呢?在使用hibernate上有什么需要特别注意的地方才能够保证session的及时关闭呢?
目前项目运作时,总是不能及时释放连接,严重影响了系统运行,请高手指点~~谢谢我也试过session.close(), 每次连接数据库,都会生成一个inactive的session. 服务器越来越慢!!!急需解决这个问题!!!
是否有些操作忘记了close另外,使用连接池的时候,数据库的连接并不会真正的释放,只是返回到空闲池中,查oracle的session确实是inactive,
但是也不会出现每次查询都要新增一个连接的情况,如果之前的连接池里有连接的话,就会利用原来存在的,而你说的情况好像是没有正确的close session导致了连接没有返回到连接池
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}}
每次连接都会产生一个inactive session, 已经确认过.你说的情况好像是没有正确的close session导致了连接没有返回到连接池-----------如何真正返回到连接池?可否通过什么方式查看是否返回连接池?
是否有些操作忘记了close ?
HibernateSessionFactory操作起来正常.没有忘记任何HibernateSessionFactory.closeSession()操作.
我是每次提交,每次都查看oracle里面的session. 即使使用session.close()也是同样每次产生INACTIVE SESSION.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="myeclipse.connection.profile">Ca3_Pisar</property>
<property name="connection.url">jdbc:oracle:thin:@100.110.101.212:1521:prswddew
</property>
<property name="connection.username">calparis</property>
<property name="connection.password">calparis</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<mapping resource="com/seagate/calib/bean/hbm/SrResource.hbm.xml" />
<mapping resource="com/seagate/calib/bean/hbm/RStatus.hbm.xml" />
<mapping resource="com/seagate/calib/bean/hbm/SrResLocation.hbm.xml" />
<mapping resource="com/seagate/calib/bean/hbm/SrResGroup.hbm.xml" />
</session-factory>
</hibernate-configuration>
session.close();
从配置文件上看,你还没有配置连接池。使用的是Hibernate默认连接池。找个免费产品干吧!其次数据CUD要关闭事务,及时提交才能保证oracle inactive session及时清理。inactive session--管理是DBA的事情,那个配置就费神了。虽然处于不活动状态但是也占CPU呀!
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">ckts_hn</property>
<property name="connection.password">ckts_hn</property>
<!-- C3P0连接池设定-->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">60</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">60</property>
<property name="hibernate.c3p0.acquire_increment">2</property> <property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="show_sql">true</property>
。。
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
}