Search in sources :

Example 41 with DefaultConnection

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);
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) GlobalStatus(io.seata.core.model.GlobalStatus) Map(java.util.Map) SneakyThrows(lombok.SneakyThrows)

Example 42 with DefaultConnection

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);
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Map(java.util.Map) SneakyThrows(lombok.SneakyThrows)

Example 43 with DefaultConnection

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);
            }
        }
    });
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection)

Example 44 with DefaultConnection

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()));
        }
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) SQLCreateViewStatement(com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ResultSet(java.sql.ResultSet) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) SneakyThrows(lombok.SneakyThrows)

Example 45 with DefaultConnection

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();
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) Statement(java.sql.Statement) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DatasourceConfig(io.mycat.config.DatasourceConfig) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Aggregations

DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)53 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)39 SneakyThrows (lombok.SneakyThrows)23 Connection (java.sql.Connection)20 MetadataManager (io.mycat.MetadataManager)6 java.util (java.util)6 SQLException (java.sql.SQLException)5 Statement (java.sql.Statement)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Collectors (java.util.stream.Collectors)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)4 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)4 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)4 NameMap (io.mycat.util.NameMap)4 NotNull (org.jetbrains.annotations.NotNull)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3