com.ecfront.easybi.dbutils.inner.DSLoader Maven / Gradle / Ivy
package com.ecfront.easybi.dbutils.inner;
import com.alibaba.druid.pool.DruidDataSource;
import com.ecfront.easybi.dbutils.inner.dialect.Dialect;
import com.ecfront.easybi.dbutils.inner.dialect.DialectFactory;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DSLoader {
private static final Map MULTI_DS = new HashMap();
private static final Map MULTI_DB_DIALECT = new HashMap();
private static final Map MULTI_DS_ENTITY = new HashMap();
public static void reload() {
MULTI_DS.clear();
MULTI_DB_DIALECT.clear();
MULTI_DS_ENTITY.clear();
loadMainDS();
if (ConfigContainer.MULTI_DS_SUPPORT) {
loadMultiDS();
}
}
public static ConnectionWrap getConnection(String dsCode) {
ConnectionWrap cw = new ConnectionWrap();
DSEntity dsEntity = MULTI_DS_ENTITY.get(dsCode);
cw.type = DialectFactory.getDialectType(dsEntity.driver);
try {
if (dsEntity.poolSupport) {
cw.conn = MULTI_DS.get(dsCode).getConnection();
if (cw.conn.isClosed()) {
//Re-setting connection when connection was close.
synchronized (DSLoader.class) {
logger.warn("Connection info:" + cw.conn.toString() + " was close.");
loadPool(dsEntity);
cw.conn = MULTI_DS.get(dsCode).getConnection();
}
}
} else {
Class.forName(dsEntity.driver).newInstance();
cw.conn = DriverManager.getConnection(dsEntity.url, dsEntity.userName, dsEntity.password);
if (null == cw.conn) {
logger.error("Connection can't create.");
}
}
} catch (Exception e) {
logger.error("Connection get error.", e);
}
return cw;
}
public static Dialect getDialect(String dsCode) {
return MULTI_DB_DIALECT.get(dsCode);
}
private static void loadMultiDS() {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy