Search in sources :

Example 31 with DefaultConnection

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

the class SpmTest method testCheckStore.

@Test
@SneakyThrows
public void testCheckStore() {
    try (DefaultConnection defaultConnection = jdbcManager.getConnection("prototypeDs")) {
        Connection rawConnection = defaultConnection.getRawConnection();
        JdbcUtils.execute(rawConnection, "DROP TABLE IF EXISTS mycat.spm_baseline");
        JdbcUtils.execute(rawConnection, "DROP TABLE IF EXISTS mycat.spm_plan");
        dbPlanManagerPersistor.checkStore();
        JdbcUtils.executeQuery(rawConnection, "SELECT * FROM mycat.spm_baseline", Collections.emptyList());
        JdbcUtils.executeQuery(rawConnection, "SELECT * FROM mycat.spm_plan", Collections.emptyList());
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Test(org.junit.Test) SneakyThrows(lombok.SneakyThrows)

Example 32 with DefaultConnection

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

the class TransactionSessionTemplate method closeStatementState.

@Override
@SneakyThrows
public Future<Void> closeStatementState() {
    try {
        if (!isInTransaction()) {
            Set<Map.Entry<String, DefaultConnection>> entries = updateConnectionMap.entrySet();
            for (Map.Entry<String, DefaultConnection> entry : entries) {
                DefaultConnection value = entry.getValue();
                if (value != null) {
                    value.close();
                }
            }
            updateConnectionMap.clear();
            dataSourceNearness.clear();
        }
        Iterator<AutoCloseable> iterator = closeResourceQueue.iterator();
        while (iterator.hasNext()) {
            iterator.next().close();
            iterator.remove();
        }
    } catch (Throwable throwable) {
        return Future.failedFuture(throwable);
    }
    return Future.succeededFuture();
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SneakyThrows(lombok.SneakyThrows)

Example 33 with DefaultConnection

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

the class TransactionSessionTemplate method setAutocommit.

@SneakyThrows
public void setAutocommit(boolean autocommit) {
    for (DefaultConnection c : updateConnectionMap.values()) {
        c.getRawConnection().setAutoCommit(autocommit);
    }
    if (autocommit) {
        for (DefaultConnection value : updateConnectionMap.values()) {
            value.close();
        }
        updateConnectionMap.clear();
        setInTranscation(false);
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) SneakyThrows(lombok.SneakyThrows)

Example 34 with DefaultConnection

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

the class UnsolvedMysqlFunctionUtil method eval.

@SneakyThrows
public static Object eval(String fun, Object... args) {
    ArrayList<String> p = new ArrayList<>(args.length);
    for (Object arg : args) {
        if (arg == null) {
            p.add("null");
        } else if (arg instanceof String) {
            p.add("'" + arg + "'");
        } else {
            p.add(Objects.toString(arg));
        }
    }
    String sql = "select " + fun + "(" + String.join(",", p) + ")";
    return objectCache.get(sql, () -> {
        ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
        JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
        String datasource = replicaSelectorRuntime.getDatasourceNameByRandom();
        try (DefaultConnection connection = jdbcConnectionManager.getConnection(datasource)) {
            RowBaseIterator rowBaseIterator = connection.executeQuery(sql);
            rowBaseIterator.next();
            return rowBaseIterator.getObject(1);
        }
    });
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ArrayList(java.util.ArrayList) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) SneakyThrows(lombok.SneakyThrows)

Example 35 with DefaultConnection

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

the class MySQLLogConsumer method init.

private void init() throws SQLException {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
        JdbcUtils.execute(connection.getRawConnection(), "CREATE TABLE  IF NOT EXISTS mycat.`sql_log` (\n" + "  `instanceId` bigint(20) DEFAULT NULL,\n" + "  `user` varchar(64) DEFAULT NULL,\n" + "  `connectionId` bigint(20) DEFAULT NULL,\n" + "  `ip` varchar(22) DEFAULT NULL,\n" + "  `port` bigint(20) DEFAULT NULL,\n" + "  `traceId` varchar(22) NOT NULL,\n" + "  `hash` varchar(22) DEFAULT NULL,\n" + "  `sqlType` varchar(22) DEFAULT NULL,\n" + "  `sql` longtext,\n" + "  `transactionId` varchar(22) DEFAULT NULL,\n" + "  `sqlTime` bigint(20) DEFAULT NULL,\n" + "  `responseTime` datetime DEFAULT NULL,\n" + "  `affectRow` int(11) DEFAULT NULL,\n" + "  `result` tinyint(1) DEFAULT NULL,\n" + "  `externalMessage` tinytext,\n" + "  PRIMARY KEY (`traceId`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", Collections.emptyList());
    }
}
Also used : MetadataManager(io.mycat.MetadataManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) 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