c3p0的几种使用方式(原文地址: https://my.oschina.net/liangtee/blog/101047)

时间:2022-09-19 23:22:59
package com.c3p0.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBPool {
private static DBPool dbPool;
private ComboPooledDataSource dataSource;

static {
dbPool
= new DBPool();
}

public DBPool() {
try {
dataSource
= new ComboPooledDataSource();
dataSource.setUser(
"id");
dataSource.setPassword(
"pw");
dataSource.setJdbcUrl(
"jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
dataSource.setDriverClass(
"com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(
2);
dataSource.setMinPoolSize(
1);
dataSource.setMaxPoolSize(
10);
dataSource.setMaxStatements(
50);
dataSource.setMaxIdleTime(
60);
}
catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}

public final static DBPool getInstance() {
return dbPool;
}

public final Connection getConnection() {
try {
return dataSource.getConnection();
}
catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接", e);
}
}

public static void main(String[] args) throws SQLException {
Connection con
= null;
try {
con
= DBPool.getInstance().getConnection();
}
catch (Exception e) {
}
finally {
if (con != null)
con.close();
}
}

}

以上是直接在combopooleddatasource里面直接设置属性值

 

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">test</property>
<property name="password">test</property>
<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="driverClass">oracle.jdbc.OracleDriver</property>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>

<user-overrides user="test-user">
<property name="maxPoolSize">10</property>
<property name="minPoolSize">1</property>
<property name="maxStatements">0</property>
</user-overrides>
</default-config>

<!-- This app is massive! -->
<named-config name="intergalactoApp">
<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>

<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>

<named-config name="userApp">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">20</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
package com.c3p0.test;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class DbConnection {
private static DataSource dataSource;
static {
dataSource
= new ComboPooledDataSource("userApp");
}

public static Connection getConnectioon() throws SQLException {
return dataSource.getConnection();
}
}

以上是使用配置文件的形式

 

package com.c3p0.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;

public final class ConnectionManager {
private static ConnectionManager instance;

public ComboPooledDataSource ds;
private static String c3p0Properties = "c3p0.properties";

private ConnectionManager() throws Exception {
Properties p
= new Properties();
p.load(
this.getClass().getResourceAsStream(c3p0Properties));
ds
= new ComboPooledDataSource();
ds.setUser(p.getProperty(
"user"));
ds.setPassword(p.getProperty(
"password"));
ds.setJdbcUrl(p.getProperty(
"jdbcUrl"));
ds.setDriverClass(p.getProperty(
"driverClass"));
ds.setInitialPoolSize(Integer.parseInt(p.getProperty(
"initialPoolSize")));
ds.setMinPoolSize(Integer.parseInt(p.getProperty(
"minPoolSize")));
ds.setMaxPoolSize(Integer.parseInt(p.getProperty(
"maxPoolSize")));
ds.setMaxStatements(Integer.parseInt(p.getProperty(
"maxStatements")));
ds.setMaxIdleTime(Integer.parseInt(p.getProperty(
"maxIdleTime")));
}

public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance
= new ConnectionManager();
}
catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}

public synchronized final Connection getConnection() {
try {
return ds.getConnection();
}
catch (SQLException e) {
e.printStackTrace();
}
return null;
}

protected void finalize() throws Throwable {
DataSources.destroy(ds);
// 关闭datasource
super.finalize();
}
}

以上使用properties属性文件设置属性