Search in sources :

Example 1 with MongoDriver

use of io.mycat.datasource.jdbc.mongodb.MongoDriver in project Mycat2 by MyCATApache.

the class JdbcConnectionManager method getConnection.

public DefaultConnection getConnection(String name, Boolean autocommit, int transactionIsolation) {
    name = resolveDataSource(name);
    final JdbcDataSource key = dataSourceMap.get(name);
    Objects.requireNonNull(key, "unknown target:" + name);
    DefaultConnection defaultConnection;
    Connection connection = null;
    switch(key.getDbType()) {
        default:
            {
                try {
                    DatasourceConfig config = key.getConfig();
                    connection = key.getDataSource().getConnection();
                    defaultConnection = new DefaultConnection(connection, key, autocommit, transactionIsolation, this);
                    LOGGER.debug("get connection:{} {}", name, defaultConnection);
                    if (config.isInitSqlsGetConnection()) {
                        if (config.getInitSqls() != null && !config.getInitSqls().isEmpty()) {
                            try (Statement statement = connection.createStatement()) {
                                for (String initSql : config.getInitSqls()) {
                                    statement.execute(initSql);
                                }
                            }
                        }
                    }
                    key.counter.getAndIncrement();
                    return defaultConnection;
                } catch (SQLException e) {
                    if (connection != null) {
                        JdbcUtils.close(connection);
                    }
                    LOGGER.debug("", e);
                    throw new MycatException(e);
                }
            }
        case "mongodb":
            {
                try {
                    Properties properties = new Properties();
                    String username = key.getUsername();
                    String password = key.getPassword();
                    if (username != null) {
                        properties.setProperty("username", username);
                    }
                    if (password != null) {
                        properties.setProperty("password", password);
                    }
                    Connection connect = new MongoDriver().connect(key.getUrl(), properties);
                    return new DefaultConnection(connect, key, autocommit, transactionIsolation, this);
                } catch (SQLException ex) {
                    throw new MycatException(ex);
                }
            }
    }
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) MycatException(io.mycat.MycatException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) MongoDriver(io.mycat.datasource.jdbc.mongodb.MongoDriver)

Aggregations

MycatException (io.mycat.MycatException)1 DatasourceConfig (io.mycat.config.DatasourceConfig)1 MongoDriver (io.mycat.datasource.jdbc.mongodb.MongoDriver)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1