保存表事务提交
This commit is contained in:
parent
83f027d1dd
commit
e929c61f3b
|
|
@ -71,15 +71,7 @@ public class StasSyncStrategyController extends JeecgController<StasSyncStrategy
|
|||
@Operation(summary="同步策略表-在目标库创建表结构")
|
||||
@PostMapping(value = "/createTargetTables")
|
||||
public Result<String> createTargetTables(@RequestBody List<StasSyncStrategy> stasSyncStrategys) {
|
||||
if(null != stasSyncStrategys && stasSyncStrategys.size() > 0) {
|
||||
String taskId = stasSyncStrategys.get(0).getTaskId();
|
||||
stasSyncStrategyService.remove(new LambdaQueryWrapper<StasSyncStrategy>().eq(StasSyncStrategy::getTaskId, taskId));
|
||||
for (StasSyncStrategy stasSyncStrategy : stasSyncStrategys) {
|
||||
if(stasSyncStrategyService.validateTables(stasSyncStrategy)){
|
||||
stasSyncStrategyService.createTargetTables(stasSyncStrategy);
|
||||
}
|
||||
}
|
||||
}
|
||||
stasSyncStrategyService.createTargetTables(stasSyncStrategys);
|
||||
return Result.OK("添加成功!");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import org.jeecg.modules.base.entity.StasSyncStrategy;
|
|||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description: 同步策略表
|
||||
|
|
@ -23,8 +24,8 @@ public interface IStasSyncStrategyService extends IService<StasSyncStrategy> {
|
|||
|
||||
/**
|
||||
* 在目标库创建表结构
|
||||
* @param stasSyncStrategy 同步策略信息
|
||||
* @param stasSyncStrategys 同步策略信息
|
||||
* @throws SQLException 数据库异常
|
||||
*/
|
||||
void createTargetTables(StasSyncStrategy stasSyncStrategy);
|
||||
void createTargetTables(List<StasSyncStrategy> stasSyncStrategys);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.jeecg.stasSyncStrategy.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jeecg.common.constant.enums.SourceDataTypeEnum;
|
||||
import org.jeecg.common.exception.JeecgBootException;
|
||||
|
|
@ -32,6 +33,7 @@ public class StasSyncStrategyServiceImpl extends ServiceImpl<StasSyncStrategyMap
|
|||
|
||||
private final StasTaskConfigMapper stasTaskConfigMapper;
|
||||
private final StasDataSourceMapper stasDataSourceMapper;
|
||||
private final StasSyncStrategyMapper stasSyncStrategyService;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -67,53 +69,59 @@ public class StasSyncStrategyServiceImpl extends ServiceImpl<StasSyncStrategyMap
|
|||
|
||||
/**
|
||||
* 在目标库创建表结构
|
||||
* @param stasSyncStrategy 同步策略信息
|
||||
* @param stasSyncStrategys 同步策略信息
|
||||
* @throws SQLException 数据库异常
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public void createTargetTables(StasSyncStrategy stasSyncStrategy) {
|
||||
StasTaskConfig stasTaskConfig = stasTaskConfigMapper.selectById(stasSyncStrategy.getTaskId());
|
||||
StasDataSource sourceInfo = stasDataSourceMapper.selectById(stasTaskConfig.getSourceId());
|
||||
StasDataSource targetInfo = stasDataSourceMapper.selectById(stasTaskConfig.getTargetId());
|
||||
public void createTargetTables(List<StasSyncStrategy> stasSyncStrategys) {
|
||||
if(null != stasSyncStrategys && stasSyncStrategys.size() > 0) {
|
||||
String taskId = stasSyncStrategys.get(0).getTaskId();
|
||||
stasSyncStrategyService.delete(new LambdaQueryWrapper<StasSyncStrategy>().eq(StasSyncStrategy::getTaskId, taskId));
|
||||
for (StasSyncStrategy stasSyncStrategy : stasSyncStrategys) {
|
||||
StasTaskConfig stasTaskConfig = stasTaskConfigMapper.selectById(stasSyncStrategy.getTaskId());
|
||||
StasDataSource sourceInfo = stasDataSourceMapper.selectById(stasTaskConfig.getSourceId());
|
||||
StasDataSource targetInfo = stasDataSourceMapper.selectById(stasTaskConfig.getTargetId());
|
||||
|
||||
String sourceUrl = DBUtil.getUrl(sourceInfo.getIpAddress(), sourceInfo.getPort(), sourceInfo.getServeId());
|
||||
String targetUrl;
|
||||
if(SourceDataTypeEnum.ORACLE.getKey().equals(targetInfo.getType())){
|
||||
targetUrl = DBUtil.getUrl(targetInfo.getIpAddress(), targetInfo.getPort(), targetInfo.getServeId());
|
||||
}else{
|
||||
targetUrl = DBUtil.getPgUrl(targetInfo.getIpAddress(), targetInfo.getPort(), targetInfo.getServeId());
|
||||
}
|
||||
|
||||
try (Connection sourceConn = DriverManager.getConnection(sourceUrl, sourceInfo.getUsername(), sourceInfo.getPassword());
|
||||
Connection targetConn = DriverManager.getConnection(targetUrl, targetInfo.getUsername(), targetInfo.getPassword())) {
|
||||
|
||||
try {
|
||||
// 检查表是否已存在
|
||||
if (!isTableExists(targetConn, stasSyncStrategy.getTargetOwner(), stasSyncStrategy.getTableName(), targetInfo.getType())) {
|
||||
// 获取源表结构
|
||||
String createSql = getCreateTableSql(sourceConn, stasSyncStrategy.getSourceOwner(),
|
||||
stasSyncStrategy.getTargetOwner(), stasSyncStrategy.getTableName(),
|
||||
sourceInfo.getType(), targetInfo.getType());
|
||||
|
||||
// 在目标库创建表
|
||||
try (Statement stmt = targetConn.createStatement()) {
|
||||
stmt.execute(createSql);
|
||||
this.baseMapper.insert(stasSyncStrategy);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new JeecgBootException(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
this.baseMapper.insert(stasSyncStrategy);
|
||||
// throw new JeecgBootException(String.format("在用户%s中,%s表已存在,请删除已存在的表!",
|
||||
// stasSyncStrategy.getTargetOwner(), stasSyncStrategy.getTableName()));
|
||||
String sourceUrl = DBUtil.getUrl(sourceInfo.getIpAddress(), sourceInfo.getPort(), sourceInfo.getServeId());
|
||||
String targetUrl;
|
||||
if(SourceDataTypeEnum.ORACLE.getKey().equals(targetInfo.getType())){
|
||||
targetUrl = DBUtil.getUrl(targetInfo.getIpAddress(), targetInfo.getPort(), targetInfo.getServeId());
|
||||
}else{
|
||||
targetUrl = DBUtil.getPgUrl(targetInfo.getIpAddress(), targetInfo.getPort(), targetInfo.getServeId());
|
||||
}
|
||||
|
||||
try (Connection sourceConn = DriverManager.getConnection(sourceUrl, sourceInfo.getUsername(), sourceInfo.getPassword());
|
||||
Connection targetConn = DriverManager.getConnection(targetUrl, targetInfo.getUsername(), targetInfo.getPassword())) {
|
||||
|
||||
try {
|
||||
// 检查表是否已存在
|
||||
if (!isTableExists(targetConn, stasSyncStrategy.getTargetOwner(), stasSyncStrategy.getTableName(), targetInfo.getType())) {
|
||||
// 获取源表结构
|
||||
String createSql = getCreateTableSql(sourceConn, stasSyncStrategy.getSourceOwner(),
|
||||
stasSyncStrategy.getTargetOwner(), stasSyncStrategy.getTableName(),
|
||||
sourceInfo.getType(), targetInfo.getType());
|
||||
|
||||
// 在目标库创建表
|
||||
try (Statement stmt = targetConn.createStatement()) {
|
||||
stmt.execute(createSql);
|
||||
this.baseMapper.insert(stasSyncStrategy);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new JeecgBootException(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
this.baseMapper.insert(stasSyncStrategy);
|
||||
// throw new JeecgBootException(String.format("在用户%s中,%s表已存在,请删除已存在的表!",
|
||||
// stasSyncStrategy.getTargetOwner(), stasSyncStrategy.getTableName()));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JeecgBootException(String.format("处理表: %s时出错", stasSyncStrategy.getTableName()));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JeecgBootException(e.getMessage());
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JeecgBootException(String.format("处理表: %s时出错", stasSyncStrategy.getTableName()));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JeecgBootException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user