【12】C3P0连接池技术的实现

1. C3P0连接池jar文件导入

maven

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

2. 使用类实现C3P0连接池

//创建连接池对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//对池进行四大参数配置
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("root");
		
//池配置
//配置初始化连接
dataSource.setInitialPoolSize(20);
//配置增量
dataSource.setAcquireIncrement(5);
//配置最小连接数
dataSource.setMinPoolSize(3);
//配置最大连接数
dataSource.setMaxPoolSize(50);
		
//获取数据库连接
Connection conn = dataSource.getConnection();
//关闭连接,其实是把连接归还给池
conn.close();

3.使用配置文件实现C3P0连接池

使用配置文件实现C3P0的要求:

  • 文件名称必须为c3p0-config.xml
  • 文件位置必须在src下

3.1 默认配置

  • c3p0-config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<!-- 默认配置 -->
	<default-config>
		<!-- 连接四大参数配置 -->
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/xm</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<!-- 池参数配置 -->
		<property name="aoquireInorement">3</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">2</property>
		<property name="maxPoolSize">10</property>
	</default-config>
</c3p0-config>
  • 直接在java代码中创建数据连接池对象
//创建连接池对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//获取数据库连接
Connection conn = dataSource.getConnection();

3.2 使用命名配置

  • c3p0-config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- Oracle配置信息 -->
	<named-config name="oracle-config">
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/xm</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="aoquireInorement">3</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">2</property>
		<property name="maxPoolSize">10</property>
	</named-config>
</c3p0-config>
  • 在java代码中创建池对象
//构造器的参数为指定命名配置元素的名称
ComboPooledDataSource dataSource = 
new ComboPooledDataSource("oracle-config");
//获取数据库连接
Connection conn = dataSource.getConnection();

4.C3P0详细配置

<property name="driverClass" value="${c3p0.driverClass}"></property>  
<property name="jdbcUrl" value="${c3p0.url}"></property>  
<property name="user" value="${c3p0.user}"></property>  
<property name="password" value="${c3p0.password}"></property>
<property name="testConnectionOnCheckout" value="true" />
<property name="unreturnedConnectionTimeout" value="300" />
<property name="debugUnreturnedConnectionStackTraces" value="true" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 --> 
<property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
<!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" /> 
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 --> 
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
<!--连接池中保留的最大连接数。默认值: 15 --> 
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" /> 
<!-- 连接池中保留的最小连接数,默认为:3-->
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<!-- 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0 --> 
<property name="checkoutTimeout" value="3000" />
<!--重新尝试的时间间隔,默认为:1000毫秒--> 
<property name="acquireRetryDelay" value="1000" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次--> 
<property name="acquireRetryAttempts" value="60" />  
<!--关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务 --> 
<property name="autoCommitOnClose" value="false" />     
<!--如果为false,则获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常,但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认: false--> 
<property name="breakAfterAcquireFailure" value="false" />
<!--每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 --> 
<property name="idleConnectionTestPeriod" value="60" />
<!--c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0--> 
<property name="maxStatements" value="100" />
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。默认值: 0 --> 
<property name="maxStatementsPerConnection" value="0" />

猜你喜欢

转载自blog.csdn.net/Spectre_win/article/details/88667512