每次程序运行一段时间以后,就报此错
08-08-17 09:55:29 WARN JDBCExceptionReporter:71 - SQL Error: 17008, SQLState: null
08-08-17 09:55:29 ERROR JDBCExceptionReporter:72 - 关闭的连接
08-08-17 09:55:29 WARN JDBCExceptionReporter:71 - SQL Error: 17008, SQLState: null
08-08-17 09:55:29 ERROR JDBCExceptionReporter:72 - 关闭的连接
org.hibernate.exception.GenericJDBCException: could not execute query
Caused by: java.sql.SQLException: 关闭的连接
我的的struts2 +hibernate3
dao是
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;
}
[b]问题补充:[/b]
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 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
SessionFactory
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; }
}
这是自动生成的
[b]问题补充:[/b]
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