各位高手:
  我有一个程序是用hibernate连接数据库的,但是一般跑几天就会报错:com.mchange.v2.resourcepool.ResourcePoolException: Attempted to use a closed or broken resource pool
这个问题困扰我好几天了,希望各问有经验的能帮忙解决一下,在此先谢谢了,我的代码如下:
hibernate.cfg.xml内容:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置事务实现方式 -->
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property> <!-- 配置JDBC里batch的大小 -->
<property name="jdbc.batch_size">50</property>
<property name="cache.use_second_level_cache">
false
</property> <!-- 配置线程安全的session -->
<property name="current_session_context_class">
thread
</property> <!-- 显示SQL -->
<property name="show_sql">false</property>
<property name="format_sql">true</property> <!-- 配置数据库方言 -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property> <!-- 配置数据库连接 -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.username">fidsadmin</property>
<property name="connection.password">fidsadmin</property>
<property name="connection.url">
jdbc:oracle:thin:@172.24.52.33:1521:FIDS
</property> <!-- 配置连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>    
<property name="c3p0.max_size">10</property>
<property name="c3p0.min_size">2</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.idle_test_period">120</property>
<property name="c3p0.acquire_increment">2</property>
<property name="c3p0.validate">true</property> <!-- 指定hibernate管理的映射文件 -->
<mapping resource="com/ccae/model/Flight.hbm.xml" />
<mapping resource="com/ccae/model/Counter.hbm.xml" />
<mapping resource="com/ccae/model/Gate.hbm.xml" />
<mapping resource="com/ccae/model/Carouse.hbm.xml" />
<mapping resource="com/ccae/model/AbnormalReason.hbm.xml" />
<mapping resource="com/ccae/model/Airline.hbm.xml" />
<mapping resource="com/ccae/model/Airport.hbm.xml" />
<mapping resource="com/ccae/model/Re.hbm.xml" />
<mapping resource="com/ccae/model/TaskCode.hbm.xml" />
</session-factory>
</hibernate-configuration>连接数据库的类DBHandler.java代码:
package com.ccae.service;import java.sql.SQLException;
import java.util.List;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import com.ccae.exception.DataBaseException;
import com.ccae.model.Counter;
import com.ccae.model.Flight;
import com.ccae.model.FlightId;
import com.ccae.model.Gate;
import com.ccae.util.Logger;/**
 * @author 作者 CaoJing:
 * @version 创建时间:2010-9-28 上午01:16:01 类说明
 */public class DBHandler {
private static Logger log = Logger.getLogger(DBHandler.class); static SessionFactory sf; public static final ThreadLocal tLocalsess = new ThreadLocal(); // Session session; // Transaction tx; static {
try {
sf = new Configuration().configure(
"/com/ccae/config/hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
} public static synchronized Session currentSession() {
Session session = (Session) tLocalsess.get();
try {
if (session == null || !session.isOpen()) {
if (sf == null) {
sf = new Configuration().configure(
"/com/ccae/config/hibernate.cfg.xml")
.buildSessionFactory();
}
session = sf.openSession();
tLocalsess.set(session);
}
} catch (HibernateException e) {
log.error("-------------加载hibernate配置文件失败!------------失败原因:"
+ e.getMessage());
// e.printStackTrace();
}
return session;
} public static void closeSession() {
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try {
if (session != null && session.isOpen()) {
session.close();
}
} catch (HibernateException e) {
e.printStackTrace();
}
} public static void insert(Object o) throws DataBaseException { Session session = currentSession();
if (session == null) {
throw new DataBaseException("连接数据库失败");
}
Transaction tx = session.beginTransaction();
session.saveOrUpdate(o);
tx.commit();
// session.close();
closeSession();
} public static void del(Object o) throws DataBaseException { // Session session = sf.openSession();
Session session = currentSession();
if (session == null) {
throw new DataBaseException("连接数据库失败");
}
Transaction tx = session.beginTransaction();
session.delete(o);
tx.commit();
// session.close();
closeSession(); } public static void update(Object o) throws DataBaseException {

if(o==null)return;
Session session = currentSession();
Transaction tx = session.beginTransaction();
session.update(o);
tx.commit();
// session.close();
closeSession();
} // flid 应该改为主键。这里有问题
public Flight load(FlightId flid) {
Session session = currentSession();
Flight flight = (Flight) session.get(Flight.class, flid);
// session.close();
closeSession();
return flight;
}
/**
 * * @Title: queryFlight* @Description: TODO(这里用一句话描述这个方法的作用)* @param @param flid
* @param @param dori
* @param @param ifM 表示是混合航班/是否需要变换
* @param @return    设定文件* @return Flight    返回类型* @throws
 */
public static Flight queryFlight(String flid){
String hql = "from Flight f where f.flid=? ";
Session session = currentSession();
Query query = session.createQuery(hql);
query.setString(0, flid);
List list = query.list();
if (list == null || list.size() == 0){
log.error("F3001表不存在FLID="+flid);
return null;
}
else {
Flight flight = (Flight) query.list().get(0);
// session.close();
closeSession();
return flight;
}
}
public static Flight queryFlight(String flid, String dori,boolean ifM) {
String dori_t = dori;
if(!ifM){
if(dori.equals("J")||(dori.equals("D"))){
dori_t = "I";
}else{
dori_t = "D";
}
}
String hql = "from Flight f where f.flid=? ";
hql = hql + "and f.id.dorI=?";
Session session = currentSession();
Query query = session.createQuery(hql);
query.setString(0, flid);
query.setString(1, dori_t);

List list = query.list();
if (list == null || list.size() == 0){
log.error("F3001表不存在FLID="+flid+",DORI="+dori_t+"的航班。");
return null;
}
else {
Flight flight = (Flight) query.list().get(0);
// session.close();
closeSession();
return flight;
}
}
public static void transferHistory(){
Session session = currentSession();
Transaction tx = session.beginTransaction();
String procedure = "{Call TRANSFER_HISTORY()}";
try{
session.connection().prepareCall(procedure);
}catch(SQLException e){
log.error("执行存储过程失败"+e.getStackTrace());
}
tx.commit();
closeSession();

} public static Gate queryGate(String gateId) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Gate gate = (Gate) session.get(Gate.class, gateId);
tx.commit();
// session.close();
closeSession();
return gate;
} public static Counter queryCounter(String counterId) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Counter counter = (Counter) session.get(Counter.class, counterId);
tx.commit();
// session.close();
closeSession();
return counter;
}
/*
  public static void main(String[] args) { DBHandler db = new DBHandler();
   Flight flight = db.queryFlight("1343", "N"); 
// System.
//   db.queryGate("11"); System.out.println(gate); //
//   System.out.println(flight.getId().getFlightNo()); }
  }
  */
}

