码迷,mamicode.com
首页 > 数据库 > 详细

数据库连接池

时间:2017-10-06 23:54:30      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:参数   https   tin   bcp   方法   driver   调用   关闭   名称   

  用池来管理Connection,这可以重复使用Connection。有了池,所有我们就不用自己来创建Connection,而是通过吃来获取Connection对象。当使用完Connection后,调用Connection的close()方法也不会真的关闭Connection,而是把Connection"归还"给池。池就可以在利用这个Connection对象了。

1、池参数(所有池参数都有默认值):

  • 初始大小:10个;
  • 最小空闲连接数:3个;
  • 增量:一次创建的最小单位(5个);
  • 最大空闲连接数:12个;
  • 最大连接数:20个;
  • 最大的等待时间:1000毫秒。

2、四大连接参数

  连接池也是使用四大连接参数来完成创建连接对象。

3、实现的接口

连接池必须实现:javax.sql.DataSource接口。

连接池返回的Connection对象,它的close()方法与众不同,调用它的close()不是关闭,而是把连接归还给池。

4、DBCP连接池示例:

 1 package cn.itcast.jdbc;
 2 
 3 import org.apache.commons.dbcp2.BasicDataSource;
 4 import org.junit.Test;
 5 import java.sql.Connection;
 6 import java.sql.SQLException;
 7 
 8 /**
 9  * DBCP连接池
10  */
11 public class Demo1 {
12     @Test
13     public void fun1() throws SQLException{
14         /**
15          * 1、创建连接池
16          * 2、配置四大参数
17          * 3、配置池参数
18          * 4、得到连接对象
19          */
20         BasicDataSource dataSource = new BasicDataSource();
21         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
22         dataSource.setUrl("jdbc:mysql://localhost:3306/mydb1");
23         dataSource.setUsername("root");
24         dataSource.setPassword("");
25 
26         dataSource.setMaxTotal(20);
27         dataSource.setMaxIdle(3);
28         dataSource.setMaxWaitMillis(1000);
29 
30         Connection con = dataSource.getConnection();
31         System.out.println(con.getClass().getName());//结果:org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
32 /* 
33 * 连接池内部使用四大参数创建了连接对象,即mysql驱动提供的Connection
34 * 连接池使用mysql的连接对象进行了装饰,只对close()方法进行了增强;
35 * 装饰之后的Connection的close()方法,用来把当前连接归还给池;
36 * */
37      con.close();//把连接归还给池;
38 }
39 }

 5、C3P0

①C3P0是开源免费的连接池。

②C3P0的使用

C3P0中池类是:CombopooledDataSource。

相关jar包下载链接:https://sourceforge.net/projects/c3p0/files/latest/download?source=files

导入c3p0-0.9.5.2.jar 和mchange-commons-java-0.2.11.jar包、mysql-connector-java-5.1.44-bin.jar(针对MySQL数据库)

③c3p0也可以指定配置文件,而配置文件可以是properties,也可以是xml的,当然xml高级一些,但是c3p0的配置文件名必须为“c3p0-config.xml”,并放在类的路径下(src下)。

④示例:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <c3p0-config>
 3     <!--默认配置信息-->
 4     <default-config>
 5         <!--连接四大参数配置-->
 6         <property name="droverClass">com.mysql.jdbc.Driver</property>
 7         <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
 8         <property name="user">root</property>
 9         <property name="password"></property>
10         <!--池参数配置-->
11         <property name="acquireIncrement">3</property>
12         <property name="initialPoolSize">10</property>
13         <property name="minPoolSize">2</property>
14         <property name="maxPoolSize">10</property>
15     </default-config>
16     <!-- 专门为oracle提供的配置信息 -->
17     <named-config name="oracle-config">
18         <!--连接四大参数配置-->
19         <property name="droverClass">oracle.jdbc.driver.OracleDriver</property>
20         <property name="jdbcUrl">jdbc:oracle:thin:@地址:端口:ORCL</property>
21         <property name="user">root</property>
22         <property name="password"></property>
23         <!--池参数配置-->
24         <property name="acquireIncrement">3</property>
25         <property name="initialPoolSize">10</property>
26         <property name="minPoolSize">2</property>
27         <property name="maxPoolSize">10</property>
28     </named-config>
29 </c3p0-config>
 1 import com.mchange.v2.c3p0.ComboPooledDataSource;
 2 import org.junit.Test;
 3 import java.beans.PropertyVetoException;
 4 import java.sql.Connection;
 5 import java.sql.SQLException;
 6 
 7 /**
 8  * c3p0
 9  */
10 public class Demo1 {
11     /*
12     * 手动配置
13     * */
14     @Test
15     public void fun1() throws PropertyVetoException,SQLException {
16         //创建连接池对象
17         ComboPooledDataSource dataSource = new ComboPooledDataSource();
18         //对池进行四大参数的配置
19         dataSource.setDriverClass("com.mysql.jdbc.Driver");
20         dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");
21         dataSource.setUser("root");
22         dataSource.setPassword("");
23 
24         //池配置
25         dataSource.setAcquireIncrement(5);
26         dataSource.setInitialPoolSize(20);
27         dataSource.setMinPoolSize(2);
28         dataSource.setMaxPoolSize(50);
29 
30         Connection con = dataSource.getConnection();
31         System.out.println(con);
32         con.close();
33     }
34     /*
35     * 配置文件的默认配置
36     * */
37     @Test
38     public void fun2() throws SQLException{
39         /*
40         * 在创建连接池对象时,这个对象就会自动加载配置文件,不用我们来指定。
41         * */
42         ComboPooledDataSource dataSource = new ComboPooledDataSource();
43         Connection con = dataSource.getConnection();
44         System.out.println(con);
45         con.close();
46     }
47     /*
48     * 使用命名配置信息
49     * */
50     @Test
51     public void fun3() throws SQLException{
52         /*
53         * 构造器的参数指定命名配置元素的名称
54         * */
55         ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle-config");
56         Connection con = dataSource.getConnection();
57         System.out.println(con);
58         con.close();
59     }
60 }

 

数据库连接池

标签:参数   https   tin   bcp   方法   driver   调用   关闭   名称   

原文地址:http://www.cnblogs.com/gdwkong/p/7633019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!