Error handling on locked geodatabases

2339
5
Jump to solution
07-16-2012 07:18 AM
MarcCusumano
Occasional Contributor
I am doing some error handling on a python script that loads features into a file geodatabase from SDE and then restarts an ArcServer instance. I'm stuck at the point where I try to get the process to run through an inability to delete an old file GDB due to a file lock. Here is the code I am trying (snippet from full script):

        try:                   # Delete Old File Geodatabase                 shutil.rmtree(OldFileGDBPath)                 print "Deleting Old File Geodatabase..."                 logging.info("Deleted %s" %(OldFileGDBPath))                  # Rename New File Geodatabase                 os.rename(FileGDBPath, OldFileGDBPath)                 print "Renaming New File Geodatabase..."                 logging.info("Renamed %s %s" %(FileGDBPath, OldFileGDBPath))          except (IOError, TypeError, NameError) as e:                 print "I/O error({0}): {1}".format(e.errno, e.strerror)                 pass


However this does nothing to prevent the un-handled exception and stops the program from running. Thoughts?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
I was able to pass the error when I used 'arcpy.Delete_management' to delete the File Geodatabase and a lock was present.  See if this will work for you.  Here is an example:

import os, logging, arcpy  OldFileGDBPath = r"C:\temp\python\Old.gdb" FileGDBPath = r"C:\temp\python\New.gdb"  try:      # Delete Old File Geodatabase     arcpy.Delete_management(OldFileGDBPath)     print "Deleting Old File Geodatabase..."     logging.info("Deleted %s" %(OldFileGDBPath))      # Rename New File Geodatabase     arcpy.Rename_management(FileGDBPath, OldFileGDBPath)     print "Renaming New File Geodatabase..."     logging.info("Renamed %s %s" %(FileGDBPath, OldFileGDBPath))  except Exception as e:     print e     print "passing error"     pass

View solution in original post

0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
Are any ArcGIS Server services accessing data within the File Geodatabase you are trying to delete?  This will cause a lock and prevent you from deleting the geodatabase.

Could replication replace the copying of feature classes to a new File Geodatabase?  You could then perform a synchronization rather than a copy/paste of feature classes.
0 Kudos
MarcCusumano
Occasional Contributor
I suppose that would work but we already have the code set up to do a quick copy of a handful of features to a new file gdb, delete the old, and rename the new. I just need a quick way to handle an exception where the shutil.rmtree command cannot execute due to a file lock.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I was able to pass the error when I used 'arcpy.Delete_management' to delete the File Geodatabase and a lock was present.  See if this will work for you.  Here is an example:

import os, logging, arcpy  OldFileGDBPath = r"C:\temp\python\Old.gdb" FileGDBPath = r"C:\temp\python\New.gdb"  try:      # Delete Old File Geodatabase     arcpy.Delete_management(OldFileGDBPath)     print "Deleting Old File Geodatabase..."     logging.info("Deleted %s" %(OldFileGDBPath))      # Rename New File Geodatabase     arcpy.Rename_management(FileGDBPath, OldFileGDBPath)     print "Renaming New File Geodatabase..."     logging.info("Renamed %s %s" %(FileGDBPath, OldFileGDBPath))  except Exception as e:     print e     print "passing error"     pass
0 Kudos
MichaelVolz
Esteemed Contributor
Marc:

Are the file geodatabases that you are using dedicated to ArcGIS Server mapservices?  If so, you should make sure that there are no locks lingering around after you stop the associated mapservices.  Maybe the file geodatabases are also available to other endusers for their data which would place locks on the file geodatabases.

Have you made sure that all services that are hitting the file geodatabases are stopped when you try to deleted the file geodatabases?

Another solution that unfortunately would need more coding would be to truncate and append data from SDE to the file geodatabases.  This operation does not crash due to schema locks as the structure of the file geodatabase remains the same.
0 Kudos
MarcCusumano
Occasional Contributor
The file geodatabase exists solely for this purpose and should not have any other locks on it.
0 Kudos