dbcp 总是偶尔java.lang.IllegalStateException: Pool not open
@红薯 我使用dbcp 正常是没问题的但是刷新过快偶尔就出现
java.lang.IllegalStateException: Pool not open
org.apache.commons.pool.BaseObjectPool.assertOpen(BaseObjectPool.java:140)
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1079)
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
org.apache.commons.dbutils.AbstractQueryRunner.prepareConnection(AbstractQueryRunner.java:175)
org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:286)
com.navitek.yq.dao.DaoImpl.executeQuerySql(DaoImpl.java:276)
com.navitek.yq.doservlet.StoryDetailDO.headPart(StoryDetailDO.java:157)
com.navitek.yq.servlet.AllServlet.doGet(AllServlet.java:186)
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
javax.servlet.http.HttpServlet.service(HttpServlet.java:730)
com.navitek.yq.webFilter.doFilter(webFilter.java:22)
使用的是dbcp +dbUtiles 而且这个错误程序虽然try-catch 却无法捕捉,这个错误是jsp出现的。dao层代码如下:
package com.navitek.yq.dao; import java.lang.reflect.Field; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import com.mysql.jdbc.JDBC4Connection; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import com.navitek.util.Config; /** */ public class DaoImpl implements Dao { private QueryRunner qryRun; private java.sql.DatabaseMetaData dbmb; public DaoImpl() { qryRun = DbHelper.getQueryRunner(); dbmb = getDatabaseMetaData(); } private java.sql.DatabaseMetaData getDatabaseMetaData() { Properties info = new Properties(); info.setProperty("user", Config.getString("mysql.user")); info.setProperty("password", Config.getString("mysql.passwd")); java.sql.DatabaseMetaData metaData = null; try { metaData = new JDBC4Connection(Config.getString("mysql.ip"), Config .getInt("mysql.port"), info, Config .getString("mysql.database"), null).getMetaData(); } catch (SQLException e) { e.printStackTrace(); } return metaData; } public <T> T findById(Class<T> clazz, int id) { long l1=System.currentTimeMillis(); ResultSetHandler<T> rsh = new BeanHandler<T>(clazz); T result = null; try { String primary_key = "id"; if (!"".equals(primary_key) || null != primary_key) { result = qryRun.query("select * from " + clazz.getSimpleName().toLowerCase() + " where " + primary_key + "=?", rsh, new Object[] { id }); System.out.println("select * from " + clazz.getSimpleName().toLowerCase() + " where " + primary_key + "=?"); } else { throw new SQLException("This table has not primary key"); } } catch (SQLException e) { e.printStackTrace(); } finally { DbHelper.closeDataSource(); } long l2=System.currentTimeMillis(); System.out.println(l2-l1); return result; } public List<Map<String, Object>> executeQuery(String sql, Object... args) { MapListHandler rsh = new MapListHandler(); List<Map<String, Object>> result = null; try { result = qryRun.query(sql, rsh, args); } catch (SQLException e) { e.printStackTrace(); } finally { DbHelper.closeDataSource(); } return result; } }连接池类
public class DbHelper { private static DataSource dataSource; private DbHelper() { } /**初始化datasourec * */ public static void initialzeDataSource() { if (DbHelper.dataSource == null) { BasicDataSource dbcpDataSource = new BasicDataSource(); dbcpDataSource .setUrl(Config.getString("mysql.dburl")); dbcpDataSource.setDriverClassName("com.mysql.jdbc.Driver"); dbcpDataSource.setUsername(Config.getString("mysql.user")); dbcpDataSource.setPassword(Config.getString("mysql.passwd")); dbcpDataSource.setDefaultAutoCommit(true); dbcpDataSource.setMaxActive(Config.getInt("mysql.max.active")); dbcpDataSource.setMaxIdle(Config.getInt("mysql.max.idle")); dbcpDataSource.setMaxWait(Config.getInt("mysql.max.wait")); /*This causes a very cheap test query to always be run first; if the connection has been closed, this gets the failure, and then a new connection is opened*/ dbcpDataSource.setValidationQuery("select 1"); DbHelper.dataSource = (DataSource) dbcpDataSource; } } public static QueryRunner getQueryRunner() { initialzeDataSource(); return new QueryRunner(DbHelper.dataSource); } public final static void closeDataSource() { try { dataSource.getClass().getMethod("close").invoke(dataSource); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /**使用jdbc得到connect调用的方法 * @return * @throws SQLException */ public static Connection getConnection() throws SQLException { initialzeDataSource(); Connection con = DbHelper.dataSource.getConnection(); return con; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你把你代码贴下 还有错误发生的情况。这我怎么看。以前我的这个是因为没有关闭造成的,谁知道你?
请问什么原因,我遇到了同样的问题?严重:
Servlet.service() for servlet spring threw exception
java.lang.IllegalStateException: Pool closed
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1135)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:182)
at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:268)
at com.arcsoft.cloud.common.dao.BaseDaoImpl.queryForObject(BaseDaoImpl.java:39)
at com.arcsoft.cloud.core.auth.service.AuthServiceImpl.validateSigature(AuthServiceImpl.java:26)
原因找到了,对阿里的任何开源都无爱。
@wenshao 很会做广告呀 各种数据库连接池的地方 都有你的广告 呵呵
更换为阿里巴巴的开源数据库连接池吧
文档看这里:https://github.com/AlibabaTech/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98