iTwin Briefcase锁错误咨询:修改iModel时锁被其他Briefcase持有如何释放
如何释放其他Briefcase持有的iModel Schema锁以解决409冲突?
问题描述
当我执行以下代码尝试修改iModel的Schema时:
await iModel.concurrencyControl.locks.lockSchema(ctx); await iModel.importSchemas(ctx, [schemaFile]); iModel.saveChanges();
遇到了409错误:"Lock(s) is owned by another briefcase"。
我已经尝试通过以下代码放弃本地变更,但问题依然存在:
const briefcaseProps = await BriefcaseManager.downloadBriefcase( ctx, { contextId, iModelId, asOf: IModelVersion.latest().toJSON() } ); // Open iModel const iModel = await BriefcaseDb.open(ctx, { fileName: briefcaseProps.fileName, readonly: false }); ctx.enter(); iModel.abandonChanges(); iModel.concurrencyControl.abandonResources(ctx);
我使用的是基于agent-starter的Node.js后端服务应用,请问如何释放其他Briefcase持有的锁?
解决方案
既然你的服务是基于iTwin Agent构建的,Agent默认会被授予iModel的管理权限,刚好可以利用这个权限来强制释放其他Briefcase持有的锁,具体步骤如下:
1. 先确认锁的持有者信息
在尝试释放锁之前,先搞清楚到底是哪个Briefcase拿着Schema锁:
const lockInfo = await iModel.concurrencyControl.locks.getLockInfo(ctx, LockType.Schema); console.log("当前持有Schema锁的Briefcase ID:", lockInfo.ownerBriefcaseId);
2. 强制释放目标锁
用Agent的权限直接调用forceRelease方法,就能释放不属于当前Briefcase的锁:
// 传入锁类型和持有锁的Briefcase ID await iModel.concurrencyControl.locks.forceRelease( ctx, LockType.Schema, lockInfo.ownerBriefcaseId );
3. 增加前置检查避免重复冲突
为了以后少踩坑,建议在执行Schema修改前先做个锁状态检查,确保没有其他持有者:
// 检查Schema锁是否被持有 const isSchemaLocked = await iModel.concurrencyControl.locks.isLocked(ctx, LockType.Schema); if (isSchemaLocked) { const lockOwner = await iModel.concurrencyControl.locks.getLockOwner(ctx, LockType.Schema); // 如果锁不是当前Briefcase持有的,直接强制释放 if (lockOwner !== iModel.briefcaseId) { await iModel.concurrencyControl.locks.forceRelease(ctx, LockType.Schema, lockOwner); } } // 现在可以安全执行Schema修改了 await iModel.concurrencyControl.locks.lockSchema(ctx); await iModel.importSchemas(ctx, [schemaFile]); await iModel.saveChanges();
重要提醒
- 强制释放锁只能由具备iModel管理员权限的主体执行,你的Agent服务默认应该有这个权限,但如果遇到权限问题,记得检查Agent的权限配置。
- 不要随便强制释放锁,除非你确定持有锁的Briefcase已经异常退出或者不再需要这个锁,不然可能会导致其他进程的数据一致性问题。
- 如果还是不行,先确认你的Agent下载Briefcase时用的
contextId和iModelId是正确的,别操作了错误的iModel实例。
内容的提问来源于stack exchange,提问作者lmborione
相关产品推荐
相关产品推荐

