Cannot acquire a lock,资料贴,仅供参考

来源:互联网 发布:淘宝的店铺链接在哪里 编辑:程序博客网 时间:2024/06/08 10:49

开篇

在做AO开发的时候,前一步骤生成一个shp文件,后一步去执行删除其中一个feature的时候就报Cannot acquire a lock错误,通常我们在使用arcmap执行编辑操作的时候,多个软件共用同一个数据源或者多用户共用一个geodatabase时就会遇到此问题,所以就不难理解了,文件锁是arcgis用于工作流的一套解决机制,那么在代码层面如何去解决呢,搜集资料整理如下。

ArcEngine错误提示

HRESULT:0x80040216
Cannot acquire a schema lock because of an existing lock. SCHEMA_LOCK_CONFLICT

File geodatabases and locking

If you are the sole user working with a dataset in a file geodatabase and are accessing it via one process only, you are able to freely edit or modify its properties. You are also able to delete, compress, or decompress the dataset.
However, if another process is already accessing the data, whether it be a process on your computer or another user’s, ArcGIS prevents you from performing any of these operations until the other process has terminated. For example, if you have a feature class displayed in ArcMap and attempt to delete it from the Catalog tree, you will get the following message:

Failed to delete selected object(s)Cannot acquire a schema lock because of an existing lock.

If you then close ArcMap and try again, you can delete the feature class, provided there are no other processes accessing the data.
When a process accesses a dataset, it locks the dataset, preventing other processes from modifying it. Accessing a dataset also locks additional related datasets as follows:
A process accessing a feature class within a feature dataset locks all the feature classes in the feature dataset.
Locks apply to both sides of a relationship class. That is, if two stand-alone feature classes are related via a relationship class and a process accesses one of them, both feature classes lock.
To keep track of locked datasets, ArcGIS places *.lock files in the file geodatabase folder. When a process terminates, the .lock files associated with it are automatically deleted. In some cases, however, such as when a process ends prematurely, .lock files may be left behind in the geodatabase folder. These .lock files are eventually removed by ArcGIS in future sessions as new locks are taken. They are also removed whenever you run the geoprocessing Compact tool or copy and paste the data in the Catalog tree. In the meantime, these .lock files do not continue to lock data.
Locks are critical for file and process management. However, sometimes it can seem like they get in the way. If you are ever prevented from editing a dataset or its properties or from deleting, compressing, or decompressing, it is because one of the following is happening:
You are accessing the data with two or more processes simultaneously, such as ArcMap and ArcCatalog or ArcCatalog and Python. Close one of the applications so you can perform the operation you want.
Another process on your computer is still accessing the data because it did not terminate properly. For example, even though ArcCatalog does not appear to be running, it may not have closed properly and could still be holding a lock on the data. Check your Task Manager for such orphaned processes and end them.
The data is being accessed by one or more processes on another computer. Wait for the processes to terminate or have the user or users terminate any process accessing the data.
If you do not know what other computers are accessing the data, use Windows Explorer to carefully delete the *.lock files in the file geodatabase folder. Exercise caution when doing this—accidental deletion of files other than .lock files can corrupt the data. After deleting the .lock files, look at the names of .lock files that you cannnot delete. The names of these remaining .lock files contain the names of the computers still accessing the data. For example, a .lock file named xxx.PLUTO.15332.sr.lock indicates the computer PLUTO is accessing the data.As a last resort, you can clear all active locks by restarting the computer containing the file geodatabase. However, before you do this, make sure nobody is accessing the data; otherwise, their work may not be saved.

AE二次开发中,打开本地shp文件后,出现文件锁定状态,即后缀为 .sr.lock

解决方法如下:           string dkpath = System.IO.Path.GetDirectoryName(dkPath);            string dkname = System.IO.Path.GetFileName(dkPath);            IWorkspaceFactory pwf = new ShapefileWorkspaceFactory();            //关闭资源锁定               IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)pwf;//注意在java api中不能强转切记需要IWorkspaceFactoryLockControl ipWsFactoryLock = new IWorkspaceFactoryLockControlProxy(pwf);            if (ipWsFactoryLock.SchemaLockingEnabled)            {                ipWsFactoryLock.DisableSchemaLocking();            }            IWorkspace pw = pwf.OpenFromFile(dkpath, 0);            IFeatureWorkspace pfw = pw as IFeatureWorkspace;            IFeatureClass pfc = pfw.OpenFeatureClass(dkname);            IFeatureLayer pf = new FeatureLayerClass();            pf.FeatureClass = pfc;            pf.Name = "DKXX";            ILayer pl = pf as ILayer;            m_FeatureLayer = pl as IFeatureLayer;            this.mcView.AddLayer(pl, 0);将解决文件锁定状态。

ArcEngine filegeodatabase锁定文件 .Lock
项目中遇到了ArcEngine锁的问题,读取Shp文件后,多产生了.Lock文件,导致下次运行无法得到预期值。

