mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-20 22:24:27 +00:00
feat[module]: 新增多线程访问报错和entity未初始化时判空
This commit is contained in:
@@ -94,7 +94,6 @@ public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> impl
|
||||
// 如果数据库中不存在则给一个默认值
|
||||
if (entity == null) {
|
||||
entity = (E) entityDef.newEntity(pk);
|
||||
logger.error("数据库[{}]没有包含主键[pk:{}]的文档,返回默认值", entityDef.getClazz().getSimpleName(), pk);
|
||||
}
|
||||
|
||||
return new PNode<E>(entity);
|
||||
@@ -144,6 +143,11 @@ 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("[{}]被多线程访问了,先后2次访问线程分别是[{}] [{}]", entity.getClass().getSimpleName(),
|
||||
currentPnode.getThreadName(), Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
// 加100以防止,立刻加载并且立刻修改数据的情况发生时,服务器取到的时间戳相同
|
||||
currentPnode.setModifiedTime(TimeUtils.now() + 100);
|
||||
}
|
||||
@@ -241,7 +245,7 @@ public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> impl
|
||||
var ids = updateList.stream().map(it -> it.id()).collect(Collectors.toList());
|
||||
|
||||
try {
|
||||
var dbList = OrmContext.getQuery((Class<E>)entityDef.getClazz()).in("_id", ids).queryAll();
|
||||
var dbList = OrmContext.getQuery((Class<E>) entityDef.getClazz()).in("_id", ids).queryAll();
|
||||
var dbMap = dbList.stream().collect(Collectors.toMap(key -> key.id(), value -> value));
|
||||
for (var entity : updateList) {
|
||||
var dbEntity = dbMap.get(entity.id());
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
package com.zfoo.orm.model.entity;
|
||||
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
@@ -46,4 +48,25 @@ public interface IEntity<PK extends Comparable<PK>> {
|
||||
default void svs(long vs) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 由于查询不存在时缓存中也会有一份,因此判断为空需要根据实际类型才能决定
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
default boolean isNull() {
|
||||
PK idValue = id();
|
||||
if (idValue == null) {
|
||||
return true;
|
||||
}
|
||||
if (idValue instanceof Integer) {
|
||||
return idValue.equals(0);
|
||||
}
|
||||
if (idValue instanceof Long) {
|
||||
return idValue.equals(0L);
|
||||
}
|
||||
if (idValue instanceof String) {
|
||||
return StringUtils.isEmpty((CharSequence) idValue);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,15 +31,21 @@ public class PNode<E extends IEntity<?>> {
|
||||
// 修改数据的时间
|
||||
private volatile long modifiedTime;
|
||||
|
||||
|
||||
private volatile E entity;
|
||||
|
||||
// 记录最初访问时的线程信息
|
||||
private long threadId;
|
||||
private String threadName;
|
||||
|
||||
public PNode(E entity) {
|
||||
this.entity = entity;
|
||||
|
||||
var currentTime = TimeUtils.now();
|
||||
this.writeToDbTime = currentTime;
|
||||
this.modifiedTime = currentTime;
|
||||
|
||||
this.threadId = Thread.currentThread().getId();
|
||||
this.threadName = Thread.currentThread().getName();
|
||||
}
|
||||
|
||||
public E getEntity() {
|
||||
@@ -65,4 +71,20 @@ public class PNode<E extends IEntity<?>> {
|
||||
public void setModifiedTime(long modifiedTime) {
|
||||
this.modifiedTime = modifiedTime;
|
||||
}
|
||||
|
||||
public long getThreadId() {
|
||||
return threadId;
|
||||
}
|
||||
|
||||
public void setThreadId(long threadId) {
|
||||
this.threadId = threadId;
|
||||
}
|
||||
|
||||
public String getThreadName() {
|
||||
return threadName;
|
||||
}
|
||||
|
||||
public void setThreadName(String threadName) {
|
||||
this.threadName = threadName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user