@Cesar
2015-12-28T10:44:09.000000Z
字数 3322
阅读 2102
sql
Java
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/jdbc
c3p0.user=root
c3p0.password=java
private static ComboPooledDataSource ds = new ComboPooledDataSource();
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">java</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
<named-config name="myApp">
<property name="user">root</property>
<property name="password">java</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</named-config>
</c3p0-config>
//如果使用default-config,那么和2中的Properties一样,否则只需要:使///用一个带参数的ComboPooledDataSource构造器就可以了
private static ComboPooledDataSource ds = new ComboPooledDataSource("myApp");
package util;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Manager {
private static C3p0Manager c3poManager;
private ComboPooledDataSource dataSource;
static {
c3poManager = new C3p0Manager();
}
public C3p0Manager() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource
.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/game?user=root&password=123456&useUnicode=true");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
// 设置初始连接池的大小!
dataSource.setInitialPoolSize(2);
// 设置连接池的最小值!
dataSource.setMinPoolSize(1);
// 设置连接池的最大值!
dataSource.setMaxPoolSize(10);
// 设置连接池中的最大Statements数量!
dataSource.setMaxStatements(50);
// 设置连接池的最大空闲时间!
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static C3p0Manager getInstance() {
return c3poManager;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接 ", e);
}
}
}
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/databasename?characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false" />
<property name="user" value="root" />
<property name="password" value="spring" />
<property name="initialPoolSize" value="1" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="40" />
<property name="maxIdleTime" value="20" />
</bean>