Spring Boot + druid在配置连接池发布PAAS平
2018-07-31 本文已影响0人
梦想又照进现实
Spring Boot + sharding jdbc + druid在配置连接池发布PAAS平台后出现No operations allowed after connection closed连接异常的解决过程;
- 错误信息
- 数据源配置
- druid配置项介绍
- 完善后数据源配置
spring boot下用sharing jdbc和druid一起做分库分表的实现,在设置完两个数据源以后发布PAAS平台出现1小时左右应用报500错误,
实时日志打印:
APPPROCWEB0 2018-07-31 03:06:44.519 ERROR 63 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException:
APPPROCWEB0 ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
APPPROCWEB0 ### The error may exist in file [/home/vcap/app/BOOT-INF/classes/mapper/StdCrdIdxMapper.xml]
APPPROCWEB0 ### The error may involve comir.route.infrastructure.repository.mapper.StdCrdIdxMapper.selectListByStdCrdNbr-Inline
APPPROCWEB0 ### The error occurred while setting parameters
APPPROCWEB0 ### SQL: select crd_ord_nbr, std_crd_nbr from t_std_crd_idx where std_crd_nbr = ?
APPPROCWEB0 ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
APPPROCWEB0 ; SQL []; No operations allowed after connection closed.; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.] with root cause
APPPROCWEB0 java.net.SocketException: Broken pipe (Write failed)
APPPROCWEB0 at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_121]
APPPROCWEB0 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) ~[na:1.8.0_121]
APPPROCWEB0 at java.net.SocketOutputStream.write(SocketOutputStream.java:155) ~[na:1.8.0_121]
APPPROCWEB0 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) ~[na:1.8.0_121]
APPPROCWEB0 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140) ~[na:1.8.0_121]
APPPROCWEB0 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3731) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
APPPROCWEB0 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
APPPROCWEB0 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
APPPROCWEB0 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.type.prepared.PreparedStatementExecutor$3.execute(PreparedStatementExecutor.java:97) ~[sharding-jdbc-core-1.5.4.jar!/:na]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.type.prepared.PreparedStatementExecutor$3.execute(PreparedStatementExecutor.java:93) ~[sharding-jdbc-core-1.5.4.jar!/:na]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine.executeInternal(ExecutorEngine.java:175) ~[sharding-jdbc-core-1.5.4.jar!/:na]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine.syncExecute(ExecutorEngine.java:155) ~[sharding-jdbc-core-1.5.4.jar!/:na]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine.execute(ExecutorEngine.java:124) ~[sharding-jdbc-core-1.5.4.jar!/:na]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine.executePreparedStatement(ExecutorEngine.java:96) ~[sharding-jdbc-core-1.5.4.jar!/:na]
APPPROCWEB0 at com.dangdang.ddframe.rdb.sharding.executor.type.prepared.PreparedStatementExecutor.execute(PreparedStatementExecutor.java:93) ~[sharding-jdbc-core-1.5.4.jar!/:na]
application.properties配置如下
#datasource
spring.devtools.remote.restart.enabled=false
#data source1
spring.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.db1.driverClassName=com.mysql.jdbc.Driver
spring.datasource.db1.url=jdbc:mysql://55.12.218.xxx:3306/mccb?characterEncoding=UTF-8&useSSL=true
spring.datasource.db1.username=mccb01exec
spring.datasource.db1.password=Mccb01exec
#data source2
spring.datasource.db2.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.db2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.db2.url=jdbc:mysql://55.12.202.xxx:3306/mccb?characterEncoding=UTF-8&useSSL=true
spring.datasource.db2.username=mccb01exec
spring.datasource.db2.password=Mccb01exec
Druid配置项说明
配置 | 缺省值 | 说明 |
---|---|---|
initialSize | 0 | 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 |
maxActive | 8 | 最大连接池数量 |
maxIdle | 8 | 已经不再使用,配置了也没效果 |
minIdle | 最小连接池数量 | |
maxWait | 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁 | |
poolPreparedStatements | false | 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 |
maxPoolPreparedStatementPerConnectionSize | -1 | 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 |
validationQuery | 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 | |
validationQueryTimeout | 单位:秒,检测连接是否有效的超时时间。底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法 | |
testOnBorrow | true | 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 |
testOnReturn | false | 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 |
testWhileIdle | false | 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 |
keepAlive | false | 连接池中的minIdle数量以内的连接,空闲时间超过minEvictableIdleTimeMillis,则会执行keepAlive操作。 |
timeBetweenEvictionRunsMillis | 1分钟(1.0.14) | 有两个含义:1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 |
numTestsPerEvictionRun | 30分钟(1.0.14) | 不再使用,一个DruidDataSource只支持一个EvictionRun |
minEvictableIdleTimeMillis | 连接保持空闲而不被驱逐的最小时间 | |
connectionInitSqls | 物理连接初始化的时候执行的sql | |
exceptionSorter | 根据dbType自动识别 当数据库抛出一些不可恢复的异常时,抛弃连接 |
application.properties配置修改如下
#data source1
spring.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.db1.driverClassName=com.mysql.jdbc.Driver
spring.datasource.db1.url=jdbc:mysql://55.12.218.xxx:3306/mccb?characterEncoding=UTF-8&useSSL=true
spring.datasource.db1.username=mccb01exec
spring.datasource.db1.password=mccb01exec
spring.datasource.db1.initial-size=5
spring.datasource.db1.max-wait=10000
spring.datasource.db1.max-idle=10
spring.datasource.db1.min-idle=5
spring.datasource.db1.validation-query=SELECT 1
spring.datasource.db1.test-while-idle=true
spring.datasource.db1.test-on-borrow=false
spring.datasource.db1.test-on-return=false
spring.datasource.db1.time-between-eviction-runs-millis=18800
spring.datasource.db1.min-evictable-idle-time-millis=150000
spring.datasource.db1.pool-prepared-statements=true
spring.datasource.db1.max-open-prepared-statements=20
#
##data source2
spring.datasource.db2.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.db2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.db2.url=jdbc:mysql://55.12.202.xxx:3306/mccb?characterEncoding=UTF-8&useSSL=true
spring.datasource.db2.username=mccb01exec
spring.datasource.db2.password=mccb01exec
spring.datasource.db2.initial-size=5
spring.datasource.db2.max-wait=10000
spring.datasource.db2.max-idle=10
spring.datasource.db2.min-idle=5
spring.datasource.db2.validation-query=SELECT 1
spring.datasource.db2.test-while-idle=true
spring.datasource.db2.test-on-borrow=false
spring.datasource.db2.test-on-return=false
spring.datasource.db2.time-between-eviction-runs-millis=18800
spring.datasource.db2.min-evictable-idle-time-millis=150000
spring.datasource.db2.pool-prepared-statements=true
spring.datasource.db2.max-open-prepared-statements=20
再次发布PAAS平台,应用运行正常无报错。