解决方案 »

  1.   

    补充一下:
    报错信息如下:
    2011-04-23 09:54:09:ERROR org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:72) org.hibernate.util.JDBCExceptionReporter - com.mchange.v2.resourcepool.ResourcePoolException: Attempted to use a closed or broken resource pool
    at com.mchange.v2.resourcepool.BasicResourcePool.ensureNotBroken(BasicResourcePool.java:920)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:191)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:170)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:172)
    at com.mchange.v2.c3p0.PoolBackedDataSource.getConnection(PoolBackedDataSource.java:58)
    at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:35)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:360)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:122)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:105)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1561)
    at org.hibernate.loader.Loader.doQuery(Loader.java:661)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    at org.hibernate.loader.Loader.doList(Loader.java:2145)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
    at org.hibernate.loader.Loader.list(Loader.java:2024)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1129)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at com.ccae.service.DBHandler.queryFlight(DBHandler.java:174)
    at com.ccae.xml.DynUpdateParse.parseXml(DynUpdateParse.java:287)
    at com.ccae.service.MessageDealService.run(MessageDealService.java:62)
      

  2.   

    为什么不用spring去托管hibernate的 session呢
      

  3.   

    在你的JAVA包里看一下,有没有冲突的;
      

  4.   

    是不是有几个地方,没有关闭session。导致数据库连接池满了?
    List list = query.list();
    if (list == null || list.size() == 0){
    .error("F3001表不存在FLID="+flid+",DORI="+dori_t+"的航班。");
    return null;
    }else{
    这里就没关。
      

  5.   

    to:xiongdan1982
    的确有两个地方没有关闭 ,已经改了代码,再测试两天看看吧。
      

  6.   

    集成了spring就让他一起管理掉pool吧
      

  7.   

    谁有集成spring的例子吗 学习一下