关闭c3p0数据源报异常

aoaoxiong 发布于 2011/12/15 20:24
阅读 6K+
收藏 1

解读下一代网络:算力网络正从理想照进现实!>>>

写了一个使用c3p0的例子,完成读取操作后准备关闭数据源,报异常了

/**
 * 数据库管理
 * @since 2011-12-15
 * @author shiwx
 */
public class DBManager {
	private static Logger logger = Logger.getLogger(DBManager.class);
	private static DataSource dataSource;
	private static Connection connection = null;
	
	/**
	 * 初始化数据库连接池(可在启动容器时执行)
	 */
	public static final void initDataSource() {
		logger.info("------ init datasource ------");
		
		//获取配置文件
		Properties dbProps = new Properties();
		try {
			dbProps.load(DBManager.class.getResourceAsStream("db.properties"));
			dataSource =  (DataSource) Class.forName(dbProps.getProperty("jdbc.datasource")).newInstance();
			
			//设置数据源信息
			if(dbProps.getProperty("jdbc.datasource").indexOf("c3p0") > -1){
				logger.info("------ config c3p0 ------");
				((ComboPooledDataSource) dataSource).setDriverClass(dbProps.getProperty("jdbc.driver"));
				((ComboPooledDataSource) dataSource).setJdbcUrl(dbProps.getProperty("jdbc.url"));
				((ComboPooledDataSource) dataSource).setUser(dbProps.getProperty("jdbc.user"));
				((ComboPooledDataSource) dataSource).setPassword(dbProps.getProperty("jdbc.password"));			
				//设置最大连接数
				((ComboPooledDataSource) dataSource).setMaxPoolSize(Integer.parseInt(dbProps.getProperty("jdbc.maxPoolSize")));
				//设置最小连接数
				((ComboPooledDataSource) dataSource).setMinPoolSize(Integer.parseInt(dbProps.getProperty("jdbc.minPoolSize")));
			}else if(dbProps.getProperty("jdbc.datasource").indexOf("dbcp") > -1){
				logger.info("------ config dbcp ------");
				((BasicDataSource) dataSource).setUrl(dbProps.getProperty("jdbc.url"));
				((BasicDataSource) dataSource).setDriverClassName(dbProps.getProperty("jdbc.driver"));
				((BasicDataSource) dataSource).setUsername(dbProps.getProperty("jdbc.user"));
				((BasicDataSource) dataSource).setPassword(dbProps.getProperty("jdbc.password"));
				//初始连接数
				((BasicDataSource) dataSource).setInitialSize(1);
				//最小空闲连接数量
				((BasicDataSource) dataSource).setMinIdle(Integer.parseInt(dbProps.getProperty("jdbc.minPoolSize")));	
				//最大活动链接
				((BasicDataSource) dataSource).setMaxActive(Integer.parseInt(dbProps.getProperty("jdbc.maxPoolSize")));	
				//允许缓存PreparedStatement
				((BasicDataSource) dataSource).setPoolPreparedStatements(true);
			}else{
				throw new Exception("------ only support c3p0 and dbcp ------");
			}
		} catch (Exception e) {
			logger.error("------ init datasource fail ------");
			e.printStackTrace();
		}
	}
	
	/**
	 * 注销数据源(可在容器关闭时执行)
	 */
	public static final void destoryDataSource(){
		logger.info("------ destroy datasource ------");
		try {
			Method close = dataSource.getClass().getMethod("close");
			close.invoke(dataSource);
		} catch (Exception e) {
			logger.error("------ destroy datasource fail ------");
			e.printStackTrace();
		}
	} 
	
	/**
	 * 获取Connection
	 */
	public Connection getConnection(){
			try {
				connection = dataSource.getConnection();
			} catch (SQLException e) {
				logger.error("------ get connection error ------");
				e.printStackTrace();
			}
			return connection;
	}
	
	/**
	 * 关闭Connection
	 */
	public static final void closeConnection(Connection conn){
		try{
			if(conn!=null && !conn.isClosed()){
				conn.setAutoCommit(true);
				conn.close();
			}
		}catch(Exception e){
			logger.error("------ close connection error: " +  e.getMessage() +" ------");
		}
		
	}
}

测试:

/**
 * 测试
 * @author shiwx
 * @since 2011-12-15
 */
public class Test {
	public static void main(String[] args) {
		//初始化数据源
		DBManager.initDataSource();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try{
			connection = new DBManager().getConnection();
			statement = connection.createStatement();
			//此处sql语句结束不能加 分号
			resultSet = statement.executeQuery("SELECT * FROM TAB");		
			
			while(resultSet.next()){
				System.out.println(resultSet.getString("TNAME"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(resultSet!=null && !resultSet.isClosed()){
					resultSet.close();
				}
				if(statement!=null && !statement.isClosed()){
					statement.close();
				}
				if(connection!=null && !connection.isClosed()){
					connection.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}
		//关闭数据源
		DBManager.destoryDataSource();
	}
}

执行这行报异常

DBManager.destoryDataSource();

2011-12-15 19:33:49  ------ destroy datasource ------

2011-12-15 19:33:49  Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main] interrupted. Shutting down.

2011-12-15 19:33:49  MBean: com.mchange.v2.c3p0:type=PooledDataSource[1hge16d8ki69ac1iut5ep|b8bef7] unregistered.

2011-12-15 19:33:49  C3P0Registry mbean unregistered.

2011-12-15 19:33:49  Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main] interrupted. Shutting down.

2011-12-15 19:33:49  Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main] interrupted. Shutting down.

2011-12-15 19:33:49  Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@1ee2c2c

2011-12-15 19:33:49  Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@1ee2c2c

2011-12-15 19:33:49  com.mchange.v2.c3p0.ComboPooledDataSource@b8bef7 has been closed. 

java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().

at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:417)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at jdbc.util.DBManager.destoryDataSource(DBManager.java:81)

at jdbc.Test.main(Test.java:41)

2011-12-15 19:33:49  com.mchange.v2.c3p0.impl.NewPooledConnection@1ee2c2c closed by a client.

java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE

at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:491)

at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:191)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)

at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)

at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)

at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)

at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)

2011-12-15 19:33:49  Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@1ee2c2c

2011-12-15 19:33:49  Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@1ee2c2c

2011-12-15 19:33:49  Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@6210fb

2011-12-15 19:33:49  Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@6210fb

2011-12-15 19:33:49  com.mchange.v2.c3p0.impl.NewPooledConnection@6210fb closed by a client.

java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE

at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:491)

at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:191)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)

at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)

at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)

at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)

at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)

2011-12-15 19:33:49  Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@6210fb

2011-12-15 19:33:49  Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@6210fb

2011-12-15 19:33:49  Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@48edb5

2011-12-15 19:33:49  Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@48edb5

2011-12-15 19:33:49  com.mchange.v2.c3p0.impl.NewPooledConnection@48edb5 closed by a client.

java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE

at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:491)

at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:191)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)

at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)

at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)

at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)

at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)

2011-12-15 19:33:49  Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@48edb5

2011-12-15 19:33:49  Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@48edb5

请问如何处理,同时请帮忙看看DBManager.java还有没有问题,可不可以再优化下,如有,最好说明下原因

加载中
0
红薯
红薯
这是因为日志设置了调试级别,所以输出了很多的调试信息,实际是没问题的。
0
aoaoxiong
aoaoxiong

引用来自“红薯”的答案

这是因为日志设置了调试级别,所以输出了很多的调试信息,实际是没问题的。
原来如此,谢了
0
MUJO
MUJO
这样啊?
返回顶部
顶部