• 谈谈数据库连接池


    前言:

    最近又在为暑假的实习奔波...今天的面试被问到连接池有没有使用过,一时竟然哑口(简历上写的可以熟悉mysql~)。回来反思总结了一下,然后又看了20分钟网上视频。为防止下次面试又出糗,于是便有了这篇随笔~

     

    l 为什么使用数据库连接池:

      为了避免每次访问数据库的时候都需要重新建立新的连接而影响运行速度,在实际的项目中通常使用数据库连接池来统一调配,从而提高数据库的访问效率。

     

    l 原理示意图:

     

     

    l 下载解压

     

    l 用浏览器查看文档doc下的index.html文件

     

    l 常见2种连接方式

    (1)java文件实例化DataSource (详见后面实例代码)

    (2)src目录下创建c3p0-config.xml

     1 <c3p0-config>
     2 
     3   <named-config name="helloc3p0">
     4 
     5    <!-- 自己配置數本機據庫屬性 -->
     6 
     7    <property name="driverClass">com.mysql.jdbc.Driver</property>
     8 
     9    <property name="jdbcUrl">jdbc:mysql://localhost/youku</property>
    10 
    11    <property name="user">root</property>
    12 
    13    <property name="password">1234</property>
    14 
    15   
    16 
    17    <!-- 從index.html說明文檔裡面複製后自行修改即可 -->
    18 
    19     <property name="acquireIncrement">5</property>
    20 
    21     <property name="initialPoolSize">5</property>
    22 
    23     <property name="minPoolSize">5</property>
    24 
    25     <property name="maxPoolSize">10</property>
    26 
    27  
    28 
    29     <property name="maxStatements">2</property> 
    30 
    31     <property name="maxStatementsPerConnection">5</property>
    32 
    33  
    34 
    35   </named-config>
    36 
    37 </c3p0-config>

     

    l 封装调用(完整代码)

      1 package util;
      2 
      3  
      4 
      5 import java.sql.Connection;
      6 
      7 import java.sql.DriverManager;
      8 
      9 import java.sql.SQLException;
     10 
     11 import javax.sql.DataSource;
     12 
     13  
     14 
     15 import com.mchange.v2.c3p0.ComboPooledDataSource;
     16 
     17  
     18 
     19 public class JDBCUtil {
     20 
     21 Connection conn = null;
     22 
     23  
     24 
     25 public Connection getjdbcConnection() throws Exception {
     26 
     27 Class.forName("com.mysql.jdbc.Driver");
     28 
     29 String url = "jdbc:mysql://localhost:3306/youku";
     30 
     31 return DriverManager.getConnection(url, "root", "1234");
     32 
     33 }
     34 
     35 //因为使用连接池只需要进行一次初始化,因此声明为'static'类型
     36 
     37 private static DataSource datasource;
     38 
     39 static {
     40 
     41 datasource = new ComboPooledDataSource("helloc3p0");
     42 
     43 }
     44 
     45  
     46 
     47 public Connection getc3p0Connection() throws Exception {
     48 
     49 return datasource.getConnection();
     50 
     51 }
     52 
     53 //测试2种不同的连接方式jdbc 和 c3p0数据库连接池
     54 
     55 public void test() throws Exception {
     56 
     57 conn = this.getjdbcConnection();
     58 
     59 System.out.println("jdbc连接方式:" + conn.getMetaData());
     60 
     61 conn = this.getc3p0Connection();
     62 
     63 System.out.println("c3p0连接池方式:" + conn.getMetaData());
     64 
     65 }
     66 
     67  
     68 
     69 // method 1: 實例化c3p0數據源對象,進行屬性set設置
     70 
     71 public void c3p0() throws Exception {
     72 
     73 ComboPooledDataSource cpds = new ComboPooledDataSource();
     74 
     75 cpds.setDriverClass("com.mysql.jdbc.Driver"); // loads the jdbc driver
     76 
     77 cpds.setJdbcUrl("jdbc:mysql://localhost/youku");
     78 
     79 cpds.setUser("root");
     80 
     81 cpds.setPassword("1234");
     82 
     83 System.out.println(cpds.getDataSourceName());
     84 
     85 System.out.println("最大連接池數量:"+cpds.getMaxPoolSize());
     86 
     87  
     88 
     89 }
     90 
     91  
     92 
     93 // method 2:引用配置好的數據源
     94 
     95 public void c3p0_1() throws SQLException {
     96 
     97 // helloc3p0 對應 "c3p0-config.xml文件中的app-name"
     98 
     99 DataSource dataSource = new ComboPooledDataSource("helloc3p0");
    100 
    101 System.out.println(dataSource.getConnection());
    102 
    103  
    104 
    105 ComboPooledDataSource cp = (ComboPooledDataSource) dataSource;
    106 
    107 System.out.println("最大連接池數量:"+cp.getMaxPoolSize());
    108 
    109 }
    110 
    111  
    112 
    113 public static void main(String[] args) throws Exception {
    114 
    115  
    116 
    117 new JDBCUtil().test();
    118 
    119 new JDBCUtil().c3p0();
    120 
    121 new JDBCUtil().c3p0_1();
    122 
    123 }
    124 
    125  
    126 
    127 }

     

    测试结果:

     

    l 可能出现的问题:

    1)没有添加相关jar包,无法运行!

     

    2)xml文件没有创建在lib或者src目录下,可能出现报错:“...don’t exist...

     

    (hint:“c3p0-config.xml”这个文件名一般不改,否则也可能出现类似的错误!

     

    结语:

    数据库连接池也算是JDBC的一个知识点的小拓展,如果这点常识都不能掌握那可不能轻易说自己熟悉mysqlJDBC了。

  • 相关阅读:
    每天一个Linux命令(10)--cat命令
    每天一个Linux命令(09)--touch命令
    每天一个Linux命令(08)--cp命令
    每天一个Linux命令(07)--mv命令
    CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)
    iptables配置服务器端口转发
    AD管理命令
    win7登入使用的是临时档案解决方法
    修复Dll文件
    iptables 用法
  • 原文地址:https://www.cnblogs.com/SeaSky0606/p/4585567.html
Copyright © 2020-2023  润新知