连接池 commons-dbcp2-2.1.1使用

原理就不说了 百度一坨坨

配置文件:

driverClassName=dm.jdbc.driver.DmDriver
url=jdbc\:dm\://192.168.0.1\:5240?autoReconnect\=true
username=aaaa
password=qqqqqq
#最小空闲连接
minIdle=5
#最大空闲连接
mzxIdle=20
maxActive=400
#初始化连接数
initialSize=30
#连接遗漏时是否打印
logAbandoned=true
#是否自动回收超时连接
removeAbandoned=true
#等待超时最大时间
maxWait=5000


编写测试代码

package createTmrData;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

public class JDBCTest {

    public static void main(String[] args) {
        BasicDataSource bs = null;
        Properties p = new Properties();
        InputStream inStream = JDBCTest.class.getResourceAsStream("/conf/jdbc.properties");
        try {
            p.load(inStream);
            bs = BasicDataSourceFactory.createDataSource(p);    
            Connection conn = bs.getConnection();
            Statement stm = conn.createStatement();
            ResultSet set  = stm.executeQuery("select count(*) from RDP_MD_CUSTOMERTABLE");
            while(set.next()){
                System.out.println("表总数为:"+set.getString(1));;
            }
            conn.close();
               
        } catch (Exception e) {
        }
        
    }
}




添加包commons-dbcp2-2.1.1.jar

下载地址:http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi

执行报如下错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
	at org.apache.commons.dbcp2.BasicDataSourceFactory.<clinit>(BasicDataSourceFactory.java:64)
	at createTmrData.JDBCTest.main(JDBCTest.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 2 more

解决办法:

添加commons-logging-4.0.6.jar

执行结果:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool2/PooledObjectFactory
	at org.apache.commons.dbcp2.BasicDataSourceFactory.createDataSource(BasicDataSourceFactory.java:338)
	at createTmrData.JDBCTest.main(JDBCTest.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.PooledObjectFactory
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 2 more

解决办法:添加commons-pool2-2.4.2.jar

下载地址:http://commons.apache.org/proper/commons-pool/download_pool.cgi

再执行:

表总数为:5625838


执行成功了

下来看一下基本的参数配置,我们打开BasicDataSourceFactory.createDataSource(p)的源代码:

 public static BasicDataSource createDataSource(Properties properties) throws Exception {
        BasicDataSource dataSource = new BasicDataSource();
        String value = null;

        value = properties.getProperty(PROP_DEFAULTAUTOCOMMIT);
        if (value != null) {
            dataSource.setDefaultAutoCommit(Boolean.valueOf(value));
        }

        value = properties.getProperty(PROP_DEFAULTREADONLY);
        if (value != null) {
            dataSource.setDefaultReadOnly(Boolean.valueOf(value));
        }

         ..........
        // Return the configured DataSource instance
        return dataSource;
    }
这个方法返回了一个DataSource的实例,这里用了一大堆代码读取我们配置的参数赋值给了BasicDataSource的对象

我们再看一下有有哪些重要配置

在这个的最上面定义了一大堆final 静态的配置,final是为了不让人恶意更改:

下面我列出一些常用的配置:

  private static final String PROP_DEFAULTAUTOCOMMIT = "defaultAutoCommit";//是否自动提交
    private static final String PROP_DEFAULTREADONLY = "defaultReadOnly";//是否只读
    private static final String PROP_DEFAULTTRANSACTIONISOLATION = "defaultTransactionIsolation";//<code class="language-yaml"><span class="com">指定由连接池所创建的连接的事务隔离级别</span></code>

    private static final String PROP_MAXIDLE = "maxIdle";//<code class="language-yaml"><span class="com">最大的空闲连接数量</span></code>
    private static final String PROP_MINIDLE = "minIdle";//<code class="language-yaml"><span class="com">最小的空闲链接数量</span></code>
    private static final String PROP_INITIALSIZE = "initialSize";//<code class="language-yaml"><span class="com">初始化时连接池中connection数量</span></code>
    private static final String PROP_MAXWAITMILLIS = "maxWaitMillis";//<code class="language-yaml"><span class="com">最大的等待时间,单位是毫秒</span></code>
 
    private static final String PROP_PASSWORD = "password";
    private static final String PROP_URL = "url";
    private static final String PROP_USERNAME = "username";
    private static final String PROP_CONNECTIONPROPERTIES = "connectionProperties";//<code class="language-yaml">建立连接时的附加参数,如果指定的编码不一致数据库中会出现乱码</code>
    private static final String NUPROP_MAXACTIVE = "maxActive";//<code class="language-yaml"><span class="com">最大连接数量</span></code>


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页