|
在配置的时候,可以配置连接池的各个属性,如最大连接数,最小等等值,但是在运行的时候,如何能够得到当前运行时的一些状态信息呢,如当前的连接数,空闲连接数,总连接数呢? 坐等大神回答! |
|
![]() 10分 |
datasource是一个bean,那就可以获取,获取后可以先获取连接,然后用getMetaData里获取各种信息。
|
![]() |
我是通过 取到的链接,那如何获取到datasource呢? |
![]() |
自己去看如何获取bean。
|
![]() |
官方文档上好象有说明,有一个网页什么的可以直接看到
|
![]() |
官方文档上好象有说明,在网页上就可以直接看到
|
![]() |
你说的是获取的是DatabaseMetaData吧 DatabaseMetaData dbmd = (DatabaseMetaData)conn.getMetaData(); 这不是我想要的!! 我要的是获取到当前连接池的状态,如连接池当前的连接数,空闲连接数等等。 |
![]() 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 |
![]() 10分 |
看了楼主的回答,我估计楼是使用SPING + HIBERNATE吧,你在SRPING里配置好ComboPooledDataSource的BEAN让SPRING帮你注入就行了.你直接拿来用
|
![]() |
Connection conn; //获取到的链接 如何转换成ComboPooledDataSource |
![]() |
如何拿,求指教,求鞭打,求调教 |
![]() 5分 |
你如何拿到的 Connection conn; 是不是用SPING帮你注入的呢?你把你的spring.xml代码贴上我看看先 |
![]() |
<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;
}
|
![]() 5分 |
<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> 直接NEW,因为是单例模式的. |
![]() |
不懂,求解 |
![]() 30分 |
ComboPooledDataSource ds = new ComboPooledDataSource(); |
![]() |
NEW 然后如何获取 当前的连接数,空闲连接数,总连接数呢,我对洋文不是太懂 |
![]() 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());// 总连接数 上面几行代码就是实现你的功能的 |

