java教程

查看 c3p0数据库连接池的使用情况

位置:首页 > java教程 > java技巧,2019-04-07 17:32
查看 c3p0数据库连接池的使用情况,如c3p0连接池中 【 总共 】 连接数量,c3p0连接池中 【 忙 】 连接数量,c3p0连接池中 【 空闲 】 连接数量
package com.xiangsoft.database.c3p0driver;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.PooledDataSource;

public class DatasourceUtil {

	private String driverClass;

	private String jdbcUrl;

	private String user;

	private String password;

	private String keyHost;

	private int minPoolSize = 5;

	private int maxPoolSize = 20;

	private int acquireIncrement = 5;

	private int initialPoolSize = 5;

	private int maxIdleTime = 300;

	private int defaultRowPrefetch = 200;

	private int idleConnectionTestPeriod = 30;

	private String preferredTestQuery = "SELECT 1 FROM DUAL";

	private int maxConnectionAge = 7200;

	private int maxStatementsPerConnection = 0;

	public ComboPooledDataSource dataSource;

	private Long readTimeout = 6000L;//120min - 20min


	public void initDatabase(){
		dataSource = new ComboPooledDataSource();

		Properties properties = new Properties();
		properties.setProperty("defaultRowPrefetch", String.valueOf(defaultRowPrefetch));
		//only oracle database use this property
		properties.setProperty("oracle.jdbc.ReadTimeout", String.valueOf(readTimeout * 1000));

		dataSource.setProperties(properties);

		try {
			driverClass = "com.mysql.jdbc.Driver";
			jdbcUrl = "jdbc:mysql://localhost:3306/quicklink";
			user = "root";
			password = "admin";
			dataSource.setDriverClass(driverClass);
			dataSource.setJdbcUrl(jdbcUrl);
			dataSource.setUser(user);
			dataSource.setPassword(password);

			// the settings below are optional -- c3p0 can work with defaults
			dataSource.setInitialPoolSize(initialPoolSize);
			dataSource.setMinPoolSize(minPoolSize);
			dataSource.setMaxPoolSize(maxPoolSize);
			dataSource.setAcquireIncrement(acquireIncrement);
			dataSource.setMaxIdleTime(maxIdleTime);
			dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
			dataSource.setPreferredTestQuery(preferredTestQuery);
			dataSource.setMaxConnectionAge(maxConnectionAge);
			dataSource.setMaxStatementsPerConnection(maxStatementsPerConnection);

		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	public Connection getConnection() throws SQLException {
		Connection connection = dataSource.getConnection();
		return connection;

	}

	public void cleanup()
	{
		try {
			if (dataSource != null) {
				dataSource.close();
			}
			} catch(Exception e) {
				//LOGGER.error(e);
			}

	}

	public String toString()
	{
		StringBuilder builder = new StringBuilder(super.toString());

		builder.append(",jdbcUrl=").append(jdbcUrl);
		builder.append(",user=").append(user);
		builder.append(",minSize=").append(minPoolSize);
		builder.append(",maxSize=").append(maxPoolSize);

		return builder.toString();
	}

	public static void main(String[] args) {
		DatasourceUtil util = new DatasourceUtil();
		util.initDatabase();

		util.showConnPoolInfo(util.dataSource);

		Connection conn = null;
		try {
			conn = util.getConnection();
			util.showConnPoolInfo(util.dataSource);

			conn = util.getConnection();
			conn = util.getConnection();
			conn = util.getConnection();
			conn = util.getConnection();
			util.showConnPoolInfo(util.dataSource);


			conn = util.getConnection();
			util.showConnPoolInfo(util.dataSource);


		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			util.showConnPoolInfo(util.dataSource);
		}


	}

	private static void showConnPoolInfo(ComboPooledDataSource pool){  
        PooledDataSource pds = (PooledDataSource) pool;  
        if(null != pds){  
            try {  
                System.out.println("------------c3p0连接池链接状态--------------");  
                System.out.println("c3p0连接池中 【 总共 】 连接数量:"+pds.getNumConnectionsDefaultUser());  
                System.out.println("c3p0连接池中 【  忙  】 连接数量:"+pds.getNumBusyConnectionsDefaultUser());  
                System.out.println("c3p0连接池中 【 空闲 】 连接数量:"+pds.getNumIdleConnectionsDefaultUser());  
                System.out.println("c3p0连接池中 【未关闭】 连接数量:"+pds.getNumUnclosedOrphanedConnectionsAllUsers());  
            } catch (SQLException e) {  
                System.out.println("c3p0连接池异常!");  
            }  
        }  
    }  


}

TAGS:c3p0数据库连接池

猜你喜欢

NewHot