Skip to content

Commit

Permalink
修改mapperProxy单例 (alibaba#10162)
Browse files Browse the repository at this point in the history
  • Loading branch information
985492783 authored Mar 28, 2023
1 parent 57a3a9d commit 953fa9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public static MapperManager instance(boolean isDataSourceLogEnable) {
public void loadInitial() {
Collection<Mapper> mappers = NacosServiceLoader.load(Mapper.class);
for (Mapper mapper : mappers) {
Map<String, Mapper> mapperMap = MAPPER_SPI_MAP.getOrDefault(mapper.getDataSource(), new HashMap<>(16));
Map<String, Mapper> mapperMap = MAPPER_SPI_MAP.computeIfAbsent(mapper.getDataSource(), (r) -> new HashMap<>(16));
mapperMap.put(mapper.getTableName(), mapper);
MAPPER_SPI_MAP.put(mapper.getDataSource(), mapperMap);
LOGGER.info("[MapperManager] Load Mapper({}) datasource({}) tableName({}) successfully.",
mapper.getClass(), mapper.getDataSource(), mapper.getTableName());
}
Expand Down Expand Up @@ -112,9 +111,8 @@ public <R extends Mapper> R findMapper(String dataSource, String tableName) {
"[MapperManager] Failed to find the table ,tableName:" + tableName);
}
if (dataSourceLogEnable) {
MapperProxy mapperProxy = new MapperProxy();
return (R) mapperProxy.createProxy(mapper);
return (R) MapperProxy.createSingleProxy(mapper);
}
return (R) mapper;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* DataSource plugin Mapper sql proxy.
Expand All @@ -36,11 +40,39 @@ public class MapperProxy implements InvocationHandler {

private Mapper mapper;

private static final Map<String, MapperProxy> SINGLE_MAPPER_PROXY_MAP = new HashMap<>(16);

private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(true);

public <R> R createProxy(Mapper mapper) {
this.mapper = mapper;
return (R) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), mapper.getClass().getInterfaces(), this);
}

/**
* create proxy-mapper single instead of using method createProxy.
*/
public static <R> R createSingleProxy(Mapper mapper) {
String key = mapper.getClass().getSimpleName();
if (!SINGLE_MAPPER_PROXY_MAP.containsKey(key)) {
try {
LOCK.writeLock().lock();
if (!SINGLE_MAPPER_PROXY_MAP.containsKey(key)) {
MapperProxy mapperProxy = new MapperProxy();
SINGLE_MAPPER_PROXY_MAP.put(key, mapperProxy.createProxy(mapper));
}
} finally {
LOCK.writeLock().unlock();
}
}
try {
LOCK.readLock().lock();
return (R) SINGLE_MAPPER_PROXY_MAP.get(key);
} finally {
LOCK.readLock().unlock();
}
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object invoke = method.invoke(mapper, args);
Expand Down

0 comments on commit 953fa9e

Please sign in to comment.