poiyang 2008-08-17 10:02
浏览 1208
已采纳

SQL Error: 17008, SQLState: null

每次程序运行一段时间以后,就报此错
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

  • 写回答

3条回答 默认 最新

  • iteye_14762 2008-08-17 11:23
    关注

    楼主如果确定是这块代码的问题, 能不能把HibernateSessionFactory的代码贴出来, 可能有两个原因:
    1, 因为dao中你已经把session关闭, 如果Userinfo中使用了lazy加载, 那么就会拿不到session
    2, HibernateSessionFactory.getSession()这个方法中拿到的session是同一个, 也说是单例的, 多线程共用一个session, 如果有一个线程把session关掉, 而其它的线程还没有执行完, 提交的时候也会发现已关闭, 应该是每次开一个新的session

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数