protected void createAndStartDestroyThread() {
destroyTask = new DestroyTask();
if (destroyScheduler != null) {
// <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
// <property name="timeBetweenEvictionRunsMillis" value="60000" />
long period = timeBetweenEvictionRunsMillis; if (period <= 0) { period = 1000; } destroySchedulerFuture = destroyScheduler.scheduleAtFixedRate(destroyTask, period, period, TimeUnit.MILLISECONDS); initedLatch.countDown(); return; } String threadName = "Druid-ConnectionPool-Destroy-" + System.identityHashCode(this); destroyConnectionThread = new DestroyConnectionThread(threadName); destroyConnectionThread.start(); }
public class DestroyTask implements Runnable { @Override public void run() { shrink(true); if (isRemoveAbandoned()) { removeAbandoned(); } } }
public int removeAbandoned() { int removeCount = 0; System.out.println("我来了.........................................."+DateUtils.getCurrentFullDateStr()); long currrentNanos = System.nanoTime(); List<DruidPooledConnection> abandonedList = new ArrayList<DruidPooledConnection>(); synchronized (activeConnections) { Iterator<DruidPooledConnection> iter = activeConnections.keySet().iterator(); for (; iter.hasNext();) { DruidPooledConnection pooledConnection = iter.next(); if (pooledConnection.isRunning()) { continue; } long timeMillis = (currrentNanos - pooledConnection.getConnectedTimeNano()) / (1000 * 1000); if (timeMillis >= removeAbandonedTimeoutMillis) { iter.remove(); pooledConnection.setTraceEnable(false); abandonedList.add(pooledConnection); } } } if (abandonedList.size() > 0) { for (DruidPooledConnection pooledConnection : abandonedList) { synchronized (pooledConnection) { if (pooledConnection.isDisable()) { continue; } } System.out.println("abandonedList>>>>>>>>>>>>>>>>>>>>>>"+DateUtils.getCurrentFullDateStr()); JdbcUtils.close(pooledConnection); pooledConnection.abandond(); removeAbandonedCount++; removeCount++; if (isLogAbandoned()) { StringBuilder buf = new StringBuilder(); buf.append("abandon connection, owner thread: "); buf.append(pooledConnection.getOwnerThread().getName()); buf.append(", connected at : "); buf.append(pooledConnection.getConnectedTimeMillis()); buf.append(", open stackTrace\n"); StackTraceElement[] trace = pooledConnection.getConnectStackTrace(); for (int i = 0; i < trace.length; i++) { buf.append("\tat "); buf.append(trace[i].toString()); buf.append("\n"); } buf.append("ownerThread current state is "+pooledConnection.getOwnerThread().getState() + ", current stackTrace\n"); trace = pooledConnection.getOwnerThread().getStackTrace(); for (int i = 0; i < trace.length; i++) { buf.append("\tat "); buf.append(trace[i].toString()); buf.append("\n"); } LOG.error(buf.toString()); } } } return removeCount; }
相关推荐
该项目为基于Java语言的阿里巴巴Druid数据源设计源码,共包含6149个文件,涵盖5027个Java源代码文件、623个SQL文件、173个文本文件、94个...该源码集成了丰富的开发资源,适用于深入理解和分析Druid数据源的核心实现。
分析源码时,我们可以看到各种设计模式的影子,理解这些模式是如何帮助开发者构建高效、可维护的代码库。 依赖注入也是Druid源码分析中不可或缺的一个知识点。在Druid的实现中,我们看到了Guice这一依赖注入框架的...
集成DRuid数据源配置函数源码
本示例将详细讲解如何在SpringBoot项目中集成Druid并实现多数据源的自动切换。 首先,我们需要理解多数据源的概念。多数据源意味着应用程序可以同时连接和操作多个不同的数据库,这对于数据分层、高可用性和分布式...
在本文中,我们将深入探讨如何在SpringBoot项目中整合JDBC和Druid数据源,并创建一个具有监控功能的示例。SpringBoot以其简洁的配置和快速的开发能力,深受Java开发者喜爱。而Druid作为一款优秀的数据库连接池,提供...
Druid 数据源操作指南 Druid 是阿里巴巴的一款开源数据库连接池,基于 Apache 2.0 协议,可以免费自由使用。它不仅仅是一个数据库连接池,还包含一个 ProxyDriver,一系列内置的 JDBC 组件库,一个 SQL Parser。...
本配置文件是讲怎样在springboot环境下配置druid数据源,期中包括了一些高级配置,这里是配置文件,另外还需要写配置类,具体请移步本人博客springboot整合druid数据源查看
本实例将详细介绍如何在Spring MVC项目中配置并使用Druid数据源。 首先,我们需要理解Spring MVC和Druid的基本概念。Spring MVC是Spring框架的一部分,用于构建Web应用程序,它遵循MVC(Model-View-Controller)...
本教程将围绕"Springboot+Mybatis+Druid多数据源配置"这一主题,详细介绍如何实现这一功能。 首先,Spring Boot简化了Spring应用的初始化和配置。在多数据源场景下,我们可以通过Spring Boot的@...
项目中的"demo"可能包含了以下内容:主配置类、数据源配置类、MyBatis配置类、Mapper接口及XML文件、Druid配置、以及可能的业务逻辑代码。通过阅读和理解这些代码,你可以深入理解如何在实际项目中实现Spring Boot、...
以下是在Spring+Struts2项目中集成Druid数据源的详细步骤: 1. **添加依赖** 首先,你需要在项目的pom.xml文件中添加Druid的Maven依赖。确保你的依赖库是最新的,例如: ```xml <groupId>...
在本项目"SpringBoot + Mybatis + Druid数据源demo"中,我们将探讨如何整合SpringBoot、Mybatis和Druid数据源来构建一个高效且灵活的数据访问层。SpringBoot以其简化Spring应用初始搭建以及开发过程而受到广泛欢迎,...
本项目将介绍如何在SpringBoot项目中结合Maven构建一个支持多数据源的系统,特别是如何处理MySQL和SQLServer两种不同的数据库类型,并使用Druid作为数据源连接池。 首先,我们需要理解“多数据源”这一概念。多数据...
Druid数据源的优势在于其强大的监控能力。它提供了Web监控统计页面,可以实时查看数据库连接池的状态,包括连接数量、空闲/活跃连接、SQL执行情况等。在Spring Boot项目中,我们可以通过引入Druid的starter包,配置...
在本文中,我们将深入探讨如何将SpringBoot与Druid数据源进行整合,以及如何启用后台的Servlet和Filter监控。SpringBoot以其简洁的配置和强大的自动配置能力,深受开发者喜爱,而Druid则是一款功能丰富的数据库连接...
1. 数据源(DataSource):作为数据库连接池的核心,DruidDataSource提供了初始化、连接获取与释放、连接池状态监控等功能。它支持配置各种参数,如最大连接数、最小连接数、超时时间等,以满足不同应用场景的需求。...
在本文中,我们将深入探讨如何在SpringBoot项目中配置并使用Druid数据源监控页面。Druid是一个功能强大的数据库连接池,它提供了丰富的监控和管理功能,有助于优化数据库操作和性能。SpringBoot作为轻量级的Java框架...
通过分析源码,我们可以学习到以下几个关键知识点: 1. **连接池设计**:Druid连接池提供了线程安全的数据库连接管理,它支持连接预热、健康检查、自动回收和扩展。源码中的`...
3. **配置Druid数据源**:使用`DruidDataSource`类作为数据源,并在配置类中通过`@Bean`注解声明。别忘了为每个数据源设置不同的名字,如`primaryDataSource`和`secondaryDataSource`。 4. **创建数据源路由`...
本项目是一个关于"Spring Boot与Druid多数据源示例"的实践,主要展示了如何利用阿里开源的Druid数据源连接池来管理多个数据库。下面将详细解释这个项目中的关键知识点。 1. **Spring Boot**: Spring Boot简化了...