开源的连接池技术DBCP与c3p0

3.3核心类

public class App_DBCP {

 

// 1. 硬编码方式实现连接池

@Test

public void testDbcp() throws Exception {

// DBCP连接池核心类

BasicDataSource dataSouce = new BasicDataSource();

// 连接池参数配置:初始化连接数、最大连接数 / 连接字符串、驱动、用户、密码

dataSouce.setUrl(“jdbc:mysql:///jdbc_demo”);//数据库连接字符串

dataSouce.setDriverClassName(“com.mysql.jdbc.Driver”);  //数据库驱动

dataSouce.setUsername(“root”);//数据库连接用户

dataSouce.setPassword(“root”); //数据库连接密码

dataSouce.setInitialSize(3);  // 初始化连接

dataSouce.setMaxActive(6);  // 最大连接

dataSouce.setMaxIdle(3000);   // 最大空闲时间

// 获取连接

Connection con = dataSouce.getConnection();

con.prepareStatement(“delete from admin where id=3”).executeUpdate();

// 关闭

con.close();

}

@Test

// 2. 【推荐】配置方式实现连接池  ,  便于维护

public void testProp() throws Exception {

// 加载prop配置文件

Properties prop = new Properties();

// 获取文件流

InputStream inStream = App_DBCP.class.getResourceAsStream(“db.properties”);

// 加载属性配置文件

prop.load(inStream);

// 根据prop配置,直接创建数据源对象

DataSource dataSouce = BasicDataSourceFactory.createDataSource(prop);

// 获取连接

Connection con = dataSouce.getConnection();

con.prepareStatement(“delete from admin where id=4”).executeUpdate();

// 关闭

con.close();

}

}

配置方式实现DBCP连接池,  配置文件中的key与BaseDataSouce中的属性一样:

db.properties

url=jdbc:mysql:///jdbc_demo

driverClassName=com.mysql.jdbc.Driver

username=root

password=root

initialSize=3

maxActive=6

maxIdle=3000

 

 

 

四、C3P0连接池:

4.1C3P0连接池介绍:

2. public

 class App {

 

@Test

//1. 硬编码方式,使用C3P0连接池管理连接

public void testCode() throws Exception {

// 创建连接池核心工具类

ComboPooledDataSource dataSource = new ComboPooledDataSource();

// 设置连接参数:url、驱动、用户密码、初始连接数、最大连接数

dataSource.setJdbcUrl(“jdbc:mysql://localhost:3306/jdbc_demo”);

dataSource.setDriverClass(“com.mysql.jdbc.Driver”);

dataSource.setUser(“root”);

dataSource.setPassword(“root”);

dataSource.setInitialPoolSize(3);

dataSource.setMaxPoolSize(6);

dataSource.setMaxIdleTime(1000);

// —> 从连接池对象中,获取连接对象

Connection con = dataSource.getConnection();

// 执行更新

con.prepareStatement(“delete from admin where id=7”).executeUpdate();

// 关闭

con.close();

}

@Test

//2. XML配置方式,使用C3P0连接池管理连接

public void testXML() throws Exception {

// 创建c3p0连接池核心工具类

// 自动加载src下c3p0的配置文件【c3p0-config.xml】

ComboPooledDataSource dataSource = new ComboPooledDataSource();// 使用默认的配置

// 获取连接

Connection con = dataSource.getConnection();

// 执行更新

con.prepareStatement(“delete from admin where id=5”).executeUpdate();

// 关闭

con.close();

}

}

c3p0-config.xml配置

<c3p0-config>

<default-config>

<property name=”jdbcUrl”>jdbc:mysql://localhost:3306/jdbc_demo

</property>

<property name=”driverClass”>com.mysql.jdbc.Driver</property>

<property name=”user”>root</property>

<property name=”password”>root</property>

<property name=”initialPoolSize”>3</property>

<property name=”maxPoolSize”>6</property>

<property name=”maxIdleTime”>1000</property>

</default-config>

 

 

<named-config name=”oracle_config”>

<property name=”jdbcUrl”>jdbc:mysql://localhost:3306/jdbc_demo</property>

<property name=”driverClass”>com.mysql.jdbc.Driver</property>

<property name=”user”>root</property>

<property name=”password”>root</property>

<property name=”initialPoolSize”>3</property>

<property name=”maxPoolSize”>6</property>

<property name=”maxIdleTime”>1000</property>

</named-config>

</c3p0-config>

 

 

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年7月9日
下一篇 2017年7月9日

相关推荐