use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
the class JDBCDatasource method getConnection.
Connection getConnection() throws SQLException {
DBHostConfig cfg = getConfig();
Connection connection = DriverManager.getConnection(cfg.getUrl(), cfg.getUser(), cfg.getPassword());
String initSql = getHostConfig().getConnectionInitSql();
if (initSql != null && !"".equals(initSql)) {
Statement statement = null;
try {
statement = connection.createStatement();
statement.execute(initSql);
} finally {
if (statement != null) {
statement.close();
}
}
}
return connection;
}
use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
the class JDBCDatasource method testConnection.
@Override
public boolean testConnection(String schema) throws IOException {
boolean isConnected = false;
Connection connection = null;
Statement statement = null;
try {
DBHostConfig cfg = getConfig();
connection = DriverManager.getConnection(cfg.getUrl(), cfg.getUser(), cfg.getPassword());
statement = connection.createStatement();
if (connection != null && statement != null) {
isConnected = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
return isConnected;
}
use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
the class PostgreSQLBackendConnectionFactory method make.
@SuppressWarnings({ "unchecked", "rawtypes" })
public PostgreSQLBackendConnection make(PostgreSQLDataSource pool, ResponseHandler handler, final String schema) throws IOException {
final DBHostConfig dsc = pool.getConfig();
NetworkChannel channel = this.openSocketChannel(MycatServer.getInstance().isAIO());
final PostgreSQLBackendConnection c = new PostgreSQLBackendConnection(channel, pool.isReadNode());
MycatServer.getInstance().getConfig().setSocketParams(c, false);
// 设置NIOHandler
c.setHandler(new PostgreSQLBackendConnectionHandler(c));
c.setHost(dsc.getIp());
c.setPort(dsc.getPort());
c.setUser(dsc.getUser());
c.setPassword(dsc.getPassword());
c.setSchema(schema);
c.setPool(pool);
c.setResponseHandler(handler);
c.setIdleTimeout(pool.getConfig().getIdleTimeout());
if (channel instanceof AsynchronousSocketChannel) {
((AsynchronousSocketChannel) channel).connect(new InetSocketAddress(dsc.getIp(), dsc.getPort()), c, (CompletionHandler) MycatServer.getInstance().getConnector());
} else {
((NIOConnector) MycatServer.getInstance().getConnector()).postConnect(c);
}
return c;
}
use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
the class MySQLConnectionFactory method make.
@SuppressWarnings({ "unchecked", "rawtypes" })
public MySQLConnection make(MySQLDataSource pool, ResponseHandler handler, String schema) throws IOException {
DBHostConfig dsc = pool.getConfig();
NetworkChannel channel = openSocketChannel(MycatServer.getInstance().isAIO());
MySQLConnection c = new MySQLConnection(channel, pool.isReadNode());
MycatServer.getInstance().getConfig().setSocketParams(c, false);
c.setHost(dsc.getIp());
c.setPort(dsc.getPort());
c.setUser(dsc.getUser());
c.setPassword(dsc.getPassword());
c.setSchema(schema);
c.setHandler(new MySQLConnectionAuthenticator(c, handler));
c.setPool(pool);
c.setIdleTimeout(pool.getConfig().getIdleTimeout());
if (channel instanceof AsynchronousSocketChannel) {
((AsynchronousSocketChannel) channel).connect(new InetSocketAddress(dsc.getIp(), dsc.getPort()), c, (CompletionHandler) MycatServer.getInstance().getConnector());
} else {
((NIOConnector) MycatServer.getInstance().getConnector()).postConnect(c);
}
return c;
}
use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
the class XMLSchemaLoader method createDBHostConf.
private DBHostConfig createDBHostConf(String dataHost, Element node, String dbType, String dbDriver, int maxCon, int minCon, String filters, long logTime) {
String nodeHost = node.getAttribute("host");
String nodeUrl = node.getAttribute("url");
String user = node.getAttribute("user");
String password = node.getAttribute("password");
String usingDecrypt = node.getAttribute("usingDecrypt");
String passwordEncryty = DecryptUtil.DBHostDecrypt(usingDecrypt, nodeHost, user, password);
String weightStr = node.getAttribute("weight");
int weight = "".equals(weightStr) ? PhysicalDBPool.WEIGHT : Integer.parseInt(weightStr);
String ip = null;
int port = 0;
if (empty(nodeHost) || empty(nodeUrl) || empty(user)) {
throw new ConfigException("dataHost " + dataHost + " define error,some attributes of this element is empty: " + nodeHost);
}
if ("native".equalsIgnoreCase(dbDriver)) {
int colonIndex = nodeUrl.indexOf(':');
ip = nodeUrl.substring(0, colonIndex).trim();
port = Integer.parseInt(nodeUrl.substring(colonIndex + 1).trim());
} else {
URI url;
try {
url = new URI(nodeUrl.substring(5));
} catch (Exception e) {
throw new ConfigException("invalid jdbc url " + nodeUrl + " of " + dataHost);
}
ip = url.getHost();
port = url.getPort();
}
DBHostConfig conf = new DBHostConfig(nodeHost, ip, port, nodeUrl, user, passwordEncryty, password);
conf.setDbType(dbType);
conf.setMaxCon(maxCon);
conf.setMinCon(minCon);
conf.setFilters(filters);
conf.setLogTime(logTime);
//新增权重
conf.setWeight(weight);
return conf;
}
Aggregations