fix[orm]: 修复数据库并发更新提示,只在update的时候记录线程号

This commit is contained in:
godotg
2022-09-08 11:32:40 +08:00
parent e8dd3f9282
commit 46a5de95cb
2 changed files with 15 additions and 5 deletions
@@ -28,6 +28,7 @@ import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.scheduler.util.TimeUtils;
import com.zfoo.util.ThreadUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
@@ -143,9 +144,20 @@ public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> impl
cache.put(entity.id(), currentPnode);
}
if (currentPnode.getThreadId() != Thread.currentThread().getId()) {
logger.error("[{} id:{}]被多线程访问了,先后2次访问线程id分别是[{}][{}]", entity.getClass().getSimpleName(), entity.id(),
currentPnode.getThreadId(), Thread.currentThread().getId());
var pnodeThreadId = currentPnode.getThreadId();
var currentThreadId = Thread.currentThread().getId();
if (pnodeThreadId != currentThreadId) {
if (pnodeThreadId == 0) {
currentPnode.setThreadId(currentThreadId);
} else {
var pnodeThread = ThreadUtils.findThread(pnodeThreadId);
if (pnodeThread == null) {
logger.warn("[{}][id:{}]有并发写风险,第一次更新的线程[id:{}],第2次更新的线程[id:{}]", entity.getClass().getSimpleName(), entity.id(), pnodeThreadId, currentThreadId);
} else {
logger.warn("[{}][id:{}]有并发写风险,第一次更新的线程[id:{}][name:{}],第2次更新的线程[id:{}][name:{}]"
, entity.getClass().getSimpleName(), entity.id(), pnodeThreadId, pnodeThread.getName(), currentThreadId, Thread.currentThread().getName());
}
}
}
// 加100以防止,立刻加载并且立刻修改数据的情况发生时,服务器取到的时间戳相同
@@ -42,8 +42,6 @@ public class PNode<E extends IEntity<?>> {
var currentTime = TimeUtils.now();
this.writeToDbTime = currentTime;
this.modifiedTime = currentTime;
this.threadId = Thread.currentThread().getId();
}
public E getEntity() {