use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.
the class JdbcDatasourcePoolImpl method getAvailableNumber.
@Override
public Integer getAvailableNumber() {
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
JdbcDataSource jdbcDataSource = jdbcConnectionManager.getDatasourceInfo().get(targetName);
return jdbcDataSource.getMaxCon() - jdbcDataSource.getUsedCount();
}
use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.
the class TestMySQLManagerImpl method getWriteableConnectionMap.
@Override
@SneakyThrows
public Map<String, java.sql.Connection> getWriteableConnectionMap() {
ConcurrentHashMap.KeySetView<String, DruidDataSource> strings = nameMap.keySet();
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
HashMap<String, Connection> map = new HashMap<>();
for (String string : strings) {
JdbcDataSource jdbcDataSource = datasourceInfo.get(string);
DatasourceConfig config = jdbcDataSource.getConfig();
if (jdbcDataSource.isMySQLType()) {
if (InstanceType.valueOf(Optional.ofNullable(config.getInstanceType()).orElse("READ_WRITE").toUpperCase()).isWriteType()) {
Connection connection = jdbcDataSource.getDataSource().getConnection();
map.put(string, connection);
}
}
}
return map;
}
use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.
the class SeataMySQLXADatasourceProvider method createDataSource.
@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
String username = config.getUser();
String password = config.getPassword();
String url = Objects.requireNonNull(config.getUrl());
String dbType = config.getDbType();
int maxRetryCount = config.getMaxRetryCount();
List<String> initSQLs = config.getInitSqls();
int maxCon = config.getMaxCon();
int minCon = config.getMinCon();
MysqlXADataSource datasource = new MysqlXADataSource();
datasource.setPassword(password);
datasource.setUser(username);
datasource.setUrl(url);
return new JdbcDataSource(config, new DataSourceProxyXA(datasource));
}
use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.
the class DruidDatasourceProvider method createDataSource.
@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
if (System.getProperty("druid.mysql.usePingMethod") == null) {
System.setProperty("druid.mysql.usePingMethod", "false");
}
String username = config.getUser();
String password = config.getPassword();
String url = Objects.requireNonNull(config.getUrl());
String dbType = config.getDbType();
int maxRetryCount = config.getMaxRetryCount();
List<String> initSQLs = config.getInitSqls();
int maxCon = config.getMaxCon();
int minCon = config.getMinCon();
DruidDataSource datasource = new DruidDataSource();
datasource.setPassword(password);
datasource.setUsername(username);
datasource.setUrl(url);
datasource.setMaxWait(config.getMaxConnectTimeout());
datasource.setMaxActive(maxCon);
datasource.setMinIdle(minCon);
datasource.setKeepAlive(true);
// datasource.setTestOnReturn(true);
// datasource.setTestOnBorrow(true);
datasource.setValidationQuery("select 'x'");
datasource.setTestWhileIdle(true);
datasource.setQueryTimeout(config.getQueryTimeout());
if (maxRetryCount > 0) {
datasource.setConnectionErrorRetryAttempts(maxRetryCount);
}
if (dbType != null) {
datasource.setDbType(dbType);
}
if (initSQLs != null) {
datasource.setConnectionInitSqls(initSQLs);
}
return new JdbcDataSource(config, datasource);
}
use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.
the class PrototypeHandlerImpl method onJdbc.
private Optional<List<Object[]>> onJdbc(String statement) {
String datasourceDs = null;
if (MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
datasourceDs = replicaSelectorManager.getDatasourceNameByReplicaName(MetadataManager.getPrototype(), true, null);
} else {
datasourceDs = "prototypeDs";
}
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
if (datasourceInfo.containsKey(datasourceDs)) {
datasourceDs = null;
} else {
List<DatasourceConfig> configAsList = jdbcConnectionManager.getConfigAsList();
if (!configAsList.isEmpty()) {
datasourceDs = configAsList.get(0).getName();
} else {
datasourceDs = null;
}
}
if (datasourceDs == null) {
datasourceDs = datasourceInfo.values().stream().filter(i -> i.isMySQLType()).map(i -> i.getName()).findFirst().orElse(null);
}
if (datasourceDs == null) {
return Optional.empty();
}
try (DefaultConnection connection = jdbcConnectionManager.getConnection(datasourceDs)) {
Connection rawConnection = connection.getRawConnection();
Statement jdbcStatement1 = rawConnection.createStatement();
ResultSet resultSet = jdbcStatement1.executeQuery(statement);
int columnCount = resultSet.getMetaData().getColumnCount();
List<Object[]> res = new ArrayList<>();
while (resultSet.next()) {
Object[] objects = new Object[columnCount];
for (int i = 0; i < columnCount; i++) {
objects[i] = resultSet.getObject(i + 1);
}
res.add(objects);
}
return Optional.of(res);
} catch (Exception e) {
LOGGER.warn("", e);
}
return Optional.empty();
}
Aggregations