java连接池


1)两种开源的数据库连接池:
•  JDBC 的数据库连接池使用 javax.sql.DataSource 来表示,DataSource 只是一个接口,
    该接口通常由服务器(Weblogic, WebSphere, Tomcat)提供实现,也有一些开源组织提供实现:
    – DBCP 数据库连接池
    – C3P0 数据库连接池
•  DataSource 通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把 DataSource 称为连接池
A:DBCP 数据源 :
•  DBCP 是 Apache 软件基金组织下的开源连接池实现,该连接池依赖该组织下的另一个开源系统:Common-pool. 如需使用该连接池实现,应在系统中增加如下两个 jar 文件:
Commons-dbcp.jar:连接池的实现。
Commons-pool.jar:连接池实现的依赖库。
•   Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
DBCP 数据源使用范例:
•  数据源和数据库连接不同,数据源无需创建多个,它是产生数据库连接的工厂,因此整个应用只需要一个数据源即可。
•  当数据库访问结束后,程序还是像以前一样关闭数据库连接:conn.close(); 但上面的代码并没有关闭数据库的物理连接,它仅仅把数据库连接释放,归还给了数据库连接池。

/**
     * 1. 加载 dbcp 的 properties 配置文件: 配置文件中的键需要来自 BasicDataSource
     * 的属性.
     * 2. 调用 BasicDataSourceFactory 的 createDataSource 方法创建 DataSource
     * 实例
     * 3. 从 DataSource 实例中获取数据库连接. 
     */
    @Test
    public void testDBCPWithDataSourceFactory() throws Exception{
        
        Properties properties = new Properties();
        InputStream inStream = JDBCTest.class.getClassLoader()
                .getResourceAsStream("dbcp.properties");
        properties.load(inStream);
        
        DataSource dataSource = 
                BasicDataSourceFactory.createDataSource(properties);
        
        System.out.println(dataSource.getConnection()); 
        
//        BasicDataSource basicDataSource = 
//                (BasicDataSource) dataSource;
//        
//        System.out.println(basicDataSource.getMaxWait()); 
    }
    
    /**
     * 使用 DBCP 数据库连接池
     * 1. 加入 jar 包(2 个jar 包). 依赖于 Commons Pool
     * 2. 创建数据库连接池
     * 3. 为数据源实例指定必须的属性
     * 4. 从数据源中获取数据库连接
     * @throws SQLException 
     */
    @Test
    public void testDBCP() throws SQLException{
        final BasicDataSource dataSource = new BasicDataSource();
        
        //2. 为数据源实例指定必须的属性
        dataSource.setUsername("root");
        dataSource.setPassword("1230");
        dataSource.setUrl("jdbc:mysql:///atguigu");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        
        //3. 指定数据源的一些可选的属性.
        //1). 指定数据库连接池中初始化连接数的个数
        dataSource.setInitialSize(5);
        
        //2). 指定最大的连接数: 同一时刻可以同时向数据库申请的连接数
        dataSource.setMaxActive(5);
        
        //3). 指定小连接数: 在数据库连接池中保存的最少的空闲连接的数量 
        dataSource.setMinIdle(2);
        
        //4).等待数据库连接池分配连接的最长时间. 单位为毫秒. 超出该时间将抛出异常. 
        dataSource.setMaxWait(1000 * 5);
        
        //4. 从数据源中获取数据库连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection.getClass()); 
        
        connection = dataSource.getConnection();
        System.out.println(connection.getClass()); 
        
        connection = dataSource.getConnection();
        System.out.println(connection.getClass()); 
        
        connection = dataSource.getConnection();
        System.out.println(connection.getClass()); 
        
        Connection connection2 = dataSource.getConnection();
        System.out.println(">" + connection2.getClass()); 
        
        new Thread(){
            public void run() {
                Connection conn;
                try {
                    conn = dataSource.getConnection();
                    System.out.println(conn.getClass()); 
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            };
        }.start();
        
        try {
            Thread.sleep(5500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        connection2.close();
    }

DBCP数据源例子


username=root
password=1230
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///atguigu

initialSize=10
maxActive=50
minIdle=5
maxWait=5000

dbcp.properties



B:C3P0 数据源
<span style="color:#000000;">/**
     * 1. 创建 c3p0-config.xml 文件, 
     * 参考帮助文档中 Appendix B: Configuation Files 的内容
     * 2. 创建 ComboPooledDataSource 实例;
     * DataSource dataSource = 
     *            new ComboPooledDataSource("helloc3p0");  
     * 3. 从 DataSource 实例中获取数据库连接. 
     */
    @Test
    public void testC3poWithConfigFile() throws Exception{
        DataSource dataSource = 
                new ComboPooledDataSource("helloc3p0");  
        
        System.out.println(dataSource.getConnection()); 
        
        ComboPooledDataSource comboPooledDataSource = 
                (ComboPooledDataSource) dataSource;
        System.out.println(comboPooledDataSource.getMaxStatements()); 
    }
    
    @Test
    public void testC3P0() throws Exception{
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver            
        cpds.setJdbcUrl( "jdbc:mysql:///atguigu" );
        cpds.setUser("root");                                  
        cpds.setPassword("1230");   
        
        System.out.println(cpds.getConnection()); 
    }

c3p0数据源例子</span>



<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

    <named-config name="helloc3p0">
        
        <!-- 指定连接数据源的基本属性 -->
        <property name="user">root</property>
        <property name="password">1230</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///atguigu</property>
        
        <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
        <property name="acquireIncrement">5</property>
        <!-- 初始化数据库连接池时连接的数量 -->
        <property name="initialPoolSize">5</property>
        <!-- 数据库连接池中的最小的数据库连接数 -->
        <property name="minPoolSize">5</property>
        <!-- 数据库连接池中的最大的数据库连接数 -->
        <property name="maxPoolSize">10</property>

        <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
        <property name="maxStatements">20</property>
        <!-- 每个连接同时可以使用的 Statement 对象的个数 -->
        <property name="maxStatementsPerConnection">5</property>
    
    </named-config>
        
</c3p0-config>

c3p0-config.xml



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值