Skip to content

Commit

Permalink
修复发布模块时销毁资源导致的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Aug 23, 2024
1 parent 51ebb7c commit 9238673
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions nop-orm/src/main/java/io/nop/orm/IOrmSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public interface IOrmSession extends AutoCloseable {

boolean isClosed();

void addOnClose(Runnable task);

/**
* 在session范围内有效的缓存。
*/
Expand Down
14 changes: 11 additions & 3 deletions nop-orm/src/main/java/io/nop/orm/factory/LoadedOrmModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.nop.orm.ILoadedOrmModel;
import io.nop.orm.IOrmCachedQueryPlan;
import io.nop.orm.IOrmInterceptor;
import io.nop.orm.IOrmSession;
import io.nop.orm.QueryPlanCacheKey;
import io.nop.orm.compile.EqlCompileContext;
import io.nop.orm.eql.ICompiledSql;
Expand All @@ -13,6 +14,7 @@
import io.nop.orm.eql.compile.ISqlCompileContext;
import io.nop.orm.eql.meta.EntityTableMeta;
import io.nop.orm.eql.meta.SqlExprMetaCache;
import io.nop.orm.impl.OrmSessionRegistry;
import io.nop.orm.model.IColumnModel;
import io.nop.orm.model.IEntityModel;
import io.nop.orm.model.IEntityPropModel;
Expand Down Expand Up @@ -134,15 +136,21 @@ public String getIdText(String entityName, String alias) {

@Override
public void close() {
IOrmSession session = OrmSessionRegistry.instance().get(env);
if (session != null && session.getLoadedOrmModel() == this) {
session.addOnClose(this::doClose);
} else {
doClose();
}
}

private void doClose() {
for (IEntityPersister persister : this.entityPersisters.values()) {
IoHelper.safeCloseObject(persister);
}

for (ICollectionPersister persister : this.collectionPersisters.values()) {
IoHelper.safeCloseObject(persister);
}

this.entityPersisters.clear();
this.collectionPersisters.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.nop.orm.IOrmSession;
import io.nop.orm.IOrmSessionFactory;

class OrmSessionRegistry extends ContextualizedRegistry<IOrmSessionFactory, IOrmSession> {
public class OrmSessionRegistry extends ContextualizedRegistry<IOrmSessionFactory, IOrmSession> {
static final String CONTEXT_KEY = OrmSessionRegistry.class.getSimpleName();

public static OrmSessionRegistry instance() {
Expand Down
19 changes: 19 additions & 0 deletions nop-orm/src/main/java/io/nop/orm/session/OrmSessionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -124,6 +125,8 @@ public class OrmSessionImpl implements IOrmSessionImplementor {
// 这里总使用当前的context,不需要自己创建
private final IContext context = ContextProvider.currentContext();

private List<Runnable> onClose;

public OrmSessionImpl(boolean stateless, IPersistEnv env, List<IOrmInterceptor> interceptors) {
this.env = env;
this.loadedOrmModel = env.getLoadedOrmModel();
Expand Down Expand Up @@ -166,6 +169,13 @@ public boolean isDirty() {
return dirty;
}

@Override
public void addOnClose(Runnable task) {
if (onClose == null)
onClose = new ArrayList<>();
onClose.add(task);
}

@Override
public void flush() {
checkContext();
Expand Down Expand Up @@ -737,6 +747,15 @@ public void close() {
if (sessionCache != null)
sessionCache.clear();
closed = true;

if (onClose != null)
for (Runnable task : onClose) {
try {
task.run();
} catch (Exception e) {
LOG.error("nop.orm.session-on-close-fail", e);
}
}
}

IOrmEntity makeProxy(String entityName, Object id) {
Expand Down

0 comments on commit 9238673

Please sign in to comment.