当前位置:   article > 正文

springboot Druid长连接sql超时The last packet successfully received from the server was 10,010 millisecond

the last packet successfully received from the server was 10,010 millisecond

有个更新数据库的sql耗时较长报错The last packet successfully received from the server was 10,010 millisecond

网上都是让在配置文件加connectTimeout和socketTimeout,然而实际没作用。

后面发现我的druid版本1.2.14没有上面的两个参数。

所以解决方法就是升级druid版本或者直接修改Druid配置文件,这里记录修改配置的方法。

一共修改两个文件:

1、DataSourceProperties

  1. /**
  2. * Copyright (c) 2018 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.commons.dynamic.datasource.properties;
  9. /**
  10. * 多数据源属性
  11. *
  12. * @author Mark sunlightcs@gmail.com
  13. * @since 1.0.0
  14. */
  15. public class DataSourceProperties {
  16. private String driverClassName;
  17. private String url;
  18. private String username;
  19. private String password;
  20. /**
  21. * Druid默认参数
  22. */
  23. private int connectTimeout=60000;//新增
  24. private int socketTimeout=60000;//新增
  25. private int initialSize = 2;
  26. private int maxActive = 10;
  27. private int minIdle = -1;
  28. private long maxWait = 60 * 1000L;
  29. private long timeBetweenEvictionRunsMillis = 60 * 1000L;
  30. private long minEvictableIdleTimeMillis = 1000L * 60L * 30L;
  31. private long maxEvictableIdleTimeMillis = 1000L * 60L * 60L * 7;
  32. private String validationQuery = "select 1";
  33. private int validationQueryTimeout = -1;
  34. private boolean testOnBorrow = false;
  35. private boolean testOnReturn = false;
  36. private boolean testWhileIdle = true;
  37. private boolean poolPreparedStatements = false;
  38. private int maxOpenPreparedStatements = -1;
  39. private boolean sharePreparedStatements = false;
  40. private String filters = "stat,wall";
  41. //新增
  42. public int getConnectTimeout() {
  43. return connectTimeout;
  44. }
  45. //新增
  46. public void setConnectTimeout(int connectTimeout) {
  47. this.connectTimeout = connectTimeout;
  48. }
  49. //新增
  50. public int getSocketTimeout() {
  51. return socketTimeout;
  52. }
  53. //新增
  54. public void setSocketTimeout(int socketTimeout) {
  55. this.socketTimeout = socketTimeout;
  56. }
  57. public String getDriverClassName() {
  58. return driverClassName;
  59. }
  60. public void setDriverClassName(String driverClassName) {
  61. this.driverClassName = driverClassName;
  62. }
  63. public String getUrl() {
  64. return url;
  65. }
  66. public void setUrl(String url) {
  67. this.url = url;
  68. }
  69. public String getUsername() {
  70. return username;
  71. }
  72. public void setUsername(String username) {
  73. this.username = username;
  74. }
  75. public String getPassword() {
  76. return password;
  77. }
  78. public void setPassword(String password) {
  79. this.password = password;
  80. }
  81. public int getInitialSize() {
  82. return initialSize;
  83. }
  84. public void setInitialSize(int initialSize) {
  85. this.initialSize = initialSize;
  86. }
  87. public int getMaxActive() {
  88. return maxActive;
  89. }
  90. public void setMaxActive(int maxActive) {
  91. this.maxActive = maxActive;
  92. }
  93. public int getMinIdle() {
  94. return minIdle;
  95. }
  96. public void setMinIdle(int minIdle) {
  97. this.minIdle = minIdle;
  98. }
  99. public long getMaxWait() {
  100. return maxWait;
  101. }
  102. public void setMaxWait(long maxWait) {
  103. this.maxWait = maxWait;
  104. }
  105. public long getTimeBetweenEvictionRunsMillis() {
  106. return timeBetweenEvictionRunsMillis;
  107. }
  108. public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
  109. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  110. }
  111. public long getMinEvictableIdleTimeMillis() {
  112. return minEvictableIdleTimeMillis;
  113. }
  114. public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
  115. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
  116. }
  117. public long getMaxEvictableIdleTimeMillis() {
  118. return maxEvictableIdleTimeMillis;
  119. }
  120. public void setMaxEvictableIdleTimeMillis(long maxEvictableIdleTimeMillis) {
  121. this.maxEvictableIdleTimeMillis = maxEvictableIdleTimeMillis;
  122. }
  123. public String getValidationQuery() {
  124. return validationQuery;
  125. }
  126. public void setValidationQuery(String validationQuery) {
  127. this.validationQuery = validationQuery;
  128. }
  129. public int getValidationQueryTimeout() {
  130. return validationQueryTimeout;
  131. }
  132. public void setValidationQueryTimeout(int validationQueryTimeout) {
  133. this.validationQueryTimeout = validationQueryTimeout;
  134. }
  135. public boolean isTestOnBorrow() {
  136. return testOnBorrow;
  137. }
  138. public void setTestOnBorrow(boolean testOnBorrow) {
  139. this.testOnBorrow = testOnBorrow;
  140. }
  141. public boolean isTestOnReturn() {
  142. return testOnReturn;
  143. }
  144. public void setTestOnReturn(boolean testOnReturn) {
  145. this.testOnReturn = testOnReturn;
  146. }
  147. public boolean isTestWhileIdle() {
  148. return testWhileIdle;
  149. }
  150. public void setTestWhileIdle(boolean testWhileIdle) {
  151. this.testWhileIdle = testWhileIdle;
  152. }
  153. public boolean isPoolPreparedStatements() {
  154. return poolPreparedStatements;
  155. }
  156. public void setPoolPreparedStatements(boolean poolPreparedStatements) {
  157. this.poolPreparedStatements = poolPreparedStatements;
  158. }
  159. public int getMaxOpenPreparedStatements() {
  160. return maxOpenPreparedStatements;
  161. }
  162. public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
  163. this.maxOpenPreparedStatements = maxOpenPreparedStatements;
  164. }
  165. public boolean isSharePreparedStatements() {
  166. return sharePreparedStatements;
  167. }
  168. public void setSharePreparedStatements(boolean sharePreparedStatements) {
  169. this.sharePreparedStatements = sharePreparedStatements;
  170. }
  171. public String getFilters() {
  172. return filters;
  173. }
  174. public void setFilters(String filters) {
  175. this.filters = filters;
  176. }
  177. }

 2、DynamicDataSourceFactory

  1. /**
  2. * Copyright (c) 2018 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.commons.dynamic.datasource.config;
  9. import com.alibaba.druid.pool.DruidDataSource;
  10. import io.renren.commons.dynamic.datasource.properties.DataSourceProperties;
  11. import java.sql.SQLException;
  12. /**
  13. * DruidDataSource
  14. *
  15. * @author Mark sunlightcs@gmail.com
  16. * @since 1.0.0
  17. */
  18. public class DynamicDataSourceFactory {
  19. public static DruidDataSource buildDruidDataSource(DataSourceProperties properties) {
  20. DruidDataSource druidDataSource = new DruidDataSource();
  21. druidDataSource.setDriverClassName(properties.getDriverClassName());
  22. druidDataSource.setUrl(properties.getUrl());
  23. druidDataSource.setUsername(properties.getUsername());
  24. druidDataSource.setPassword(properties.getPassword());
  25. druidDataSource.setInitialSize(properties.getInitialSize());
  26. druidDataSource.setMaxActive(properties.getMaxActive());
  27. druidDataSource.setMinIdle(properties.getMinIdle());
  28. druidDataSource.setMaxWait(properties.getMaxWait());
  29. druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
  30. druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
  31. druidDataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis());
  32. druidDataSource.setValidationQuery(properties.getValidationQuery());
  33. druidDataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout());
  34. druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
  35. druidDataSource.setTestOnReturn(properties.isTestOnReturn());
  36. druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
  37. druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
  38. druidDataSource.setSharePreparedStatements(properties.isSharePreparedStatements());
  39. //新增
  40. if (properties.getConnectTimeout() > 0) {
  41. druidDataSource.setConnectTimeout(properties.getConnectTimeout());
  42. }
  43. //新增
  44. if (properties.getSocketTimeout() > 0) {
  45. druidDataSource.setSocketTimeout(properties.getSocketTimeout());
  46. }
  47. try {
  48. // druidDataSource.setFilters(properties.getFilters());
  49. druidDataSource.init();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. return druidDataSource;
  54. }
  55. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/1014649
推荐阅读
相关标签
  

闽ICP备14008679号