zoukankan      html  css  js  c++  java
  • Spring c3p0连接池配置

      数据库连接池
      数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。

      获取一个连接,系统要在背后做很多消耗资源的事情,大多时候,创建连接的时间比执行sql语句的时间还要长。
    用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长。

      数据库连接池负责分配,管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个。

      c3p0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。c3p0一般是与Hibernate,Spring等框架一块使用的,当然也可以单独使用。
      dbcp没有自动回收空闲连接的功能,c3p0有自动回收空闲连接功能。

    第一种方式:通过xml文件来配置

    <?xml version="1.0" encoding="UTF-8"?>
    
      <c3p0-config>
        <default-config>
            <property name="driverClass" >com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl" >jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK</property>
            <property name="user" >root</property>
            <property name="password" ></property>
            
            <property name="maxPoolSize" >15</property>
            <property name="minPoolSize" >5</property>
            <property name="initialPoolSize" >5</property>
        </default-config>
    </c3p0-config>  

    第二种方式:通过properties文件来配置

    c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydb  
    c3p0.driverClass=com.mysql.jdbc.Driver    
    c3p0.user=root    
    c3p0.password= 
        
    c3p0.acquireIncrement=3     
    c3p0.idleConnectionTestPeriod=60        
    c3p0.initialPoolSize=10     
    c3p0.maxIdleTime=60     
    c3p0.maxPoolSize=20     
    c3p0.maxStatements=100      
    c3p0.minPoolSize=5    

    调用main函数来读取配置

    public static void main(String[] args) throws Exception {
            
            
            
            ComboPooledDataSource cb = new ComboPooledDataSource();
            
            Connection conn=cb.getConnection();
            System.out.println(conn.isClosed());
            conn.close();
            
            
        }

    返回结果为false

    通过下面的实验,来验证一下连接池的作用:

    package com.test;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Calendar;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class Test {
    
        public static void main1111(String[] args) throws Exception {
            long start = Calendar.getInstance().getTimeInMillis();
            for(int i=0; i<100;i++){
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
            System.out.println(conn.isClosed());
            conn.close();
            }
            long end = Calendar.getInstance().getTimeInMillis();
            System.out.println(end-start);
        }
        public static void main(String[] args) throws Exception {
            
            long start = Calendar.getInstance().getTimeInMillis();
            
            ComboPooledDataSource cb = new ComboPooledDataSource();
            for(int i=0; i<100;i++){
            Connection conn=cb.getConnection();
            System.out.println(conn.isClosed());
            conn.close();
            }
            long end = Calendar.getInstance().getTimeInMillis();
            System.out.println(end-start);
        }
    
    }

      main1111的函数是采用最基本的方式连接数据库,main函数是用连接池连接数据库,同样循环一百次第一个结果为8000毫秒,第二个约为1000毫秒。

      既然可以通过xml文件和properties文件来配置连接池,那么我们也可以通过别的方式进行配置:

    (1)直接在main函数中定义

    public static void main11(String[] args) throws Exception {
            ComboPooledDataSource cd = new ComboPooledDataSource();
            cd.setDriverClass("com.mysql.jdbc.Driver");
            cd.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
            cd.setUser("root");
            cd.setPassword("");
            cd.setMinPoolSize(5);
            cd.setMaxPoolSize(10);
            Connection conn = cd.getConnection();
            System.out.println(conn.isClosed());
            conn.close();
    
        }

    (2)通过读取properties配置采用${}方式在xml中读取

    driverClass=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK
    user=root
    password=
    
    minPoolSize=5
    maxPoolSize=15
    initialPoolSize=5
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
        <context:property-placeholder location="classpath:db.properties"/>
        
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${driverClass}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            
            <property name="minPoolSize" value="${minPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
            <property name="initialPoolSize" value="${initialPoolSize}"></property>
        </bean>
        
    </beans>
    public static void main3333(String[] args) throws Exception {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            DataSource d = (DataSource)context.getBean("dataSource");
            Connection conn = d.getConnection();
            System.out.println(conn.isClosed());
            conn.close();
        }
  • 相关阅读:
    unity UGUI实现类似NGUI切换Sprite的方式
    商业智能系统在税务行业的应用
    MSRDS机器人仿真软件学习资源汇总
    Emotiv脑电设备与RDS机器人仿真初步测试
    unity使用UGUI创建摇杆
    如何利用FineBI做财务分析
    Android4.2.2源码目录结构分析
    一个前端妹子的悲欢编程之路
    推荐一款优雅高效的免费在线APP原型工具
    数据分析概述和理论基础
  • 原文地址:https://www.cnblogs.com/claricre/p/6665570.html
Copyright © 2011-2022 走看看