Maven下C3P0连接池配置及使用

2022-11-17 14:30:32 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

1.在pom.xml文件中导入jar包同时不要忘记jdbc包,否则会报错

代码语言:javascript复制
<dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.17</version>
    </dependency>

2.在src目录下创建c3p0-config.xml文件

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--使用默认的配置读取连接池对象-->
<default-config>
<!--连接参数-->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai</property>
<property name="user">root</property>
<property name="password">root</property>
<!--连接池参数-->
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--超时时间-->
<property name="checkoutTimeout">3000</property>
<property name="maxIdleTime">30</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="mysql">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
<named-config name="oracle">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>

3.写一个类测试下

代码语言:javascript复制
package c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Demo1 { 

public static void main(String[] args) throws SQLException { 

//1.创建数据库连接池对象
DataSource ds = new ComboPooledDataSource();
//2.获取连接对象
Connection conn = ds.getConnection();
//3.打印
System.out.println(conn);
}
}

输出下面语句表示成功!其中红色的不是报错,而是日志

4.访问其他配置文件

代码语言:javascript复制
package c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Demo2 { 

public static void main(String[] args) throws SQLException { 

//1.获取DataSource 即连接池对象,使用默认配置
DataSource ds = new ComboPooledDataSource();
//2.获取连接对象
/*for (int i = 1; i <=11 ; i  ) {//报错,因为池子最大连接数为10 Connection conn = ds.getConnection(); System.out.println(i ":" conn); }*/
for (int i = 1; i <=11 ; i  ) { 
//正确,因为归还了
Connection conn = ds.getConnection();
System.out.println(i ":" conn);
if (i==5){ 

conn.close();//归还连接到连接池
}
}
System.out.println("----------");
test();
}
public static void test() throws SQLException { 

//1.1获取DataSource,使用指定名称配置
DataSource ds = new ComboPooledDataSource("mysql");
for (int i = 1; i <=60 ; i  ) { 
//不报错,因为新配置的最大连接数很大
Connection conn = ds.getConnection();
System.out.println(i ":" conn);
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/234213.html原文链接:https://javaforall.cn

0 人点赞