Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
{
"name": "close",
"parameterTypes": []
},
{
"name": "getHikariPoolMXBean",
"parameterTypes": []
}
]
},
{
"condition": {
"typeReached": "com.baomidou.dynamic.datasource.destroyer.HikariDataSourceActiveDetector"
},
"type": "com.zaxxer.hikari.pool.HikariPool",
"methods": [
{
"name": "getActiveConnections",
"parameterTypes": []
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions dynamic-datasource-spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// MasterSlaveAutoRoutingPlugin 依赖 org.apache.ibatis(主源经 compileOnly mybatis-plus 可见,
// 但 testCompile 不继承 compileOnly),故为插件单测重新引入;仅 test 作用域,不影响发布构件。
testImplementation(libs.p6spy)
testImplementation(libs.seata.rm.datasource) {
exclude group: "com.alibaba", module: "druid"
}
testImplementation(libs.mybatis.plus)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
import com.baomidou.dynamic.datasource.strategy.LoadBalanceDynamicDataSourceStrategy;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.p6spy.engine.spy.P6DataSource;
import io.seata.rm.datasource.DataSourceProxy;
import com.baomidou.dynamic.datasource.unwrapper.DataSourceUnwrapperChain;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -65,17 +64,37 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource implemen
private String primary = "master";
@Setter
private Boolean strict = false;
@Setter
private Boolean p6spy = false;
@Setter
private Boolean seata = false;
@Setter
private Boolean graceDestroy = false;
private DataSourceUnwrapperChain dataSourceUnwrapperChain;

public DynamicRoutingDataSource(List<DynamicDataSourceProvider> providers) {
this.providers = providers;
}

public synchronized void setP6spy(Boolean p6spy) {
if (!Objects.equals(this.p6spy, p6spy)) {
this.p6spy = p6spy;
this.dataSourceUnwrapperChain = null;
}
}

public synchronized void setSeata(Boolean seata) {
if (!Objects.equals(this.seata, seata)) {
this.seata = seata;
this.dataSourceUnwrapperChain = null;
}
}

private synchronized DataSourceUnwrapperChain getDataSourceUnwrapperChain() {
if (dataSourceUnwrapperChain == null) {
dataSourceUnwrapperChain = new DataSourceUnwrapperChain(p6spy, seata);
}
return dataSourceUnwrapperChain;
}

@Override
protected String getPrimary() {
return primary;
Expand Down Expand Up @@ -289,19 +308,7 @@ private void closeDataSource(String ds, DataSource dataSource, boolean graceDest
if (dataSource instanceof ItemDataSource) {
realDataSource = ((ItemDataSource) dataSource).getRealDataSource();
} else {
if (seata) {
if (dataSource instanceof DataSourceProxy) {
DataSourceProxy dataSourceProxy = (DataSourceProxy) dataSource;
realDataSource = dataSourceProxy.getTargetDataSource();
}
}
if (p6spy) {
if (dataSource instanceof P6DataSource) {
Field realDataSourceField = P6DataSource.class.getDeclaredField("realDataSource");
realDataSourceField.setAccessible(true);
realDataSource = (DataSource) realDataSourceField.get(dataSource);
}
}
realDataSource = getDataSourceUnwrapperChain().unwrap(dataSource);
}

if (null == realDataSource) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.unwrapper;

import org.springframework.core.Ordered;

import javax.sql.DataSource;

/**
* Unwraps one layer of a datasource proxy without opening a connection.
* <p>
* Register implementations in {@code META-INF/services/} using this interface's
* fully qualified name. Lower order values run first. Implementations must be
* safe to load when their optional integration is absent. Providers must have
* a public no-argument constructor and be visible to the thread context
* classloader used when the chain is first created (or Spring's defining
* classloader if no context classloader is set). Instances are reused by a
* routing datasource and must support concurrent calls without retaining
* per-datasource state.
*
* @author Pika-Yalei
* @since 4.6.0
*/
public interface DataSourceUnwrapper extends Ordered {

boolean support(DataSource dataSource);

/**
* @return the wrapped datasource, or the input (or {@code null}) if unchanged
* @throws Exception if the supported proxy cannot be unwrapped
*/
DataSource unwrap(DataSource dataSource) throws Exception;

@Override
default int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.unwrapper;

import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ClassUtils;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

/**
* Ordered datasource unwrappers, shared by lifecycle and diagnostic consumers.
*
* @author Pika-Yalei
* @since 4.6.0
*/
public class DataSourceUnwrapperChain {

private final List<DataSourceUnwrapper> unwrappers = new ArrayList<>();

/**
* Retains the routing datasource's opt-in flags for the built-in integrations.
* Service providers are independent of those flags and are discovered once
* per chain using Spring's default classloader policy: the thread context
* classloader first, falling back to Spring's defining classloader. This
* preserves application-provider discovery in executable jars and child
* application classloaders.
*/
public DataSourceUnwrapperChain(boolean p6spy, boolean seata) {
if (seata) {
unwrappers.add(new SeataDataSourceUnwrapper());
}
if (p6spy) {
unwrappers.add(new P6SpyDataSourceUnwrapper());
}
try {
for (DataSourceUnwrapper unwrapper : ServiceLoader.load(
DataSourceUnwrapper.class, ClassUtils.getDefaultClassLoader())) {
unwrappers.add(unwrapper);
}
} catch (ServiceConfigurationError e) {
throw new IllegalStateException("Cannot load datasource unwrappers", e);
}
AnnotationAwareOrderComparator.sort(unwrappers);
}

/**
* Returns the first successfully unwrapped target, or the input unchanged.
* Only one layer is removed, preserving the routing datasource's lifecycle
* behavior for nested proxies. Failures propagate to the caller for logging.
*/
public DataSource unwrap(DataSource dataSource) throws Exception {
if (dataSource != null) {
for (DataSourceUnwrapper unwrapper : unwrappers) {
if (unwrapper.support(dataSource)) {
DataSource target = unwrapper.unwrap(dataSource);
if (target != null && target != dataSource) {
return target;
}
}
}
}
return dataSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.unwrapper;

import com.p6spy.engine.spy.P6DataSource;
import org.springframework.util.ClassUtils;

import javax.sql.DataSource;
import java.lang.reflect.Field;

/**
* Optional P6Spy integration. The proxy has no public target accessor.
*
* @author Pika-Yalei
* @since 4.6.0
*/
public class P6SpyDataSourceUnwrapper implements DataSourceUnwrapper {

private static final boolean PRESENT = ClassUtils.isPresent(
"com.p6spy.engine.spy.P6DataSource", P6SpyDataSourceUnwrapper.class.getClassLoader());

@Override
public boolean support(DataSource dataSource) {
return PRESENT && dataSource instanceof P6DataSource;
}

@Override
public DataSource unwrap(DataSource dataSource) throws ReflectiveOperationException {
Field field = P6DataSource.class.getDeclaredField("realDataSource");
field.setAccessible(true);
return (DataSource) field.get(dataSource);
}

@Override
public int getOrder() {
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.unwrapper;

import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.util.ClassUtils;

import javax.sql.DataSource;

/**
* Optional Seata AT datasource proxy integration.
*
* @author Pika-Yalei
* @since 4.6.0
*/
public class SeataDataSourceUnwrapper implements DataSourceUnwrapper {

private static final boolean PRESENT = ClassUtils.isPresent(
"io.seata.rm.datasource.DataSourceProxy", SeataDataSourceUnwrapper.class.getClassLoader());

@Override
public boolean support(DataSource dataSource) {
return PRESENT && dataSource instanceof DataSourceProxy;
}

@Override
public DataSource unwrap(DataSource dataSource) {
return ((DataSourceProxy) dataSource).getTargetDataSource();
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.unwrapper;

import javax.sql.DataSource;

/** Public close method exercised by the production destroyer. */
public interface CloseableDataSource extends DataSource, AutoCloseable {

@Override
void close();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.unwrapper;

/** Registered only by a temporary service descriptor in the context classloader test. */
public class ContextClassLoaderDataSourceUnwrapper extends TestDataSourceUnwrapper {
}
Loading
Loading