|
|
|
@ -1,11 +1,19 @@
|
|
|
|
package com.bsmlab.dfx.agent.config.datasource;
|
|
|
|
package com.bsmlab.dfx.agent.config.datasource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.bsmlab.dfx.agent.config.AgentConfigDto;
|
|
|
|
|
|
|
|
import com.bsmlab.dfx.agent.config.AgentConfigReader;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.ibatis.session.SqlSession;
|
|
|
|
import org.apache.ibatis.session.SqlSession;
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
|
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.springframework.transaction.TransactionDefinition;
|
|
|
|
|
|
|
|
import org.springframework.transaction.TransactionStatus;
|
|
|
|
|
|
|
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
@ -13,6 +21,7 @@ import java.util.Map;
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
public class SqlExecuteService {
|
|
|
|
public class SqlExecuteService {
|
|
|
|
|
|
|
|
private final AgentConfigReader agentConfigReader;
|
|
|
|
private final DynamicRoutingDataSource dynamicRoutingDataSource;
|
|
|
|
private final DynamicRoutingDataSource dynamicRoutingDataSource;
|
|
|
|
private final DynamicDataSourceService dynamicDataSourceService;
|
|
|
|
private final DynamicDataSourceService dynamicDataSourceService;
|
|
|
|
|
|
|
|
|
|
|
|
@ -37,6 +46,49 @@ public class SqlExecuteService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> insert(String dataSourceId, String sqlId, List<Map<String, Object>> parameterList) {
|
|
|
|
|
|
|
|
List<Map<String, Object>> resultParameterList = new ArrayList<>();
|
|
|
|
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
|
|
|
|
List<AgentConfigDto.DataSourceConfig> dataSourceConfigList = agentConfigReader.getAgentConfigDto().getDataSourceConfig();
|
|
|
|
|
|
|
|
AgentConfigDto.DataSourceConfig dataSourceConfig = null;
|
|
|
|
|
|
|
|
for(AgentConfigDto.DataSourceConfig config : dataSourceConfigList) {
|
|
|
|
|
|
|
|
if(dataSourceId.equals(config.getDataSourceId())) {
|
|
|
|
|
|
|
|
dataSourceConfig = config;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int maximumRowForTransaction = dataSourceConfig == null ? 1000 : dataSourceConfig.getMaximumRowForTransaction();
|
|
|
|
|
|
|
|
if(parameterList.size() > maximumRowForTransaction) {
|
|
|
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSessionFactory(dataSourceId).openSession()) {
|
|
|
|
|
|
|
|
for(int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
|
|
|
Map<String, Object> parameter = parameterList.get(i);
|
|
|
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultParameterList;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
DataSourceTransactionManager transactionManager = dynamicDataSourceService.getTransactionManager(dataSourceId);
|
|
|
|
|
|
|
|
DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
|
|
|
|
|
|
|
|
TransactionStatus transactionStatus = transactionManager.getTransaction(defaultTransactionDefinition);
|
|
|
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSessionFactory(dataSourceId).openSession()) {
|
|
|
|
|
|
|
|
for(int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
|
|
|
Map<String, Object> parameter = parameterList.get(i);
|
|
|
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
transactionManager.commit(transactionStatus);
|
|
|
|
|
|
|
|
return resultParameterList;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int update(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
public int update(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSessionFactory(dataSourceId).openSession()) {
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSessionFactory(dataSourceId).openSession()) {
|
|
|
|
|