为什么我关闭不了hibernate的session?

starry11 2005-01-06 04:55:49
可以肯定的是,程序里面,凡是open了一个session的地方,我最后都会close这个session的,跟踪也看到这个session的closed是true,可是实际上仍保持者和数据库的连接,从数据库的会话管理那里可以看到,这个连接的状态为inative而已。

为什么会这样呢?在使用hibernate上有什么需要特别注意的地方才能够保证session的及时关闭呢?
我的connection.pool.size只定了为1而已。
目前项目运作时,总是不能及时释放连接,严重影响了系统运行,请高手指点~~谢谢
...全文
366 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
hujin19861102 2010-06-05
  • 打赏
  • 举报
回复
兄弟,我也遇到相同的问题,怎么解决?我是继承HibernateDaoSuport,然后this.getSession(),最后releaseSession(),查看了下Session还是没释放呀!
starry11 2005-02-01
  • 打赏
  • 举报
回复
luxx123

你改用dbcp了?
luxx123 2005-02-01
  • 打赏
  • 举报
回复
我换新的版本了,e3p0和dbcp都换高版本了,都不行。
用tomcat的连接池就没问题,但不知道程序运行时间长以后会不会出现资源耗尽的情况。
starry11 2005-01-28
  • 打赏
  • 举报
回复
现在只能怀疑是hibernate自带的cp30有bug了,不知道应该去哪里可以下载个新的回来?
starry11 2005-01-26
  • 打赏
  • 举报
回复
up
luxx123 2005-01-21
  • 打赏
  • 举报
回复
用e3p0间接池在insert和update数据库的时候就会报Problem with checked-in Statement, discarding
不知道因为什么?
starry11 2005-01-20
  • 打赏
  • 举报
回复
我跟踪了一下,发现异常发生在tran.coommit()的时候

objSession.save(object);
tran.commit();

异常如下

Problem with checked-in Statement, discarding.
java.lang.NullPointerException
at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
at oracle.jdbc.driver.OraclePreparedStatement.clearParameters(OraclePreparedStatement.java:3401)
at com.mchange.v2.c3p0.stmt.GooGooStatementCache.refreshStatement(GooGooStatementCache.java:460)
at com.mchange.v2.c3p0.stmt.GooGooStatementCache.checkinStatement(GooGooStatementCache.java:139)
at com.mchange.v2.c3p0.impl.C3P0PooledConnection$1WrapperStatementHelper.doClose(C3P0PooledConnection.java:511)
at com.mchange.v2.c3p0.impl.C3P0PooledConnection$2.close(C3P0PooledConnection.java:570)
at net.sf.hibernate.impl.BatcherImpl.closePreparedStatement(BatcherImpl.java:270)
at net.sf.hibernate.impl.BatcherImpl.closeStatement(BatcherImpl.java:140)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)



孙亖 2005-01-20
  • 打赏
  • 举报
回复
gz
luxx123 2005-01-19
  • 打赏
  • 举报
回复
我也是啊,怎么回事?
还要关sessionFactory?
starry11 2005-01-19
  • 打赏
  • 举报
回复
那其实和我上面的写法基本一样
chainli 2005-01-18
  • 打赏
  • 举报
回复
不仅要session.close(),还要SessionFactory.close().前几天不知道,程序运行一会儿,开了200多个session,汗!!1
pigo 2005-01-18
  • 打赏
  • 举报
回复

myclipse生成的例子:

package com.mywebapp.hibernate;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.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.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();

/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();

/** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory;

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null) {
System.out.println("开始创建session");
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}

return session;
}

/**
* 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) {
System.out.println("关闭session");
session.close();
}
}

/**
* Default constructor.
*/
private HibernateSessionFactory() {
}

}


luxx123 2005-01-18
  • 打赏
  • 举报
回复
SessionFactory.close()掉,那下次查询还要重新建SessionFactory?
好像不应该这样吧?
luxx123 2005-01-17
  • 打赏
  • 举报
回复
我也遇到同样的问题,用c3p0显示Problem with checked-in Statement, discarding.错误。
用dbcp显示数据库打开游标数超过最大值,
我用的是oracle

是不是要用tomcat的连接池才能解决问题?
starry11 2005-01-17
  • 打赏
  • 举报
回复
有啊,我是用threadlocal来管理的

public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
{
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
baffling 2005-01-17
  • 打赏
  • 举报
回复
你关闭session后,设该session=null试试。另外,一般推荐用threadlocal来管理session
starry11 2005-01-17
  • 打赏
  • 举报
回复
完整的:

Problem with checked-in Statement, discarding.
java.lang.NullPointerException
at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
at oracle.jdbc.driver.OraclePreparedStatement.clearParameters(OraclePreparedStatement.java:3401)
at com.mchange.v2.c3p0.stmt.GooGooStatementCache.refreshStatement(GooGooStatementCache.java:460)
at com.mchange.v2.c3p0.stmt.GooGooStatementCache.checkinStatement(GooGooStatementCache.java:139)
at com.mchange.v2.c3p0.impl.C3P0PooledConnection$1WrapperStatementHelper.doClose(C3P0PooledConnection.java:511)
at com.mchange.v2.c3p0.impl.C3P0PooledConnection$2.close(C3P0PooledConnection.java:570)
at net.sf.hibernate.impl.BatcherImpl.closePreparedStatement(BatcherImpl.java:270)
at net.sf.hibernate.impl.BatcherImpl.closeStatement(BatcherImpl.java:140)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
starry11 2005-01-17
  • 打赏
  • 举报
回复
配了C3P0后,连接问题解决了,但是却经常出这样的异常:

Problem with checked-in Statement, discarding.
java.lang.NullPointerException
at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
…………

不知道是哪里出了问题?
lveyo 2005-01-10
  • 打赏
  • 举报
回复
我也用了C3P0,还有dbcp连接池,程序出问题,还不如不用正常。
这种情况怎么解决啊?
starry11 2005-01-10
  • 打赏
  • 举报
回复
改用了C3P0去管理连接池,但情况并没有好转 :(
加载更多回复(5)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