biGpython

生亦何欢,死亦何苦? 予我長袖,我必善舞!

导航

BoneCP与C3P0比较 BoneCP不能自动重连


BoneCP基本使用

package cn.redcdn.sc.server.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

public class DbUtil {
    private static BoneCP  bone;
    static{
        Properties props = new Properties();
        try {
            props.load(DbUtil.class.getClassLoader().getResourceAsStream("dbcp.properties"));
            
            String driverName     = props.getProperty("driverClassName");
            String jdbcUrl        = props.getProperty("jdbcUrl");
            String userName       = props.getProperty("username");
            String password       = props.getProperty("password");
            int partitionCount    = Integer.parseInt(props.getProperty("partitionCount"));
//          int idleMaxAge     = Integer.parseInt(props.getProperty("idleMaxAge"));
//          int acquireIncrement   = Integer.parseInt(props.getProperty("acquireIncrement"));
//          int releaseHelperThreads = Integer.parseInt(props.getProperty("releaseHelperThreads"));
//          int maxConnectionsPerPartition= Integer.parseInt(props.getProperty("maxConnectionsPerPartition"));
//          int minConnectionsPerPartition= Integer.parseInt(props.getProperty("minConnectionsPerPartition"));
//          int idleConnectionTestPeriod= Integer.parseInt(props.getProperty("idleConnectionTestPeriod"));
            
            Class.forName(driverName);
            BoneCPConfig config = new BoneCPConfig();
            
            config.setJdbcUrl                        (jdbcUrl);
            config.setUsername                        (userName);
            config.setPassword                        (password);
//          config.setIdleMaxAge                    (idleMaxAge);
            config.setPartitionCount                (partitionCount);
        //  config.setAcquireIncrement                (acquireIncrement);
//          config.setReleaseHelperThreads            (releaseHelperThreads);
//          config.setMaxConnectionsPerPartition    (maxConnectionsPerPartition);
//          config.setMinConnectionsPerPartition    (minConnectionsPerPartition);
//          config.setIdleConnectionTestPeriod        (idleConnectionTestPeriod);
            
            bone = new BoneCP(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static Connection conn;

    public static Connection getConnection() throws SQLException{       
        if(conn == null){
            conn = bone.getConnection();
        }
        return conn;                                                    
    }                                                                   
    public static void closeConnection() throws SQLException{           
        if(conn != null){
            conn.close();
            conn = null;
        }
        
    }                                                                   
    public static void main(String[] args) {
        
/*        try {
            System.out.println(DbUtil.getConnection());
            DbUtil.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
    }
}

 

C3p0基本使用
一个DataSource对象 ComboPooledDataSource

posted on 2012-05-11 10:47  biGpython  阅读(1869)  评论(0编辑  收藏  举报