cn.sylinx.hbatis.ext.ctxds.SPIManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-core Show documentation
Show all versions of hbatis-core Show documentation
hbatis is a simple orm framework
The newest version!
package cn.sylinx.hbatis.ext.ctxds;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.concurrent.locks.ReentrantLock;
import cn.sylinx.hbatis.kit.ClassUtil;
class SPIManager {
private static DynamicDatasourceProvider dynamicDatasourceProvider;
private static ReentrantLock lock = new ReentrantLock();
static DynamicDatasourceProvider getDynamicDatasourceProvider() {
if (dynamicDatasourceProvider != null) {
return dynamicDatasourceProvider;
}
// 加載DynamicDatasourceProvider
loadDynamicDatasourceProvider();
return dynamicDatasourceProvider;
}
private static void loadDynamicDatasourceProvider() {
try {
lock.lock();
if (dynamicDatasourceProvider == null) {
ServiceLoader sl = ServiceLoader.load(DynamicDatasourceProvider.class, ClassUtil.getDefaultClassLoader());
Iterator it = sl.iterator();
dynamicDatasourceProvider = it.hasNext() ? it.next() : null;
}
if (dynamicDatasourceProvider == null) {
dynamicDatasourceProvider = new DefaultDynamicDatasourceProvider();
}
} finally {
lock.unlock();
}
}
}