DBCP 配置文件加Java code

来源:互联网 发布:加入淘宝商城条件 编辑:程序博客网 时间:2024/06/05 05:30
#Driver
driverClassName=com.mysql.jdbc.Driver
#UrL
url=jdbc:mysql://192.168.111.15:3306/archive_reader
#User name
username=trafficcast
#Password
password=naitou
#The initial number of connections that are created when the pool is started. 
initialSize=10
#The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
maxActive=50
#The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
maxIdle=20
#The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none.
minIdle=5
#The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.
maxWait=60000
#The connection properties that will be sent to our JDBC driver when establishing new connections. 
connectionProperties=useUnicode=true;characterEncoding=gbk
#The default auto-commit state of connections created by this pool.
defaultAutoCommit=true
#The default read-only state of connections created by this pool. If not set then the setReadOnly method will not be called.
defaultReadOnly=
#The default TransactionIsolation state of connections created by this pool.

defaultTransactionIsolation=READ_UNCOMMITTED



package com.trafficcast.jdbc;import java.io.InputStream;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSourceFactory;public final class JdbcUtils {//Data sourceprivate static DataSource myDataSource = null;//Private Constructorprivate JdbcUtils() {}    //Init the properties and init the data source.static {try {Properties prop = new Properties();InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");prop.load(is);myDataSource = BasicDataSourceFactory.createDataSource(prop);} catch (Exception e) {throw new ExceptionInInitializerError(e);}}    /**     * Get the data source     * @return     */public static DataSource getDataSource() {return myDataSource;}}