Search in sources :

Example 1 with DBHostConfig

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;
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 2 with DBHostConfig

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;
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 3 with DBHostConfig

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;
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) NetworkChannel(java.nio.channels.NetworkChannel) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) NIOConnector(io.mycat.net.NIOConnector) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with DBHostConfig

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;
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) NetworkChannel(java.nio.channels.NetworkChannel) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) NIOConnector(io.mycat.net.NIOConnector) InetSocketAddress(java.net.InetSocketAddress)

Example 5 with DBHostConfig

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;
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) ConfigException(io.mycat.config.util.ConfigException) URI(java.net.URI) IOException(java.io.IOException) ConfigException(io.mycat.config.util.ConfigException)

Aggregations

DBHostConfig (io.mycat.config.model.DBHostConfig)14 Connection (java.sql.Connection)7 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)6 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)6 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)4 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)3 DataHostConfig (io.mycat.config.model.DataHostConfig)2 ConfigException (io.mycat.config.util.ConfigException)2 NIOConnector (io.mycat.net.NIOConnector)2 InetSocketAddress (java.net.InetSocketAddress)2 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)2 NetworkChannel (java.nio.channels.NetworkChannel)2 Statement (java.sql.Statement)2 DataNodeConfig (io.mycat.config.model.DataNodeConfig)1 NIOProcessor (io.mycat.net.NIOProcessor)1 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1