yumeng 1 day ago
parent
commit
ed13566dbc

+ 60 - 0
jeecg-boot-material-view/src/main/java/org/jeecg/ctop/material/service/SimpleTiFlashInterceptor.java

@@ -0,0 +1,60 @@
+package org.jeecg.ctop.material.service;
+
+
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.*;
+        import org.springframework.stereotype.Component;
+import java.sql.Connection;
+import java.sql.Statement;
+import java.util.Properties;
+
+@Intercepts({
+        @Signature(type = Executor.class, method = "query",
+                args = {MappedStatement.class, Object.class,
+                        org.apache.ibatis.session.RowBounds.class,
+                        org.apache.ibatis.session.ResultHandler.class})
+})
+@Component
+public class SimpleTiFlashInterceptor implements Interceptor {
+
+    @Override
+    public Object intercept(Invocation invocation) throws Throwable {
+        try {
+            // 获取连接并设置TiFlash
+            Object target = invocation.getTarget();
+            if (target instanceof Executor) {
+                Executor executor = (Executor) target;
+
+                // 反射获取连接
+                java.lang.reflect.Field transactionField =
+                        executor.getClass().getDeclaredField("transaction");
+                transactionField.setAccessible(true);
+                Object transaction = transactionField.get(executor);
+
+                java.lang.reflect.Field connectionField =
+                        transaction.getClass().getDeclaredField("connection");
+                connectionField.setAccessible(true);
+                Connection connection = (Connection) connectionField.get(transaction);
+
+                // 执行设置语句
+                try (Statement stmt = connection.createStatement()) {
+                    stmt.execute("SET session tidb_isolation_read_engines = 'tiflash,tidb'");
+                }
+            }
+        } catch (Exception e) {
+            // 忽略异常,不中断正常流程
+        }
+
+        return invocation.proceed();
+    }
+
+    @Override
+    public Object plugin(Object target) {
+        return Plugin.wrap(target, this);
+    }
+
+    @Override
+    public void setProperties(Properties properties) {
+    }
+}

+ 0 - 2
jeecg-boot-material-view/src/main/resources/application-prod.yml

@@ -111,8 +111,6 @@ spring:
           url: jdbc:mysql://111.204.208.234:3394/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&allowMultiQueries=true&serverTimezone=GMT%2B8
           username: hcst
           password: hcst@2024
-          connection-init-sqls:
-            - SET session tidb_isolation_read_engines = 'tiflash,tidb'
           driver-class-name: com.mysql.jdbc.Driver