package foundation.persist.source;
|
|
import java.sql.Connection;
|
import java.sql.SQLException;
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
import foundation.server.config.DBaseType;
|
|
public class NamedDataSource extends DruidDataSource implements IJSONProvider {
|
|
private static final long serialVersionUID = 1L;
|
private String name;
|
private DBaseType dbaseType;
|
|
public NamedDataSource(String name) throws SQLException {
|
this.name = name;
|
|
setInitialSize(1);
|
setFilters("stat,log4j2");
|
setMaxWait(60000);
|
setMinIdle(1);
|
setTimeBetweenEvictionRunsMillis(3000);
|
setMinEvictableIdleTimeMillis(3000 * 500);
|
setRemoveAbandonedTimeout(300);
|
setTestWhileIdle(true);
|
setTestOnBorrow(false);
|
setTestOnReturn(false);
|
|
dbaseType = DBaseType.Unknown;
|
}
|
|
public static NamedDataSource getInstance() {
|
return DataSourceManager.getMain();
|
}
|
|
public static NamedDataSource getInstance(String name) {
|
NamedDataSource result = DataSourceManager.getDataSource(name);
|
return result;
|
}
|
|
@Override
|
public void setUrl(String url) {
|
super.setUrl(url);
|
|
if (url == null) {
|
return;
|
}
|
|
url = url.toLowerCase();
|
if (url.indexOf("oracle") > 0) {
|
dbaseType = DBaseType.Oracle;
|
setValidationQuery("select 1 from dual");
|
}
|
else if (url.indexOf("mysql") > 0) {
|
dbaseType = DBaseType.MySQL;
|
setValidationQuery("select 1");
|
}
|
else if (url.indexOf("sqlserver") > 0) {
|
dbaseType = DBaseType.SQLServer;
|
setValidationQuery("select 1");
|
}
|
else {
|
dbaseType = DBaseType.Unknown;
|
setValidationQuery("select 1");
|
}
|
}
|
|
public ConnectionAgent getConnectionAgent() throws SQLException {
|
Connection conn = getConnection();
|
return new ConnectionAgent(name, conn, dbaseType);
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public DBaseType getDBaseType() {
|
return dbaseType;
|
}
|
|
public boolean keyEquals(String sourceKey) {
|
return this.name.equalsIgnoreCase(sourceKey);
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginArray();
|
|
writer.write("name", getName());
|
writer.write("dbType", getDbType());
|
|
writer.write("connectCount", getConnectCount());
|
writer.write("activeCount", getActiveCount());
|
writer.write("closeCount", getCloseCount());
|
writer.write("destroyCount", getDestroyCount());
|
writer.write("scardCount", getDiscardCount());
|
writer.write("activePeak", getActivePeak());
|
writer.write("createTime", getCreatedTime());
|
|
writer.endArray();
|
}
|
}
|