From ae5b44bf0338c0e20e5cfc1df1e95a3fa7a52c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=B0=E9=9C=96?= Date: Mon, 14 Oct 2024 15:19:45 +0800 Subject: [PATCH] feat: support dbcp2 --- .../tracer/plugins/datasource/utils/DataSourceUtils.java | 7 +++++-- tracer-sofa-boot-starter/pom.xml | 5 +++++ .../sofa/tracer/boot/datasource/DataSourceUrlTest.java | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/main/java/com/alipay/sofa/tracer/plugins/datasource/utils/DataSourceUtils.java b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/main/java/com/alipay/sofa/tracer/plugins/datasource/utils/DataSourceUtils.java index 5dbdfcfee..a90994099 100644 --- a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/main/java/com/alipay/sofa/tracer/plugins/datasource/utils/DataSourceUtils.java +++ b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/main/java/com/alipay/sofa/tracer/plugins/datasource/utils/DataSourceUtils.java @@ -41,6 +41,7 @@ public class DataSourceUtils { public static final String DS_DRUID_CLASS = "com.alibaba.druid.pool.DruidDataSource"; public static final String DS_DBCP_CLASS = "org.apache.commons.dbcp.BasicDataSource"; + public static final String DS_DBCP2_CLASS = "org.apache.commons.dbcp2.BasicDataSource"; public static final String DS_C3P0_CLASS = "com.mchange.v2.c3p0.ComboPooledDataSource"; @@ -69,11 +70,13 @@ public static boolean isDruidDataSource(String clazzType) { } public static boolean isDbcpDataSource(Object dataSource) { - return isTargetDataSource(DS_DBCP_CLASS, dataSource); + return isTargetDataSource(DS_DBCP_CLASS, dataSource) + || isTargetDataSource(DS_DBCP2_CLASS, dataSource); } public static boolean isDbcpDataSource(String clazzType) { - return !StringUtils.isBlank(clazzType) && DS_DBCP_CLASS.equals(clazzType); + return !StringUtils.isBlank(clazzType) + && (DS_DBCP_CLASS.equals(clazzType) || DS_DBCP2_CLASS.equals(clazzType)); } public static boolean isC3p0DataSource(Object dataSource) { diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml index 4c9a7d44f..44cbdc021 100644 --- a/tracer-sofa-boot-starter/pom.xml +++ b/tracer-sofa-boot-starter/pom.xml @@ -236,6 +236,11 @@ 1.4 test + + org.apache.commons + commons-dbcp2 + test + com.github.kstyrc embedded-redis diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/datasource/DataSourceUrlTest.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/datasource/DataSourceUrlTest.java index 14b06863e..ae51639bb 100644 --- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/datasource/DataSourceUrlTest.java +++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/datasource/DataSourceUrlTest.java @@ -51,6 +51,14 @@ public void testGetDataSourceUrl() throws Throwable { Assert.assertNotNull(method); Assert.assertEquals("test-url", method.invoke(basicDataSource)); + // dbcp2 + org.apache.commons.dbcp2.BasicDataSource basicDataSource2 = new org.apache.commons.dbcp2.BasicDataSource(); + basicDataSource2.setUrl("test-url"); + method = ReflectionUtils.findMethod(basicDataSource2.getClass(), + DataSourceUtils.METHOD_GET_URL); + Assert.assertNotNull(method); + Assert.assertEquals("test-url", method.invoke(basicDataSource2)); + // tomcat datasource DataSource dataSource = new DataSource(); dataSource.setUrl("test-url");