From 027eea65db5e0b820e8d7b74c0e24285a15a63ed Mon Sep 17 00:00:00 2001 From: chenquanzhen Date: Mon, 15 Mar 2021 15:38:11 +0800 Subject: [PATCH] resolve conflict for mysql 8.0 --- .../das/core/datasource/SingleDataSource.java | 4 - ...aultConnectionPhantomReferenceCleaner.java | 77 ------------------- 2 files changed, 81 deletions(-) delete mode 100644 das-client/src/main/java/com/ppdai/das/core/helper/DefaultConnectionPhantomReferenceCleaner.java diff --git a/das-client/src/main/java/com/ppdai/das/core/datasource/SingleDataSource.java b/das-client/src/main/java/com/ppdai/das/core/datasource/SingleDataSource.java index 496d257..189ab0d 100644 --- a/das-client/src/main/java/com/ppdai/das/core/datasource/SingleDataSource.java +++ b/das-client/src/main/java/com/ppdai/das/core/datasource/SingleDataSource.java @@ -22,8 +22,6 @@ import com.ppdai.das.core.configure.DataSourceConfigure; import com.ppdai.das.core.configure.DataSourceConfigureConstants; import com.ppdai.das.core.datasource.tomcat.DalTomcatDataSource; -import com.ppdai.das.core.helper.ConnectionPhantomReferenceCleaner; -import com.ppdai.das.core.helper.DefaultConnectionPhantomReferenceCleaner; import com.ppdai.das.core.helper.PoolPropertiesHelper; import com.ppdai.das.core.helper.ServiceLoaderHelper; import com.ppdai.das.core.log.Callback; @@ -41,7 +39,6 @@ public class SingleDataSource implements DataSourceConfigureConstants { private static final String DATASOURCE_CREATE_DATASOURCE = "DataSource::createDataSource:%s"; private static ILogger ilogger = ServiceLoaderHelper.getInstance(ILogger.class, DefaultLoggerImpl.class); - private static ConnectionPhantomReferenceCleaner connectionPhantomReferenceCleaner = new DefaultConnectionPhantomReferenceCleaner(); private static AtomicBoolean containsMySQL=new AtomicBoolean(false); private static final String MYSQL_URL_PREFIX = "jdbc:mysql://"; public static final String JMX_TOMCAT_DATASOURCE = "TomcatDataSource"; @@ -90,7 +87,6 @@ public void execute() throws Exception { try { if (!containsMySQL.get()) { if (dataSourceConfigure.getConnectionUrl().startsWith(MYSQL_URL_PREFIX)){ - connectionPhantomReferenceCleaner.start(); containsMySQL.set(true); } } diff --git a/das-client/src/main/java/com/ppdai/das/core/helper/DefaultConnectionPhantomReferenceCleaner.java b/das-client/src/main/java/com/ppdai/das/core/helper/DefaultConnectionPhantomReferenceCleaner.java deleted file mode 100644 index b0eda08..0000000 --- a/das-client/src/main/java/com/ppdai/das/core/helper/DefaultConnectionPhantomReferenceCleaner.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.ppdai.das.core.helper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; - -/** - * Created by lilj on 2018/4/12. - */ -public class DefaultConnectionPhantomReferenceCleaner implements ConnectionPhantomReferenceCleaner { - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultConnectionPhantomReferenceCleaner.class); - private static AtomicReference defaultConnectionPhantomReferenceCleanerRef = new AtomicReference<>(); - private static final Integer DEFAULT_INTERVAL = 900; - private static String driverClassName = "com.mysql.jdbc.NonRegisteringDriver"; - private static String fieldName = "connectionPhantomRefs"; - private static Class nonRegisteringDriver; - private static Field connectionPhantomReference; - private static AtomicBoolean started = new AtomicBoolean(false); - - static { - Runtime.getRuntime().addShutdownHook(new CustomThreadFactory("DefaultConnectionPhantomReferenceCleaner").newThread(new Runnable() { - @Override - public void run() { - shutdown(); - } - })); - } - - @Override - public void start() throws Exception { - if (started.getAndSet(true)) { - return; - } - try { - nonRegisteringDriver = Class.forName(driverClassName); - connectionPhantomReference = nonRegisteringDriver.getDeclaredField(fieldName); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(connectionPhantomReference, connectionPhantomReference.getModifiers() & ~Modifier.FINAL); - connectionPhantomReference.setAccessible(true); - } catch (ClassNotFoundException e) { - LOGGER.error(String.format("Class %s not found", driverClassName), e); - started.compareAndSet(true,false); - return; - } - ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, new CustomThreadFactory("DefaultConnectionPhantomReferenceCleaner")); - executor.scheduleWithFixedDelay(new ConnectionPhantomReferenceCleanUpThread(), DEFAULT_INTERVAL, DEFAULT_INTERVAL, TimeUnit.SECONDS); - defaultConnectionPhantomReferenceCleanerRef.set(executor); - } - - - private static class ConnectionPhantomReferenceCleanUpThread implements Runnable { - @Override - public void run() { - try { - ((ConcurrentHashMap) connectionPhantomReference.get(nonRegisteringDriver)).clear(); - LOGGER.info(String.format("ConnectionPhantomReference cleaned.")); - } catch (Exception ex) { - LOGGER.warn(String.format("Cleaning ConnectionPhantomReference Error"), ex.getMessage()); - } - } - } - - - private static void shutdown() { - if (defaultConnectionPhantomReferenceCleanerRef.get() == null) { - return; - } - defaultConnectionPhantomReferenceCleanerRef.get().shutdown(); - defaultConnectionPhantomReferenceCleanerRef.set(null); - started.compareAndSet(true,false); - } -}