问题描述
每次程序运行一段时间以后,就报此错08-08-17 09:55:29 WARN JDBCExceptionReporter:71 - SQL Error: 17008, SQLState: null08-08-17 09:55:29 ERROR JDBCExceptionReporter:72 - 关闭的连接08-08-17 09:55:29 WARN JDBCExceptionReporter:71 - SQL Error: 17008, SQLState: null08-08-17 09:55:29 ERROR JDBCExceptionReporter:72 - 关闭的连接org.hibernate.exception.GenericJDBCException: could not execute queryCaused by: java.sql.SQLException: 关闭的连接我的的struts2 +hibernate3dao是 public Userinfo checkUserinfo(String name, String pwd) {Session session = HibernateSessionFactory.getSession();Transaction ta = session.beginTransaction();Userinfo userinfo = (Userinfo) session.createCriteria(Userinfo.class).add(Expression.eq("userinfoName", name)).add(Expression.eq("userinfoPassword", pwd)).uniqueResult();ta.commit();HibernateSessionFactory.closeSession();return userinfo;}问题补充:package com.shop.hibernate.util;import org.hibernate.HibernateException;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<Session> threadLocal = new ThreadLocal<Session>(); 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;}}这是自动生成的问题补充:------------------------------------------------------Caused by: java.sql.SQLException: 关闭的连接at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)at oracle.jdbc.driver.OracleConnection.privatePrepareStatement(OracleConnection.java:895)at oracle.jdbc.driver.OracleConnection.prepareStatement(OracleConnection.java:802)at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:396)at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:334)at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88)at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1162)at org.hibernate.loader.Loader.doQuery(Loader.java:390)at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)at org.hibernate.loader.Loader.doList(Loader.java:1593)... 122 more
解决方案
楼主如果确定是这块代码的问题, 能不能把HibernateSessionFactory的代码贴出来, 可能有两个原因:1, 因为dao中你已经把session关闭, 如果Userinfo中使用了lazy加载, 那么就会拿不到session2, HibernateSessionFactory.getSession()这个方法中拿到的session是同一个, 也说是单例的, 多线程共用一个session, 如果有一个线程把session关掉, 而其它的线程还没有执行完, 提交的时候也会发现已关闭, 应该是每次开一个新的session
解决方案二:
没看出什么问题, 能不能把"Caused by: java.sql.SQLException: 关闭的连接 "这个异常的堆栈贴出来!也就是整个异常信息
解决方案三:
你的service层中是怎么用DAO的?感觉 你是在哪里没有打开session就开始使用查询。或者是你在DAO层中把session连接关闭了,然后又在service层中使用它。