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);
}
}
}
}
Aggregations