Skip to content

Java客户端 数据源配置

He, Jiehui edited this page Sep 20, 2016 · 11 revisions

简介

Dal通过datasource.xml定义实际使用的数据库配置。dal启动的时候会扫描datasource.xml并根据里面的配置初始化数据库连接池。目前使用的连接池实现为tomcat-jdbc。

datasource.xml可以放在项目的resource目录,运行时会去找jar包的根目录寻找。

配置说明

datasource.xml的结构是一个2层的嵌套元素。顶层为Datasources,包含1到多个Datasource。每个Datasource包含与数据库建立连接和数据库连接池创建相关的属性

例子

<Datasources>
    <Datasource name="SimpleShard_1"
        userName="userName"
        password="password"
        connectionUrl="jdbc:mysql://yourcompany.com:1234/SimpleShard_1"
        driverClassName="com.mysql.jdbc.Driver"
        testWhileIdle="true"
        testOnBorrow="false"
        testOnReturn="false"
        validationQuery="SELECT 1"
        validationInterval="30000"
        timeBetweenEvictionRunsMillis="30000"
        maxActive="100"
        minIdle="10"
        maxWait="10000"
        initialSize="10"
        removeAbandonedTimeout="60"
        removeAbandoned="true"
        logAbandoned="true"
        minEvictableIdleTimeMillis="30000"/>
    <Datasource name="SimpleShard_0"
        userName="userName"
        password="password"
        connectionUrl="jdbc:mysql://yourcompany.com:1234/SimpleShard_0"
        driverClassName="com.mysql.jdbc.Driver"
        testWhileIdle="true"
        testOnBorrow="false"
        testOnReturn="false"
        validationQuery="SELECT 1"
        validationInterval="30000"
        timeBetweenEvictionRunsMillis="30000"
        maxActive="100"
        minIdle="10"
        maxWait="10000"
        initialSize="10"
        removeAbandonedTimeout="60"
        removeAbandoned="true"
        logAbandoned="true"
        minEvictableIdleTimeMillis="30000"/>
</Datasources>

缺省的连接池属性

如果没找到相关的配置项,dal会使用内置的缺省值来初始化连接池。缺省值和code gen生成datasource.xml所使用的值大体相同

testWhileIdle = false;
testOnBorrow = false;
testOnReturn = false;
validationQuery = "SELECT 1";
validationInterval = 30000L;
timeBetweenEvictionRunsMillis = 5000;
maxActive = 100;
minIdle = 1;
maxWait = 10000;
initialSize = 1;
removeAbandonedTimeout = 60;
removeAbandoned = true;
logAbandoned = true;
minEvictableIdleTimeMillis = 30000;

数据库连接池参数详细说明请参考

常用选项

removeAbandonedTimeout:超时设置,单位秒

推荐配置

Sqlserver

我们实践下来,Sqlserver不太耐受测试连接的请求,请修改每个datasource内部的"testWhileIdle"选项改为"false"。

如果应用空闲和繁忙时对数据库的访问频率相差巨大,建议修改下面3个配置项的值

minIdle="1"。连接池里面空闲连接的最小个数
initialSize="1"。连接池里面初始连接的最小个数
timeBetweenEvictionRunsMillis="5000"。回收线程的运行间隔(毫秒)

如果存在超过8小时都没有请求发生的事情,建议把最小值改为0:

minIdle="0"。连接池里面空闲连接的最小个数

或者添加一个maxAge属性,并设置maxAge为一个DBA认可的一个数据库连接可保持有效的最大时间长度,单位为毫秒

同时检查并请保证下面两个选项也为false

testOnBorrow="false"
testOnReturn="false"

Mysql

如果应用空闲和繁忙时对数据库的访问频率相差巨大,建议修改下面3个配置项的值

minIdle="1"。连接池里面空闲连接的最小个数
initialSize="1"。连接池里面初始连接的最小个数
timeBetweenEvictionRunsMillis="5000"。回收线程的运行间隔(毫秒)
testWhileIdle="true"。空闲时检测链接是否有效
validationInterval="1800000"。最大链接测试间隔,单位为毫秒,目前值为半小时

如果存在超过8小时都没有请求发生的事情,建议把最小值改为0:

minIdle="0"。连接池里面空闲连接的最小个数

或者添加一个maxAge属性,并设置maxAge为一个DBA认可的一个数据库连接可保持有效的最大时间长度,单位为毫秒

同时检查并请保证下面两个选项也为false

testOnBorrow="false"
testOnReturn="false"

Mysql连接超时报错处理

Mysql 连接存在超时报错的现象。当连接池里面缓存的连接在超过Mysql服务器设置的wait_timeout的时间范围内没有任何数据库请求时,在下次请求的时候会报错。信息类似下面:

Mysql error

     Log Name: Get connection
     Event: 2011
     Message: Connectiing to FNCwalletAccountshardDB database failed.
       
       
       
     ********** Exception Info **********
     com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 39,454,778 milliseconds ago.  The last packet sent successfully to the server was 39,454,779 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
     at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)

原因是minIdle>0并且testWhileIdle=“false”或validationInterval<wait_timeout。请按照上面的说明配置

连接串属性

可以通过connectionProperties配置额外的连接串属性

Sql Server

MySQL

推荐配置

通过添加connectionProperties属性可以设置数据库连接属性

Mysql

connectionProperties="rewriteBatchedStatements=true;allowMultiQueries=true"

SqlServer

connectionProperties="sendTimeAsDateTime=false;sendStringParametersAsUnicode=false"

目前代码生成器缺省生成上面的配置

缺省隔离级别

数据库连接池缺省建立的连接按照数据库的不同会有不同的缺省事务隔离级别

Mysql是TRANSACTION_REPEATABLE_READ

A constant indicating that dirty reads and non-repeatable reads are prevented; phantom

reads can occur.  This level prohibits a transaction from reading a row with uncommitted

changes in it, and it also prohibits the situation where one transaction reads a row, a second

transaction alters the row, and the first transaction rereads the row, getting different values the second time

(a "non-repeatable read").

Sqlserver是TRANSACTION_READ_COMMITTED

 A constant indicating that dirty reads are prevented; non-repeatable reads and phantom

reads can occur.  This level only prohibits a transaction from reading a row with uncommitted changes in it.
Clone this wiki locally