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

解决方案 »

  1.   


    我是通过
    net.sf.hibernate.SessionFactory sessionFactory
    sessionFactory.openSession()
    currentSession().connection()取到的链接,那如何获取到datasource呢?
      

  2.   


    你说的是获取的是DatabaseMetaData吧DatabaseMetaData  dbmd  = (DatabaseMetaData)conn.getMetaData();这不是我想要的!!!!!!!!!我要的是获取到当前连接池的状态,如连接池当前的连接数,空闲连接数等等。
      

  3.   

    我不知道你的环境是什么.所以只贴部分代码:
    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,网上很多你就自行百度吧
      

  4.   

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

  5.   


     Connection conn; //获取到的链接
    DatabaseMetaData  dbmd  = (DatabaseMetaData)conn.getMetaData();如何转换成ComboPooledDataSource
      

  6.   


     Connection conn; //获取到的链接
    DatabaseMetaData  dbmd  = (DatabaseMetaData)conn.getMetaData();如何转换成ComboPooledDataSource
    你如何拿到的 Connection conn; 是不是用SPING帮你注入的呢?你把你的spring.xml代码贴上我看看先
      

  7.   


    <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;  
        }
      

  8.   

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

  9.   

    不懂,求解
    ComboPooledDataSource ds = new ComboPooledDataSource();
    这样直接NEW出来就可以用啊。因为连接池是单例模式的,重新NEW的也是那个。
    因为你没有用到SPING所以得自己NEWNEW 然后如何获取 当前的连接数,空闲连接数,总连接数呢,我对洋文不是太懂
      

  10.   

            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());// 总连接数
    上面几行代码就是实现你的功能的