如何获取到C3P0连接池当前的状态

J2EE 码拜 9年前 (2015-04-19) 1615次浏览 0个评论
 

在配置的时候,可以配置连接池的各个属性,如最大连接数,最小等等值,但是在运行的时候,如何能够得到当前运行时的一些状态信息呢,如当前的连接数,空闲连接数,总连接数呢?

坐等大神回答!

如何获取到C3P0连接池当前的状态
10分
datasource是一个bean,那就可以获取,获取后可以先获取连接,然后用getMetaData里获取各种信息。
如何获取到C3P0连接池当前的状态
引用 1 楼 fangmingshijie 的回复:

datasource是一个bean,那就可以获取,获取后可以先获取连接,然后用getMetaData里获取各种信息。

我是通过
net.sf.hibernate.SessionFactory sessionFactory
sessionFactory.openSession()
currentSession().connection()

取到的链接,那如何获取到datasource呢?

如何获取到C3P0连接池当前的状态
自己去看如何获取bean。
如何获取到C3P0连接池当前的状态
官方文档上好象有说明,有一个网页什么的可以直接看到
如何获取到C3P0连接池当前的状态
官方文档上好象有说明,在网页上就可以直接看到 
如何获取到C3P0连接池当前的状态
引用 3 楼 fangmingshijie 的回复:

自己去看如何获取bean。

你说的是获取的是DatabaseMetaData吧

DatabaseMetaData  dbmd  = (DatabaseMetaData)conn.getMetaData();

这不是我想要的!!

我要的是获取到当前连接池的状态,如连接池当前的连接数,空闲连接数等等。

如何获取到C3P0连接池当前的状态
10分
我不知道你的环境是什么.所以只贴部分代码:
1.ComboPooledDataSource主要使用这个类的方法获取状态
2.如果你是用DataSource这个接口来接收的.ComboPooledDataSource请先强转回.ComboPooledDataSource;

ComboPooledDataSource ds = new ComboPooledDataSource();
		System.out.println(ds.getMaxPoolSize());// 最大连接数
		System.out.println(ds.getMinPoolSize());// 最小连接数
		System.out.println(ds.getNumBusyConnections());// 正在使用连接数
		System.out.println(ds.getNumIdleConnections());// 空闲连接数
		System.out.println(ds.getNumConnections());// 总连接数

其实这个看API比较好,不知道楼主英文水平怎么样,我找了一个中文的API这是传送门http://wenku.baidu.com/link?url=Mle05gtmNKkQ3bfZMAkK5fvFe2_-BH5z2Q4fXdJ7xAYTHuNsrp8KveztmPcfo1EiObHC-9OKayh_yI-y-MjWIQxfuINY_ZuH346P8lloaEG
如果英文不错的话建议直接看C3P0英文API,网上很多你就自行百度吧

如何获取到C3P0连接池当前的状态
10分
看了楼主的回答,我估计楼是使用SPING + HIBERNATE吧,你在SRPING里配置好ComboPooledDataSource的BEAN让SPRING帮你注入就行了.你直接拿来用
如何获取到C3P0连接池当前的状态
引用 7 楼 dandan8989251 的回复:

我不知道你的环境是什么.所以只贴部分代码:
1.ComboPooledDataSource主要使用这个类的方法获取状态
2.如果你是用DataSource这个接口来接收的.ComboPooledDataSource请先强转回.ComboPooledDataSource;

ComboPooledDataSource ds = new ComboPooledDataSource();
		System.out.println(ds.getMaxPoolSize());// 最大连接数
		System.out.println(ds.getMinPoolSize());// 最小连接数
		System.out.println(ds.getNumBusyConnections());// 正在使用连接数
		System.out.println(ds.getNumIdleConnections());// 空闲连接数
		System.out.println(ds.getNumConnections());// 总连接数

其实这个看API比较好,不知道楼主英文水平怎么样,我找了一个中文的API这是传送门http://wenku.baidu.com/link?url=Mle05gtmNKkQ3bfZMAkK5fvFe2_-BH5z2Q4fXdJ7xAYTHuNsrp8KveztmPcfo1EiObHC-9OKayh_yI-y-MjWIQxfuINY_ZuH346P8lloaEG
如果英文不错的话建议直接看C3P0英文API,网上很多你就自行百度吧

 Connection conn; //获取到的链接
DatabaseMetaData  dbmd  = (DatabaseMetaData)conn.getMetaData();

如何转换成ComboPooledDataSource

如何获取到C3P0连接池当前的状态
引用 8 楼 dandan8989251 的回复:

