first commit
This commit is contained in:
@ -183,7 +183,7 @@ public class DynamicDataSourceConfig {
|
||||
dataSource.setInitialSize(dynamicDataSource.getInitialSize());
|
||||
dataSource.setMinIdle(dynamicDataSource.getMinIdle());
|
||||
dataSource.setMaxActive(dynamicDataSource.getMaxActive());
|
||||
dataSource.setKeepAlive(false);
|
||||
//dataSource.setKeepAlive(false);
|
||||
try {
|
||||
// 初始化数据源
|
||||
dataSource.init();
|
||||
|
@ -1,42 +0,0 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.utils.SqlUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* MysqlSchemaHandler
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:25
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MysqlSchemaHandler implements SchemaHandler {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String baseDatabase;
|
||||
private static final String MYSQL_CHANGE_SCHEMA_SQL = "alter session set current_schema = {}";
|
||||
|
||||
|
||||
@Override
|
||||
public void setSchema(Connection conn) {
|
||||
// eg: use qiaoba-1;
|
||||
String sql = StrUtil.format("use `{}-{}`;", baseDatabase, BaseContext.getTenantId());
|
||||
log.debug("Run MysqlSchemaHandler, Sql: {}", sql);
|
||||
try {
|
||||
SqlUtil.runSql(conn, sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* OracleSchemaHandler
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:25
|
||||
*/
|
||||
@Component
|
||||
public class OracleSchemaHandler implements SchemaHandler {
|
||||
|
||||
private static final String ORACLE_CHANGE_SCHEMA_SQL = "alter session set current_schema = {};";
|
||||
|
||||
@Override
|
||||
public void setSchema(Connection conn) {
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
statement.execute(StrUtil.format(ORACLE_CHANGE_SCHEMA_SQL, "ROOT"));
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* PostgreSqlSchemaHandler
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:29
|
||||
*/
|
||||
@Component
|
||||
public class PostgreSqlSchemaHandler implements SchemaHandler {
|
||||
@Override
|
||||
public void setSchema(Connection conn) {
|
||||
try {
|
||||
conn.setSchema("");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* Schema处理器
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:18
|
||||
*/
|
||||
public interface SchemaHandler {
|
||||
|
||||
/**
|
||||
* 设置/切换schema
|
||||
*
|
||||
* @param conn conn
|
||||
*/
|
||||
void setSchema(Connection conn);
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package com.qiaoba.common.database.handlers.schema;
|
||||
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* SchemaHandlerFactory-用于选择哪种类型处理器
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/20 9:22
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class SchemaHandlerFactory {
|
||||
|
||||
private static Map<String, SchemaHandler> HANDLERS = new ConcurrentHashMap<>();
|
||||
|
||||
private final MysqlSchemaHandler mysqlSchemaHandler;
|
||||
|
||||
@PostConstruct
|
||||
public void register() {
|
||||
HANDLERS.put(DataBaseEnum.MY_SQL.getType(), mysqlSchemaHandler);
|
||||
}
|
||||
|
||||
public static SchemaHandler getHandler(String name) {
|
||||
SchemaHandler schemaHandler = HANDLERS.get(name);
|
||||
if (Objects.isNull(schemaHandler)) {
|
||||
throw new ServiceException("Schema处理器工厂异常, 类型:[" + name + "]找不到相对应的解析器");
|
||||
}
|
||||
return schemaHandler;
|
||||
}
|
||||
}
|
@ -1,11 +1,17 @@
|
||||
package com.qiaoba.common.database.interceptors;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.handlers.schema.SchemaHandlerFactory;
|
||||
import com.qiaoba.common.database.utils.JdbcUtil;
|
||||
import com.qiaoba.common.database.utils.SqlUtil;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.postgresql.jdbc.PgConnection;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -22,9 +28,13 @@ public class SchemaInterceptor implements InnerInterceptor {
|
||||
public void beforePrepare(StatementHandler sh, Connection conn, Integer transactionTimeout) {
|
||||
|
||||
if (Objects.nonNull(BaseContext.isSchemaMode()) && BaseContext.isSchemaMode()) {
|
||||
SchemaHandlerFactory.getHandler(BaseContext.getDatabaseType()).setSchema(conn);
|
||||
try {
|
||||
conn.unwrap(PgConnection.class).setSchema("qiaoba-boot-2");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
InnerInterceptor.super.beforePrepare(sh, conn, transactionTimeout);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.qiaoba.common.database.factories.DynamicDataSourceFactory,\
|
||||
com.qiaoba.common.database.monitor.OnlineDatasourceMonitor,\
|
||||
com.qiaoba.common.database.monitor.NotOnlineDatasourceMonitor,\
|
||||
com.qiaoba.common.database.handlers.schema.SchemaHandlerFactory,\
|
||||
com.qiaoba.common.database.handlers.schema.MysqlSchemaHandler,\
|
||||
com.qiaoba.common.database.config.MybatisPlusConfig
|
||||
|
Reference in New Issue
Block a user