数据库连接池

连接池
因为获取连接和释放连接是很耗费系统资源的,所以用连接池来代替。
Java为数据库连接池提供了公共的接口:javax.sql.DataSource,各个厂商需要让自己的连接池实现这个接口。这样应用程序可以方便的切换不同厂商的连接池!
常见的连接池: DBCP、Druid、C3P0。
Druid的概述
druid是阿里下面的一个开源的连接池产品,使用简单,而且可以和spring框架进行整合
Druid的使用
@Test
    /**


     * Druid 的使用 :


     * * 手动设置参数的方式


     */


    public void demo1(){


        Connection conn = null ;


        PreparedStatement pstmt = null ;


        ResultSet rs = null ;


        try {


           // 使用连接池:


           DruidDataSource dataSource
= new DruidDataSource();


           // 手动设置数据库连接的参数 :


            dataSource .setDriverClassName( "com.mysql.jdbc.Driver" );


           dataSource .setUrl( "jdbc:mysql:///test4" );


           dataSource .setUsername( "root" );


           dataSource .setPassword( "abc" );


           // 获得连接:


//         conn = JDBCUtils.getConnection();


           conn = dataSource .getConnection();


           // 编写 SQL:


           String sql = "select
* from account" ;


           // 预编译 SQL:


           pstmt = conn .prepareStatement( sql );


           // 设置参数 :


           // 执行 SQL:


           rs = pstmt .executeQuery();


           while ( rs .next()){


               System. out .println( rs .getInt( "id" )+ "
" + rs .getString( "name" )+ "
" + rs .getDouble( "money" ));


           }


        } catch (Exception e ){


           e .printStackTrace();


        } finally {


           JDBCUtils. release ( rs , pstmt , conn );


        }


    }
配置文件的方式
@Test


    /**


     * Druid 的使用 :


     * * 配置方式设置参数


     * Druid 配置方式可以使用属性文件配置的。


     * * 文件名称没有规定但是属性文件中的 key 要一定的。


     */


    public void demo2 (){


        Connection conn = null ;


        PreparedStatement pstmt = null ;


        ResultSet rs = null ;


        try {


           // 使用连接池:


           // 从属性文件中获取:


           Properties properties = new Properties();


           properties .load( new
FileInputStream( "src/druid.properties" ));


           DataSource dataSource = DruidDataSourceFactory. createDataSource ( properties );


           // 获得连接:


//         conn = JDBCUtils.getConnection();


           conn = dataSource .getConnection();


           // 编写 SQL:


           String sql = "select
* from account" ;


           // 预编译 SQL:


           pstmt = conn .prepareStatement( sql );


           // 设置参数 :


           // 执行 SQL:


           rs = pstmt .executeQuery();


           while ( rs .next()){


               System. out .println( rs .getInt( "id" )+ "
" + rs .getString( "name" )+ "
" + rs .getDouble( "money" ));


           }


        } catch (Exception e ){


           e .printStackTrace();


        } finally {


           JDBCUtils. release ( rs , pstmt , conn );


        }


    }

C3p0
使用方式








@Test


public void demo01() throws Exception{


   


    //1 获得连接池 ( 数据源 )


    ComboPooledDataSource
dataSource = new ComboPooledDataSource();


    //1.1 设置基本项


    dataSource.setDriverClass("com.mysql.jdbc.Driver");


   dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/webdb_4");


    dataSource.setUser("root");


    dataSource.setPassword("root");


    //1.2 其他项


    // * 初始化连接池中连接的个数


    dataSource.setInitialPoolSize(5);


    // * 最小 | 最大
连接池中连接的个数


    dataSource.setMinPoolSize(2);


    dataSource.setMaxPoolSize(10);


    // * 最大空闲数


    dataSource.setMaxIdleTime(60);


    // * 每次增长个数


    dataSource.setAcquireIncrement(2);


   


   


    //2 获得连接


    Connection
conn = dataSource.getConnection();


    System.out.println(conn);


}






*�Ҷ��[
配置文件
配置文件名称:c3p0-config.xml(固定的)
配置文件位置:src(类路径)
配置文件内容:命名配置
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///lyc</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="minPoolSize">5</property>
<property name="initialPoolSize">5</property>
</default-config>
<!-- 命名的配置 -->
<named-config name="oracle">
<!-- 连接数据库的4项基本参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/webdb</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="acquireIncrement">5</property>
<!-- 初始化连接数 -->
<property name="initialPoolSize">20</property>
<!-- 最小连接受 -->
<property name="minPoolSize">10</property>
<!-- 最大连接数 -->
<property name="maxPoolSize">40</property>
<!-- -JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量 -->
<property name="maxStatements">0</property>
<!-- 连接池内单个连接所拥有的最大缓存statements数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>

分类
属性
描述
必须项
User
用户名
password
密码
driverClass
驱动
mysql 驱动, com.mysql.jdbc.Driver
jdbcUrl
路径
mysql 路径, jdbc:mysql://localhost:3306/ 数据库
基本配置
acquireIncrement
连接池无空闲连接可用时,一次性创建的新连接数
默认值: 3
initialPoolSize
连接池初始化时创建的连接数
默认值: 3
maxPoolSize
连接池中拥有的最大连接数
默认值: 15
minPoolSize
连接池保持的最小连接数。
maxIdleTime
连接的最大空闲时间。如果超过这个时间,某个数据库连接还没有被使用,则会断开掉这个连接,如果为 0 ,则永远不会断开连接。
默认值: 0
管理连接池的大小和连接的生存时间(扩展)
maxConnectionAge
配置连接的生存时间,超过这个时间的连接将由连接池自动断开丢弃掉。当然正在使用的连接不会马上断开,而是等待它 close 再断开。配置为 0 的时候则不会对连接的生存时间进行限制。默认值 0
maxIdleTimeExcessConnections
这个配置主要是为了减轻连接池的负载,配置不为 0 ,则会将连接池中的连接数量保持到 minPoolSize ,为 0 则不处理。
配置 Prepared
Statement 缓存(扩展)
maxStatements
连接池为数据源缓存的 PreparedStatement 的总数。由于 PreparedStatement 属于单个 Connection, 所以这个数量应该根据应用中平均连接数乘以每个连接的平均 PreparedStatement 来计算。为 0 的时候不缓存,同时 maxStatementsPerConnection 的配置无效。
maxStatementsPerConnection
连接池为数据源单个 Connection 缓存的 PreparedStatement 数,这个配置比 maxStatements 更有意义,因为它缓存的服务对象是单个数据连接,如果设置的好,肯定是可以提高性能的。为 0 的时候不缓存。
c3p0的工具类
C3P0提供核心工具类:ComboPooledDataSource,如果要使用连接池,必须创建该类的实例对象。
 new ComboPooledDataSource(“名称”); 使用配置文件“命名配置”
 <named-config name="oracle">
 new ComboPooledDataSource(); 使用配置文件“默认配置”
 <default-config>
public class C3P0Utils{
//使用默认配置
// private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
//使用命名配置
private static ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle");
/**
* 获得数据源(连接池)
* @return
*/
public static DataSource getDataSource(){
return dataSource;
}

/**
* 获得连接
* @return
* @throws SQLException
*/
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值