use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class SeataTransactionSession method begin.
@Override
@SneakyThrows
public void begin() {
if (tx == null) {
tx = GlobalTransactionContext.createNew();
}
String xid = RootContext.getXID();
if (xid == null) {
GlobalStatus localStatus = tx.getLocalStatus();
switch(localStatus) {
case Rollbacked:
case Committed:
case Finished:
case UnKnown:
for (Map.Entry<String, DefaultConnection> e : updateConnectionMap.entrySet()) {
e.getValue().close();
}
updateConnectionMap.clear();
tx.begin();
mycatXid = tx.getXid();
break;
case Begin:
case Committing:
case CommitRetrying:
case Rollbacking:
case RollbackRetrying:
case TimeoutRollbacking:
case TimeoutRollbackRetrying:
case AsyncCommitting:
case CommitFailed:
case RollbackFailed:
case TimeoutRollbacked:
case TimeoutRollbackFailed:
default:
}
}
dataContext.setInTransaction(true);
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class SeataTransactionSession method commit.
@Override
@SneakyThrows
public void commit() {
for (Map.Entry<String, DefaultConnection> e : updateConnectionMap.entrySet()) {
DefaultConnection value = e.getValue();
value.getRawConnection().commit();
}
tx.commit();
for (Map.Entry<String, DefaultConnection> e : updateConnectionMap.entrySet()) {
DefaultConnection value = e.getValue();
value.close();
}
updateConnectionMap.clear();
tx = null;
dataContext.setInTransaction(false);
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class CreateTableUtils method createPhysicalTable.
public static void createPhysicalTable(JdbcConnectionManager jdbcConnectionManager, Partition node, String createSQL) {
ReplicaSelectorManager selectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
Set<String> set = new HashSet<>();
if (selectorRuntime.isDatasource(node.getTargetName())) {
set.add(node.getTargetName());
}
if (selectorRuntime.isReplicaName(node.getTargetName())) {
set.addAll(selectorRuntime.getReplicaMap().get(node.getTargetName()).getWriteDataSourceByReplicaType().stream().map(i -> i.getName()).collect(Collectors.toList()));
}
if (set.isEmpty()) {
throw new IllegalArgumentException("can not found " + node.getTargetName());
}
normalizeCreateTableSQLToMySQL(createSQL).ifPresent(sql -> {
for (String s : set) {
try (DefaultConnection connection = jdbcConnectionManager.getConnection(s)) {
Connection rawConnection = connection.getRawConnection();
String backupSchema = null;
try {
backupSchema = JdbcUtils.executeQuery(rawConnection, "select database()", Collections.emptyList()).get(0).values().iterator().next().toString();
} catch (Exception e) {
LOGGER.error("", e);
}
if (InstanceType.valueOf(connection.getDataSource().getConfig().getInstanceType()).isWriteType()) {
if (!rawConnection.isReadOnly()) {
connection.createDatabase(node.getSchema());
JdbcUtils.execute(rawConnection, "use `" + node.getSchema() + "`");
connection.createTable(rewriteCreateTableSql(sql, node.getSchema(), node.getTable()));
if (backupSchema != null) {
JdbcUtils.execute(rawConnection, "use `" + backupSchema + "`");
}
}
}
} catch (Throwable throwable) {
LOGGER.error("", throwable);
}
}
});
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class SQL2ResultSetUtil method getMycatRowMetaData.
@SneakyThrows
public static MycatRowMetaData getMycatRowMetaData(JdbcConnectionManager jdbcConnectionManager, String prototypeServer, String schema, String table) {
try (DefaultConnection connection = jdbcConnectionManager.getConnection(prototypeServer)) {
Connection rawConnection = connection.getRawConnection();
try (Statement statement = rawConnection.createStatement()) {
statement.setMaxRows(0);
ResultSet resultSet = statement.executeQuery("select * from " + schema + "." + table + " where 0");
resultSet.next();
return new CopyMycatRowMetaData(new JdbcRowMetaData(resultSet.getMetaData()));
}
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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