看了楼主的回答,我估计楼是使用SPING + HIBERNATE吧,你在SRPING里配置好ComboPooledDataSource的BEAN让SPRING帮你注入就行了.你直接拿来用

如何拿,求指教,求鞭打,求调教

如何获取到C3P0连接池当前的状态
5分
引用 9 楼 fulong258 的回复:
Quote: 引用 7 楼 dandan8989251 的回复:

我不知道你的环境是什么.所以只贴部分代码:
1.ComboPooledDataSource主要使用这个类的方法获取状态
2.如果你是用DataSource这个接口来接收的.ComboPooledDataSource请先强转回.ComboPooledDataSource;

ComboPooledDataSource ds = new ComboPooledDataSource();
		System.out.println(ds.getMaxPoolSize());// 最大连接数
		System.out.println(ds.getMinPoolSize());// 最小连接数
		System.out.println(ds.getNumBusyConnections());// 正在使用连接数
		System.out.println(ds.getNumIdleConnections());// 空闲连接数
		System.out.println(ds.getNumConnections());// 总连接数

其实这个看API比较好,不知道楼主英文水平怎么样,我找了一个中文的API这是传送门http://wenku.baidu.com/link?url=Mle05gtmNKkQ3bfZMAkK5fvFe2_-BH5z2Q4fXdJ7xAYTHuNsrp8KveztmPcfo1EiObHC-9OKayh_yI-y-MjWIQxfuINY_ZuH346P8lloaEG
如果英文不错的话建议直接看C3P0英文API,网上很多你就自行百度吧

 Connection conn; //获取到的链接
DatabaseMetaData  dbmd  = (DatabaseMetaData)conn.getMetaData();

如何转换成ComboPooledDataSource

你如何拿到的 Connection conn; 是不是用SPING帮你注入的呢?你把你的spring.xml代码贴上我看看先

如何获取到C3P0连接池当前的状态
<hibernate-configuration>
    <session-factory>
        <!-- properties -->
        <property name="connection.username">xx</property>
    <!--     <property name="connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test;SelectMethod=cursor</property>  2000 -->
        <property name="connection.url">jdbc:oracle:thin:@192.168.1.10:1521:orcl</property>        
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="connection.password">xx</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="show_sql">true</property>

       <!-- 配置C3P0连接池属性 -->    
    <property name="hibernate.c3p0.max_size">500</property>  
    <property name="hibernate.c3p0.min_size">5</property>  
    <property name="hibernate.c3p0.timeout">6000</property>  
    <property name="hibernate.c3p0.max_statements">100</property>  
    <property name="hibernate.c3p0.idle_test_period">3000</property>  
    <!-- 当连接池耗尽并接到获得连接的请求,则新增加连接的数量 -->  
    <property name="hibernate.c3p0.acquire_increment">5</property>  
    <!-- 是否验证,检查连接 -->  
    <property name="hibernate.c3p0.validate">false</property>
    </session-factory>
</hibernate-configuration>
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.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 SessionUtil {
	/**
	 * Location of hibernate.cfg.xml file. NOTICE: Location should be on the
	 * classpath as Hibernate uses #resourceAsStream style lookup for its
	 * configuration file. That is place the config file in a Java package - the
	 * default location is the default Java package.<br>
	 * <br>
	 * Examples: <br>
	 * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
	 * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
	 */

	/** Holds a single instance of Session */
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

	/** The single instance of hibernate configuration */
	private static final Configuration cfg = new Configuration();

	/** The single instance of hibernate SessionFactory */
	private static net.sf.hibernate.SessionFactory sessionFactory;

	/**
	 * Returns the ThreadLocal Session instance. Lazy initialize the
	 * <code>SessionFactory</code> if needed.
	 * 
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session currentSession() throws HibernateException {
		Session session = (Session) threadLocal.get();

		if (session == null) {
			if (sessionFactory == null) {
				try {
					cfg.configure(ResourceUtil.getDatasourceName());//从配置文件获取参数
					sessionFactory = cfg.buildSessionFactory();
				} catch (Exception e){
					System.err.println("%%%% Error Creating SessionFactory %%%%" +e.getMessage());
					e.printStackTrace();
				}
			}
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}

		return session;
	}

	/**
	 * 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();
		}
	}

	/**
	 * Default constructor.
	 */
	private SessionUtil() {
	}

}

获取

	public  Connection getConn(){
		if(conn!=null){
			return conn;
		}
		try {
			conn = SessionUtil.currentSession().connection();
			// MyInit.getInstance();
		} catch (Exception e) {
			System.out.println("db.init:"+e.getMessage());
		}
		//System.out.println("getConn()->conn:"+conn);
        return conn;  
    }