Arcgis官方文件锁说明
转载自:AE filegeodatabase锁定文件
网上找到这篇内容,虽然还没尝试成功,感觉比较靠谱,先记录下。
1、说明
在使用ArcGIS桌面产品的时候,有时会碰到这样的情况:同时在ArcCatalog和ArcMap中浏览图层数据,然后在ArcMap中remove掉图层,再回到ArcCatalog中删除该图层,却得到该图层被锁的信息。非得关掉ArcMap才能正常删除。同样的情况也会在ArcEngine开发的应用程序中碰到。可有时候会有这样需要,在不关闭ArcEngine应用程序的情况下,对图层数据做相应的操作,如将图层数据压缩打包。对这个问题ArcEngine提供了相应的接口控制,只是我们很少去关注而已。
2、解锁资源文件
解除对资源文件的锁定需要使用IWorkspaceFactoryLockControl接口,默认情况下对资源文件的锁定状态是打开的,可通过如下的方式得知:
ipWsFactoryLock.SchemaLockingEnabled
解锁资源文件的一般步骤:使用IWorkspaceFactoryLockControl接口关闭资源锁定,然后在使用完AE COM接口后,使用ComRelease释放COM接口即可。示例源码如下:

private void mnuItemUnlockFGDB_Click(object sender, EventArgs e)  {          IWorkspaceFactory2 ipWsFactory = new FileGDBWorkspaceFactoryClass();          //关闭资源锁定      IWorkspaceFactoryLockControl ipWsFactoryLock;         ipWsFactoryLock = (IWorkspaceFactoryLockControl)ipWsFactory;      if (ipWsFactoryLock.SchemaLockingEnabled)      {          ipWsFactoryLock.DisableSchemaLocking();      }      String strConn = "DATABASE=F:\\临时测试\\world.gdb";              IWorkspace ipWorkspace = ipWsFactory.OpenFromString(strConn,0);      IFeatureWorkspace ipFtWorkspace = (IFeatureWorkspace)ipWorkspace;      IFeatureDataset ipFtDataset = ipFtWorkspace.OpenFeatureDataset("world");      IEnumDataset ipEnumDataset = ipFtDataset.Subsets;      ipEnumDataset.Reset();      IDataset ipDataset = ipEnumDataset.Next();      while (ipDataset!=null)      {                         IFeatureClass ipFtClass = (IFeatureClass)ipDataset;               IFeatureLayer ipFtLayer = new FeatureLayerClass();                    ipFtLayer.FeatureClass = ipFtClass;                       ipFtLayer.Name = ipDataset.Name;              //对图层做相关操作处理          ipDataset = ipEnumDataset.Next();          ComReleaser.ReleaseCOMObject(ipFtClass);              ComReleaser.ReleaseCOMObject(ipFtLayer);      }      ComReleaser.ReleaseCOMObject(ipWorkspace);      ComReleaser.ReleaseCOMObject(ipFtWorkspace);      ComReleaser.ReleaseCOMObject(ipFtDataset);      ComReleaser.ReleaseCOMObject(ipEnumDataset);      ComReleaser.ReleaseCOMObject(ipDataset);      ComReleaser.ReleaseCOMObject(ipWsFactory);      ComReleaser.ReleaseCOMObject(ipWsFactoryLock);  }  

国外博客(就靠它了,摊手)
https://thoughtconnect.wordpress.com/

Disabling Schema Lock while working with ArcObjectsWhile working with Arcobjects and using IWorkspaceFactory to create a workspace for working with layers by default places Schema locks. It maybe sometimes required to disable this locking. Below is the code snippet with demostrates how to diasable schema locking while working with ArcObjects//ArcObjects Java code to unlock the WorkspacescExport = getServerContext(); // Open SDE WorkspaceIWorkspaceFactory sdeWkspFactory = new IworkspaceFactoryProxy(scExport.createObject(“esriDataSourcesGDB.SdeWorkspaceFactory”));// Open IworkspaceFactoryLockControlProxy objectIWorkspaceFactoryLockControlProxy ipWsFactoryLockProxy = new IWorkspaceFactoryLockControlProxy(sdeWkspFactory);ipWsFactoryLockProxy.disableSchemaLocking();public IServerContext getServerContext(){IServerContext sc = null;String server = Constants.getAGSServer();String user = Constants.getAGSUser();String pwd = Constants.getAGSPwd();String domain = Constants.getAGSDomain();try{ServerInitializer serverInitializer = new ServerInitializer();serverInitializer.initializeServer(domain, user, pwd);ServerConnection connection = new ServerConnection();connection.connect(server);IServerObjectManager som = connection.getServerObjectManager();sc = som.createServerContext(“”, “”);}catch(Exception ex){ex.printStackTrace();}return sc;}

ArcGIS锁的介绍
集思园集思园http://blog.csdn.net/linghe301/article/details/7345194

一点儿思考
百度上找不到解决方案,不翻墙情况下试下bing
试着在相关官网上查找国外外网gis.stackexchange
这里写图片描述这里写图片描述

原创粉丝点击