当 Druid 连接池中的连接超时时,可以通过配置相关的参数来处理超时连接。以下是一些处理超时连接的常见方法:
spring.datasource.druid.maxWait=10000
spring.datasource.druid.removeAbandoned=true
spring.datasource.druid.removeAbandonedTimeout=180
DruidDataSource dataSource = (DruidDataSource) dataSource;
int removeAbandonedTimeout = dataSource.getRemoveAbandonedTimeout();
int abandonedConnectionCount = dataSource.getRemoveAbandonedCount();
if (removeAbandonedTimeout > 0 && abandonedConnectionCount > 0) {
Date now = new Date();
List<DruidPooledConnection> abandonedConnections = new ArrayList<>();
for (DruidPooledConnection connection : dataSource.getConnections()) {
if ((now.getTime() - connection.getLastActiveTimeMillis()) > removeAbandonedTimeout) {
abandonedConnections.add(connection);
}
}
for (DruidPooledConnection connection : abandonedConnections) {
dataSource.removeAbandoned(connection);
}
}
通过以上方法可以有效处理 Druid MyBatis 超时连接的问题,保证应用程序的稳定性和性能。