|
|
|
|
@ -5,11 +5,7 @@ import com.bsmlab.dfx.agent.config.AgentConfigReader;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.ibatis.session.SqlSession;
|
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.TransactionDefinition;
|
|
|
|
|
import org.springframework.transaction.TransactionStatus;
|
|
|
|
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
@ -27,7 +23,7 @@ public class SqlExecuteService {
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> select(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId)) {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
return sqlSession.selectList(sqlId, parameter);
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
@ -37,7 +33,7 @@ public class SqlExecuteService {
|
|
|
|
|
|
|
|
|
|
public Map<String, Object> insert(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId)) {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
return parameter;
|
|
|
|
|
}
|
|
|
|
|
@ -58,72 +54,77 @@ public class SqlExecuteService {
|
|
|
|
|
}
|
|
|
|
|
Map<String, Object> parameter = null;
|
|
|
|
|
int maximumRowForTransaction = dataSourceConfig == null ? 1000 : dataSourceConfig.getMaximumRowForTransaction();
|
|
|
|
|
DataSourceTransactionManager transactionManager = dynamicDataSourceService.getTransactionManager(dataSourceId);
|
|
|
|
|
DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
|
|
|
|
|
TransactionStatus transactionStatus = transactionManager.getTransaction(defaultTransactionDefinition);
|
|
|
|
|
Connection connection = null;
|
|
|
|
|
if(parameterList.size() > maximumRowForTransaction) {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId)) {
|
|
|
|
|
connection = sqlSession.getConnection();
|
|
|
|
|
for(int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
parameter = parameterList.get(i);
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
}
|
|
|
|
|
transactionManager.commit(transactionStatus);
|
|
|
|
|
}
|
|
|
|
|
catch(RuntimeException e) {
|
|
|
|
|
log.error("dataSourceId: {}, sqlId: {}, parameters: {}", dataSourceId, sqlId, parameter, e);
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
try {
|
|
|
|
|
transactionManager.rollback(transactionStatus);
|
|
|
|
|
}
|
|
|
|
|
catch (RuntimeException ex) {
|
|
|
|
|
log.warn("Rollback failed", ex);
|
|
|
|
|
}
|
|
|
|
|
if(connection != null) {
|
|
|
|
|
connection = sqlSession.getConnection();
|
|
|
|
|
for (int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
parameter = parameterList.get(i);
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
|
|
|
|
|
if((i + 1) % maximumRowForTransaction == 0) { // maximumRowForTransaction 마다 커밋
|
|
|
|
|
sqlSession.flushStatements();
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sqlSession.flushStatements(); // 남은 부분 커밋
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
log.error("dataSourceId: {}, sqlId: {}, parameters: {}", dataSourceId, sqlId, parameter, e);
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
log.warn("Failed to close connection after error", ex);
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
} catch (RuntimeException ex) {
|
|
|
|
|
log.warn("Rollback failed", ex);
|
|
|
|
|
}
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
log.warn("Failed to close connection after error", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId)) {
|
|
|
|
|
connection = sqlSession.getConnection();
|
|
|
|
|
for(int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
parameter = parameterList.get(i);
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
}
|
|
|
|
|
transactionManager.commit(transactionStatus);
|
|
|
|
|
return resultParameterList;
|
|
|
|
|
}
|
|
|
|
|
catch(RuntimeException e) {
|
|
|
|
|
log.error("dataSourceId: {}, sqlId: {}, parameters: {}", dataSourceId, sqlId, parameter, e);
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
try {
|
|
|
|
|
transactionManager.rollback(transactionStatus);
|
|
|
|
|
}
|
|
|
|
|
catch (RuntimeException ex) {
|
|
|
|
|
log.warn("Rollback failed", ex);
|
|
|
|
|
connection = sqlSession.getConnection();
|
|
|
|
|
for(int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
parameter = parameterList.get(i);
|
|
|
|
|
sqlSession.insert(sqlId, parameter);
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
}
|
|
|
|
|
sqlSession.flushStatements();
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
return resultParameterList;
|
|
|
|
|
}
|
|
|
|
|
if(connection != null) {
|
|
|
|
|
catch(RuntimeException e) {
|
|
|
|
|
log.error("dataSourceId: {}, sqlId: {}, parameters: {}", dataSourceId, sqlId, parameter, e);
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
}
|
|
|
|
|
catch (RuntimeException ex) {
|
|
|
|
|
log.warn("Rollback failed", ex);
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException ex) {
|
|
|
|
|
log.warn("Failed to close connection after error", ex);
|
|
|
|
|
if(connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException ex) {
|
|
|
|
|
log.warn("Failed to close connection after error", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return resultParameterList;
|
|
|
|
|
@ -131,7 +132,7 @@ public class SqlExecuteService {
|
|
|
|
|
|
|
|
|
|
public int update(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId)) {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
return sqlSession.update(sqlId, parameter);
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
@ -139,17 +140,98 @@ public class SqlExecuteService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int update(String dataSourceId, String sqlId, List<Map<String, Object>> parameterList) {
|
|
|
|
|
public List<Map<String, Object>> update(String dataSourceId, String sqlId, List<Map<String, Object>> parameterList) {
|
|
|
|
|
int result = 0;
|
|
|
|
|
for(Map<String, Object> parameter : parameterList) {
|
|
|
|
|
result += this.update(dataSourceId, sqlId, parameter);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
Map<String, Object> parameter = null;
|
|
|
|
|
int maximumRowForTransaction = dataSourceConfig == null ? 1000 : dataSourceConfig.getMaximumRowForTransaction();
|
|
|
|
|
Connection connection = null;
|
|
|
|
|
if(parameterList.size() > maximumRowForTransaction) {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
try {
|
|
|
|
|
connection = sqlSession.getConnection();
|
|
|
|
|
for (int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
parameter = parameterList.get(i);
|
|
|
|
|
result += sqlSession.update(sqlId, parameter);
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
|
|
|
|
|
if((i + 1) % maximumRowForTransaction == 0) { // maximumRowForTransaction 마다 커밋
|
|
|
|
|
sqlSession.flushStatements();
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sqlSession.flushStatements(); // 남은 부분 커밋
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
log.error("dataSourceId: {}, sqlId: {}, parameters: {}", dataSourceId, sqlId, parameter, e);
|
|
|
|
|
try {
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
} catch (RuntimeException ex) {
|
|
|
|
|
log.warn("Rollback failed", ex);
|
|
|
|
|
}
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
log.warn("Failed to close connection after error", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
try {
|
|
|
|
|
connection = sqlSession.getConnection();
|
|
|
|
|
for(int i = 0; i < parameterList.size(); i++) {
|
|
|
|
|
parameter = parameterList.get(i);
|
|
|
|
|
result += sqlSession.update(sqlId, parameter);
|
|
|
|
|
resultParameterList.add(parameter);
|
|
|
|
|
}
|
|
|
|
|
sqlSession.flushStatements();
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
}
|
|
|
|
|
catch(RuntimeException e) {
|
|
|
|
|
log.error("dataSourceId: {}, sqlId: {}, parameters: {}", dataSourceId, sqlId, parameter, e);
|
|
|
|
|
try {
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
}
|
|
|
|
|
catch (RuntimeException ex) {
|
|
|
|
|
log.warn("Rollback failed", ex);
|
|
|
|
|
}
|
|
|
|
|
if(connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException ex) {
|
|
|
|
|
log.warn("Failed to close connection after error", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
dynamicRoutingDataSource.clearDataSource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.info("dataSourceId: {}, sqlId: {}, update count: {}", dataSourceId, sqlId, result);
|
|
|
|
|
return resultParameterList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int delete(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId)) {
|
|
|
|
|
try(SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false)) {
|
|
|
|
|
return sqlSession.delete(sqlId, parameter);
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
@ -160,7 +242,7 @@ public class SqlExecuteService {
|
|
|
|
|
public void execute(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
|
|
|
|
dynamicRoutingDataSource.setDataSource(dataSourceId);
|
|
|
|
|
try {
|
|
|
|
|
SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId);
|
|
|
|
|
SqlSession sqlSession = dynamicDataSourceService.getSqlSession(dataSourceId, false);
|
|
|
|
|
sqlSession.selectOne(sqlId, parameter);
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
|