|
@@ -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) {
|
|
|
|
|
+ }
|
|
|
|
|
+}
|