【JDBC】3.c3p0连接池

c3p0连接池,是Hibernate推荐使用的连接池,该连接池已有接近10年没有更新了.

 c3p0性能很高,其实是在测试环境高,真实应用中很低.

Java框架数据库连接池比较(c3p0,dbcp和proxool)

准备:

1):拷贝jar:c3p0-0.9.1.2.jar

2):build path,添加到classpath路径.

3):阅读文档:c3p0-0.9.1.2/doc/index.html#quickstart.

4):把c3p0的连接信息存放到配置文件中.

c3p0配置文件使用注意:

  1):配置文件名称,必须叫做:c3p0.properties或者c3p0-config.xml.

  2):c3p0.properties文件必须存放与classpath的根路径.

  3):要求c3p0.properties文件中所有的key必须以c3p0.作为前缀,如:c3p0.jdbcUrl.

  4):要求c3p0.properties文件中的key,必须是ComboPooledDataSource对象中的属性名.

c3p0.driverClass=com.mysql.jdbc.Driver

c3p0.jdbcUrl=jdbc:mysql:///jdbcdemo

c3p0.user=root

c3p0.password=root

c3p0.maxPoolSize=10

 

public class c3p0Util {

    private c3p0Util() {
        super();
        // TODO Auto-generated constructor stub
    }

    private static DataSource  dataSource = null;
    
    static{
        try {
            DataSource load = DataSources.unpooledDataSource();
            dataSource = DataSources.pooledDataSource(load);
            //DataSource loadData = DataSources.
            
        } catch (Exception e) {
            // TODO: handle exception
        }
        
    }

    public static Connection getConn() {
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

 

posted @ 2017-04-19 18:01  Qingyun_Qearl  阅读(162)  评论(0)    收藏  举报