如何获取到C3P0连接池当前的状态
5分
引用 12 楼 fulong258 的回复:
<hibernate-configuration>
    <session-factory>
        <!-- properties -->
        <property name="connection.username">xx</property>
    <!--     <property name="connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test;SelectMethod=cursor</property>  2000 -->
        <property name="connection.url">jdbc:oracle:thin:@192.168.1.10:1521:orcl</property>        
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="connection.password">xx</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="show_sql">true</property>

       <!-- 配置C3P0连接池属性 -->    
    <property name="hibernate.c3p0.max_size">500</property>  
    <property name="hibernate.c3p0.min_size">5</property>  
    <property name="hibernate.c3p0.timeout">6000</property>  
    <property name="hibernate.c3p0.max_statements">100</property>  
    <property name="hibernate.c3p0.idle_test_period">3000</property>  
    <!-- 当连接池耗尽并接到获得连接的请求,则新增加连接的数量 -->  
    <property name="hibernate.c3p0.acquire_increment">5</property>  
    <!-- 是否验证,检查连接 -->  
    <property name="hibernate.c3p0.validate">false</property>
    </session-factory>
</hibernate-configuration>
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.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 SessionUtil {
	/**
	 * Location of hibernate.cfg.xml file. NOTICE: Location should be on the
	 * classpath as Hibernate uses #resourceAsStream style lookup for its
	 * configuration file. That is place the config file in a Java package - the
	 * default location is the default Java package.<br>
	 * <br>
	 * Examples: <br>
	 * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
	 * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
	 */

	/** Holds a single instance of Session */
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

	/** The single instance of hibernate configuration */
	private static final Configuration cfg = new Configuration();

	/** The single instance of hibernate SessionFactory */
	private static net.sf.hibernate.SessionFactory sessionFactory;

	/**
	 * Returns the ThreadLocal Session instance. Lazy initialize the
	 * <code>SessionFactory</code> if needed.
	 * 
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session currentSession() throws HibernateException {
		Session session = (Session) threadLocal.get();

		if (session == null) {
			if (sessionFactory == null) {
				try {
					cfg.configure(ResourceUtil.getDatasourceName());//从配置文件获取参数
					sessionFactory = cfg.buildSessionFactory();
				} catch (Exception e){
					System.err.println("%%%% Error Creating SessionFactory %%%%" +e.getMessage());
					e.printStackTrace();
				}
			}
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}

		return session;
	}

	/**
	 * 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();
		}
	}

	/**
	 * Default constructor.
	 */
	private SessionUtil() {
	}

}

获取

	public  Connection getConn(){
		if(conn!=null){
			return conn;
		}
		try {
			conn = SessionUtil.currentSession().connection();
			// MyInit.getInstance();
		} catch (Exception e) {
			System.out.println("db.init:"+e.getMessage());
		}
		//System.out.println("getConn()->conn:"+conn);
        return conn;  
    }

直接NEW,因为是单例模式的.

如何获取到C3P0连接池当前的状态
引用

直接NEW,因为是单例模式的.

不懂,求解

如何获取到C3P0连接池当前的状态
30分
引用 14 楼 fulong258 的回复:
引用

直接NEW,因为是单例模式的.

不懂,求解

ComboPooledDataSource ds = new ComboPooledDataSource();
这样直接NEW出来就可以用啊。因为连接池是单例模式的,重新NEW的也是那个。
因为你没有用到SPING所以得自己NEW

如何获取到C3P0连接池当前的状态
引用 15 楼 dandan8989251 的回复:
Quote: 引用 14 楼 fulong258 的回复:
引用

直接NEW,因为是单例模式的.

不懂,求解

ComboPooledDataSource ds = new ComboPooledDataSource();
这样直接NEW出来就可以用啊。因为连接池是单例模式的,重新NEW的也是那个。
因为你没有用到SPING所以得自己NEW

NEW 然后如何获取 当前的连接数,空闲连接数,总连接数呢,我对洋文不是太懂

如何获取到C3P0连接池当前的状态
30分
        ComboPooledDataSource ds = new ComboPooledDataSource();
        System.out.println(ds.getMaxPoolSize());// 最大连接数
        System.out.println(ds.getMinPoolSize());// 最小连接数
        System.out.println(ds.getNumBusyConnections());// 正在使用连接数
        System.out.println(ds.getNumIdleConnections());// 空闲连接数
        System.out.println(ds.getNumConnections());// 总连接数
上面几行代码就是实现你的功能的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何获取到C3P0连接池当前的状态
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!