首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

C3p0源码探索(1)之配置篇

2012-09-03 
C3p0源码探索(一)之配置篇?C3p0源码探索(一)之配置篇?ComboPooledDataSource cpds new ComboPooledDataS

C3p0源码探索(一)之配置篇

?

C3p0源码探索(一)之配置篇

?

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();
?

?

?

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>
?

?

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即可
?

参数配置:

除了以上连接数据库必要的参数外,提供以下最基本的参数配置信息才能形成数据库连接池

?

<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"                              driverstyle="margin-left: 18.75pt; text-indent: 0cm;">?

?

?

<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");

热点排行