C3p0源码探索(一)之配置篇
所需文件:
1、 c3p0-0.9.1.2.jar http://sourceforge.net/projects/c3p0/
2、 mysql.jar http://dev.mysql.com/downloads/connector/j/5.0.html
3、 c3p0-0.9.1.2http://nchc.dl.sourceforge.net/sourceforge/c3p0/c3p0-0.9.1.2.src.zip(可选)
拥有以上三样东西就可以开始c3p0之旅了,把mysql.jar和c3p0-0.9.1.2.jar放到classpath中就可以开始编写我们的代码了。
C3p0最简单的使用方式就如其官网下所说的一样,只需提供driverName,url,user,password,程序就可以跑起来。
第一种获取数据源的方式:
ComboPooledDataSource cpds = new ComboPooledDataSource();
String driverClass = "com.mysql.jdbc.Driver";
String jdbcURL = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "";
cpds.setDriverClass(driverClass);
cpds.setJdbcUrl(jdbcURL);
cpds.setUser(user);
cpds.setPassword(password);
cpds.setMaxStatements(100);
Connection conn = cpds.getConnection();
正如简单的jdbc连接数据库一样仅仅只需要这些参数而已。
对于这种配置,如果classpath中有c3p0.properties的配置文件,代码中不需要设置连接信息,直接new
ComboPooledDataSource(),他会自动读取配置文件中的配置。当然也可以使用c3p0-config.xml文件配置连接信息,使用xml作为配置信息的话,comboPoolDataSource还可以接受一个String参数,这个参数的名称是在c3p0-config.xml文件中配置,这就意味着我们可以在xml文件中可有都多个数据库源连接信息,比如可以是mysql,oracle的。
ComboPooledDataSource cpds = new ComboPooledDataSource(“test”);
<named-config name="test">
<property name="maxStatements">200</property> <propertyname="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password"></property>
</named-config>
使用配置文件的方式连接时,c3p0默认在classpath根目录读取配置文件,如果想把配置文件放在自己想放得地方只需设置系统属性
System.setProperties(“ com.mchange.v2.c3p0.cfg.xml”,”config/c3p0-config.xml”);
程序就在指定的目录下读取该配置文件。
第二种方式获取数据源,使用数据源工厂类DataSources
DataSource ds = DataSources.unpooledDataSource(jdbcURL, user, password);
DataSource pooledDateSource = DataSources.pooledDataSource(ds);
System.out.println(pooledDateSource.getConnection());
第三种获取数据源的方式:
PoolBackedDataSource backedDataSource = new PoolBackedDataSource();
backedDataSource.setConnectionPoolDataSource(new ConnectionPoolDataSource() );
//实现自己的connectionpooldatasource即可
参数配置:
除了以上连接数据库必要的参数外,提供以下最基本的参数配置信息才能形成数据库连接池
1、 acquireIncrement 每次连接增加数量
2、 initalPoolSize 初始连接数
3、 maxPoolSize 最大连接数
4、 maxIdleTime 最大空闲数
5、 minPoolSize 池中连接最小数量
Tomcat中配置c3p0的方法:
1、server.xml中配置
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="jdbc/test"
auth="Container"
description="User database that can be updated and saved"
factory="org.apache.naming.factory.BeanFactory"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="4"
minPoolSize="2"
acquireIncrement="1"
user="root"
password=""
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/test"
/>
</GlobalNamingResources>
2、 conf目录下Context.xml
<ResourceLink name="jdbc/test" global="jdbc/test" type="javax.sql.DataSource"/>
3、 web.xml
<resource-ref>
<res-ref-name>jdbc/ test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
测试:
InitialContext context = new InitialContext();
return (DataSource) context.lookup("java:comp/env/jdbc/test");
分享到:
相关推荐
C3P0源码及实例:C3P0资源文件配置、XML文件配置 C3P0源码及实例:C3P0资源文件配置、XML文件配置
最新C3P0jar 及其源码 C3P0具有极好的性能,容易的配置,方便开发
c3p0jar包和源码c3p0jar包和源码c3p0jar包和源码c3p0jar包和源码c3p0jar包和源码c3p0jar包和源码
c3p0连接池的源码包,版本号为0.9.5.2,直接放入eclipse添加可用
c3p0jar包和源码以及参数配置c3p0jar包和源码以及参数配置c3p0jar包和源码以及参数配置c3p0jar包和源码以及参数配置
hibernate c3p0实例源码,配置hibernate使用c3p0连接池测试demo
c3p0配置文件
C3P0连接池配置详解C3P0连接池配置详解C3P0连接池配置详解C3P0连接池配置详解
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate、Spring等。 适合人群:正在学习使用数据库连接池 里面的配置文件直接可用,少走弯路,...
<c3p0-config> <!--默认配置--> <default-config> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> ...
c3p0配置 关于c3p0的一些简单配置
用于servlet+jsp开发时连接数据库,必须要配置文件。该配置文件已经设置了时区和页面的编码问题。只要改数据库名即可用。
完整的c3p0jar包,直接创建lib目录导入就可以使用。并且有相应的配置文件,可以按照需求自行选择。
数据源 c3p0 配置 官方 参考 配置 下载
c3p0开源连接池源码,测试例子及说明文档。
C3P0 在tomcat中如何配置C3P0 在tomcat中如何配置C3P0 在tomcat中如何配置C3P0 在tomcat中如何配置
c3p0详细配置,具体讲述从从c3p0的配置参数
C3P0详细配置C3P0详细配置.docC3P0详细配置.docC3P0详细配置.docC3P0详细配置.docC3P0详细配置.docC3P0详细配置.docC3P0详细配置.docC3P0详细配置.doc
c3p Hibernate java 带源码