hibernate管理连接是连接池方式,因此虽然是close方法,但只是把连接交给连接池,并不是真的关闭。你只要保证在finally段里调用了Session.close()方法就可以。或者,可以参考spring对hibernate的封装。网络上也有一些简单的封装方式。
解决方案 »
- JAVA 读取 XML 问题
- BIRT中,如何通过javascript更改对象的样式?
- poi向excel写数据的错误 老抛这样的异常 急求解决 谢谢
- EJB3.0中一个EntityBeanA 采用@ManyToOne 关联另外一EntityBeanB后,CreatQuery("From EntityBeanA").getResults()无法返回EntityBeanA的List
- java同步方法与同步块静态同步的区别
- 新手在线求大神解决。。
- 如何在容器中做单元测试
- 我快要疯了求求你救救我把
- 关于一个下载程序的问题
- concurrentHashMap原理
- struts1.1中要验证文字长度不大于50在validation.xml文件中怎么写??(高分求解,进者有分),菜鸟真苦恼啊!
- 急:求一个简短方法,对一个ArrayList排序
这种情况怎么解决啊?
java.lang.NullPointerException
at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
…………不知道是哪里出了问题?
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)
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();
}
用dbcp显示数据库打开游标数超过最大值,
我用的是oracle是不是要用tomcat的连接池才能解决问题?
好像不应该这样吧?
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() {
}}
还要关sessionFactory?
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)
不知道因为什么?
用tomcat的连接池就没问题,但不知道程序运行时间长以后会不会出现资源耗尽的情况。