赞
踩
有个更新数据库的sql耗时较长报错The last packet successfully received from the server was 10,010 millisecond
网上都是让在配置文件加connectTimeout和socketTimeout,然而实际没作用。
后面发现我的druid版本1.2.14没有上面的两个参数。
所以解决方法就是升级druid版本或者直接修改Druid配置文件,这里记录修改配置的方法。
一共修改两个文件:
1、DataSourceProperties
- /**
- * Copyright (c) 2018 人人开源 All rights reserved.
- *
- * https://www.renren.io
- *
- * 版权所有,侵权必究!
- */
-
- package io.renren.commons.dynamic.datasource.properties;
-
- /**
- * 多数据源属性
- *
- * @author Mark sunlightcs@gmail.com
- * @since 1.0.0
- */
- public class DataSourceProperties {
- private String driverClassName;
- private String url;
- private String username;
- private String password;
-
- /**
- * Druid默认参数
- */
- private int connectTimeout=60000;//新增
- private int socketTimeout=60000;//新增
- private int initialSize = 2;
- private int maxActive = 10;
- private int minIdle = -1;
- private long maxWait = 60 * 1000L;
- private long timeBetweenEvictionRunsMillis = 60 * 1000L;
- private long minEvictableIdleTimeMillis = 1000L * 60L * 30L;
- private long maxEvictableIdleTimeMillis = 1000L * 60L * 60L * 7;
- private String validationQuery = "select 1";
- private int validationQueryTimeout = -1;
- private boolean testOnBorrow = false;
- private boolean testOnReturn = false;
- private boolean testWhileIdle = true;
- private boolean poolPreparedStatements = false;
- private int maxOpenPreparedStatements = -1;
- private boolean sharePreparedStatements = false;
- private String filters = "stat,wall";
-
- //新增
- public int getConnectTimeout() {
- return connectTimeout;
- }
- //新增
- public void setConnectTimeout(int connectTimeout) {
- this.connectTimeout = connectTimeout;
- }
- //新增
- public int getSocketTimeout() {
- return socketTimeout;
- }
- //新增
- public void setSocketTimeout(int socketTimeout) {
- this.socketTimeout = socketTimeout;
- }
-
- public String getDriverClassName() {
- return driverClassName;
- }
-
- public void setDriverClassName(String driverClassName) {
- this.driverClassName = driverClassName;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public int getInitialSize() {
- return initialSize;
- }
-
- public void setInitialSize(int initialSize) {
- this.initialSize = initialSize;
- }
-
- public int getMaxActive() {
- return maxActive;
- }
-
- public void setMaxActive(int maxActive) {
- this.maxActive = maxActive;
- }
-
- public int getMinIdle() {
- return minIdle;
- }
-
- public void setMinIdle(int minIdle) {
- this.minIdle = minIdle;
- }
-
- public long getMaxWait() {
- return maxWait;
- }
-
- public void setMaxWait(long maxWait) {
- this.maxWait = maxWait;
- }
-
- public long getTimeBetweenEvictionRunsMillis() {
- return timeBetweenEvictionRunsMillis;
- }
-
- public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
- this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
- }
-
- public long getMinEvictableIdleTimeMillis() {
- return minEvictableIdleTimeMillis;
- }
-
- public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
- this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
- }
-
- public long getMaxEvictableIdleTimeMillis() {
- return maxEvictableIdleTimeMillis;
- }
-
- public void setMaxEvictableIdleTimeMillis(long maxEvictableIdleTimeMillis) {
- this.maxEvictableIdleTimeMillis = maxEvictableIdleTimeMillis;
- }
-
- public String getValidationQuery() {
- return validationQuery;
- }
-
- public void setValidationQuery(String validationQuery) {
- this.validationQuery = validationQuery;
- }
-
- public int getValidationQueryTimeout() {
- return validationQueryTimeout;
- }
-
- public void setValidationQueryTimeout(int validationQueryTimeout) {
- this.validationQueryTimeout = validationQueryTimeout;
- }
-
- public boolean isTestOnBorrow() {
- return testOnBorrow;
- }
-
- public void setTestOnBorrow(boolean testOnBorrow) {
- this.testOnBorrow = testOnBorrow;
- }
-
- public boolean isTestOnReturn() {
- return testOnReturn;
- }
-
- public void setTestOnReturn(boolean testOnReturn) {
- this.testOnReturn = testOnReturn;
- }
-
- public boolean isTestWhileIdle() {
- return testWhileIdle;
- }
-
- public void setTestWhileIdle(boolean testWhileIdle) {
- this.testWhileIdle = testWhileIdle;
- }
-
- public boolean isPoolPreparedStatements() {
- return poolPreparedStatements;
- }
-
- public void setPoolPreparedStatements(boolean poolPreparedStatements) {
- this.poolPreparedStatements = poolPreparedStatements;
- }
-
- public int getMaxOpenPreparedStatements() {
- return maxOpenPreparedStatements;
- }
-
- public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
- this.maxOpenPreparedStatements = maxOpenPreparedStatements;
- }
-
- public boolean isSharePreparedStatements() {
- return sharePreparedStatements;
- }
-
- public void setSharePreparedStatements(boolean sharePreparedStatements) {
- this.sharePreparedStatements = sharePreparedStatements;
- }
-
- public String getFilters() {
- return filters;
- }
-
- public void setFilters(String filters) {
- this.filters = filters;
- }
- }
2、DynamicDataSourceFactory
- /**
- * Copyright (c) 2018 人人开源 All rights reserved.
- *
- * https://www.renren.io
- *
- * 版权所有,侵权必究!
- */
-
- package io.renren.commons.dynamic.datasource.config;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import io.renren.commons.dynamic.datasource.properties.DataSourceProperties;
-
- import java.sql.SQLException;
-
- /**
- * DruidDataSource
- *
- * @author Mark sunlightcs@gmail.com
- * @since 1.0.0
- */
- public class DynamicDataSourceFactory {
-
- public static DruidDataSource buildDruidDataSource(DataSourceProperties properties) {
- DruidDataSource druidDataSource = new DruidDataSource();
- druidDataSource.setDriverClassName(properties.getDriverClassName());
- druidDataSource.setUrl(properties.getUrl());
- druidDataSource.setUsername(properties.getUsername());
- druidDataSource.setPassword(properties.getPassword());
-
- druidDataSource.setInitialSize(properties.getInitialSize());
- druidDataSource.setMaxActive(properties.getMaxActive());
- druidDataSource.setMinIdle(properties.getMinIdle());
- druidDataSource.setMaxWait(properties.getMaxWait());
- druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
- druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
- druidDataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis());
- druidDataSource.setValidationQuery(properties.getValidationQuery());
- druidDataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout());
- druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
- druidDataSource.setTestOnReturn(properties.isTestOnReturn());
- druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
- druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
- druidDataSource.setSharePreparedStatements(properties.isSharePreparedStatements());
- //新增
- if (properties.getConnectTimeout() > 0) {
- druidDataSource.setConnectTimeout(properties.getConnectTimeout());
- }
- //新增
- if (properties.getSocketTimeout() > 0) {
- druidDataSource.setSocketTimeout(properties.getSocketTimeout());
- }
-
- try {
- // druidDataSource.setFilters(properties.getFilters());
- druidDataSource.init();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return druidDataSource;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。