用 jboos 配置数据源连接数据库时,在长时间没有操作数据库后,再次操作数据库会出现java.lang.reflect.InvocationTargetException警告,大致警告内容如下:
WARN [org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker] (http--10.8.221.69-8080-4) Unexpected error: java.lang.reflect.InvocationTargetException
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,096,711 milliseconds ago. The last packet sent successfully to the server was 930,460 milliseconds ago.
...
Caused by: java.net.SocketException: 连接超时
...
WARN [org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory] (http--10.8.221.69-8080-4) Destroying connection that is not valid, due to the following exception: com.mysql.jdbc.JDBC4Connection@160d09ec: java.sql.SQLException: Ping failed: java.lang.reflect.InvocationTargetException
...
WARN [org.jboss.jca.core.connectionmanager.listener.TxConnectionListener] (http--10.8.221.69-8080-4) IJ000305: Connection error occured: org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@3091faea[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@32067043 connection handles=0 lastUse=1552621036027 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.PoolBySubject@32ac8b1d pool internal context=SemaphoreArrayListManagedConnectionPool@5de136e8[pool=cloudMysql] xaResource=LocalXAResourceImpl@44b4585d[connectionListener=3091faea connectionManager=4c63ce6 warned=false currentXid=null] txSync=null]: java.sql.SQLException: Ping failed: java.lang.reflect.InvocationTargetException
经查询发现数据库连接池在长时间连接后,如果在其间没有进行过数据库操作,那么数据库会中断连接,此时数据库连接池并不知道已经中断,所以当再次进行数据库操作时,会出现以上警告。
由于时用jboss配置的数据源,所以我们需要对jboss进行配置
配置如下
<datasources>
<datasource jndi-name="java:jboss/datasources/cloudMysql" pool-name="cloudMysql" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://xx.xx.xx.xx:3306/xxxx?characterEncoding=GBK</connection-url>
<driver>mysql</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>100</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<security-domain>EncryptedPassword</security-domain>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<check-valid-connection-sql>select 1</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<!-- <background-validation>false</background-validation> -->
<background-validation>true</background-validation>
<background-validation-millis>10</background-validation-millis>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>2</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
</datasource>
</datasources>
其中
check-valid-connection-sql 在 sql执行之前执行一条在这个配置项中配置的sql
background-validation 当使用这个功能的时候,jboss将使用一个独立的线程(ConnectionValidator)去验证当前池中的连接
background-validation-millis ConnectionValidator线程被唤醒的定时间隔。默认设置为10分钟
exception-sorter-class-name 这个配置项的 作用是捕获数据库请求中所返回的异常,如果是可以判定为该连接失效的异常,则将这个连接废弃。