opt: 抛出异常时数据连接不断开
This commit is contained in:
@@ -44,30 +44,35 @@ public class TaskUtils {
|
|||||||
* @param function
|
* @param function
|
||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static void taskLock(String code, Runnable function){
|
public static void taskLock(String code, Runnable function) {
|
||||||
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
||||||
RLock lock = redissonClient.getLock(code);
|
RLock lock = redissonClient.getLock(code);
|
||||||
boolean tryLock = lock.tryLock(5, TimeUnit.SECONDS);
|
boolean tryLock = lock.tryLock(5, TimeUnit.SECONDS);
|
||||||
|
PlatformTransactionManager txManager = SpringContextHolder.getBean(PlatformTransactionManager.class);
|
||||||
|
TransactionStatus status = null;
|
||||||
try {
|
try {
|
||||||
if (tryLock) {
|
if (tryLock) {
|
||||||
PlatformTransactionManager txManager = SpringContextHolder.getBean(PlatformTransactionManager.class);
|
|
||||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||||
TransactionStatus status = txManager.getTransaction(def);
|
status = txManager.getTransaction(def);
|
||||||
|
|
||||||
//执行目标方法
|
// 执行目标方法
|
||||||
function.run();
|
function.run();
|
||||||
|
|
||||||
txManager.commit(status);
|
txManager.commit(status);
|
||||||
} else {
|
} else {
|
||||||
throw new BadRequestException("其他设备占用锁,等待结束!");
|
throw new BadRequestException("其他设备占用锁,等待结束!");
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (status != null) {
|
||||||
|
txManager.rollback(status);
|
||||||
|
}
|
||||||
|
throw e; // 重新抛出异常以便调用者处理
|
||||||
} finally {
|
} finally {
|
||||||
if (tryLock) {
|
if (tryLock) {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setCreateByAcs(SchBaseTask taskObj) {
|
public static void setCreateByAcs(SchBaseTask taskObj) {
|
||||||
|
|||||||
@@ -1,12 +1,24 @@
|
|||||||
package org.nl;
|
package org.nl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.TypeReference;
|
import com.alibaba.fastjson.TypeReference;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.nl.common.exception.BadRequestException;
|
||||||
|
import org.nl.config.SpringContextHolder;
|
||||||
|
import org.nl.wms.sch.point.service.ISchBasePointService;
|
||||||
|
import org.nl.wms.sch.point.service.dao.SchBasePoint;
|
||||||
|
import org.nl.wms.util.TaskUtils;
|
||||||
|
import org.redisson.api.RLock;
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: lyd
|
* @Author: lyd
|
||||||
@@ -16,6 +28,9 @@ import java.util.Map;
|
|||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public class ApplicationTest {
|
public class ApplicationTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ISchBasePointService schBasePointService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
}
|
}
|
||||||
@@ -38,4 +53,42 @@ public class ApplicationTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSQLException() throws InterruptedException {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
new Thread(this::doExec).start();
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
void doExec() {
|
||||||
|
TaskUtils.taskLock("sss", () -> {
|
||||||
|
SchBasePoint byId = schBasePointService.getById("100896325655");
|
||||||
|
if (ObjectUtil.isEmpty(byId)) {
|
||||||
|
throw new BadRequestException("sss-1");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@SneakyThrows
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
void doExec2() {
|
||||||
|
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
||||||
|
RLock lock = redissonClient.getLock("sss");
|
||||||
|
boolean tryLock = lock.tryLock(5, TimeUnit.SECONDS);
|
||||||
|
try {
|
||||||
|
if (tryLock) {
|
||||||
|
SchBasePoint byId = schBasePointService.getById("100896325655");
|
||||||
|
if (ObjectUtil.isEmpty(byId)) {
|
||||||
|
throw new BadRequestException("sss-1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (tryLock) {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user