Select to view content in your preferred language

IWorkspaceFactory deletes all features in original database

701
6
08-06-2013 07:44 AM
AlexanderGray
Honored Contributor
Another fun intermittant problem...  I have a system requirement to make a full back-up of a gdb before doing some crunching on it to make sure to be able to back-out if something nasty happens.  Turns out the back-up I do is causing something nasty

    Dim wkspcFact As IWorkspaceFactory = New FileGDBWorkspaceFactory 'I later changed this use activator (no difference)
    Dim copyWsName As IWorkspaceName = Nothing
    wkspcFact.Copy(CType(dsWork.FullName, IWorkspaceName), IO.Path.GetDirectoryName(candidateFullPath), copyWsName)

    Trace.WriteLine("copy workspace done to " & copyWsName.BrowseName)
    Dim copyWkDS As IDataset = DirectCast(DirectCast(copyWsName, IName).Open, IDataset)
    If copyWkDS.CanRename() Then
      copyWkDS.Rename(IO.Path.GetFileName(candidateFullPath))
      Trace.WriteLine("Rename to : " & candidateFullPath)
    End If


The code is called from arcmap during an edit session.
The problem that some times happens is that the copy goes smoothly and the back-up is fine but all the features in the original are gone.  Using featurecount on the featureclasses return the correct count but using gp tools report the feature classes as empty as they appear to be in arcmap.  Restarting the edit session still reports the original number of features in the feature classes even if they are empty.  If I restart ArcMap, the feature count is zero.  ArcCatalog also report a feature count of zero.

I replace the above code with this much less elegant code that seems to work better.
  Dim SourcePath As String = workspace.PathName
    IO.Directory.CreateDirectory(candidateFullPath)
    For Each dirPath As String In IO.Directory.GetDirectories(SourcePath, "*", IO.SearchOption.AllDirectories)
      IO.Directory.CreateDirectory(dirPath.Replace(SourcePath, candidateFullPath))
    Next


    For Each newPath As String In IO.Directory.GetFiles(SourcePath, "*.*", IO.SearchOption.AllDirectories)
      Try
        'if the file has a lock on it, as lock files do, it will fail to copy.
        'the prefered workspace copy is causing bigger problems than this.
        If String.Compare(IO.Path.GetExtension(newPath), ".lock", True) <> 0 Then
          IO.File.Copy(newPath, newPath.Replace(SourcePath, candidateFullPath), True)
        End If
      Catch ex As Exception
        Trace.WriteLine(ex)
        Trace.WriteLine(newPath)
      End Try
    Next
0 Kudos
6 Replies
NeilClemmons
Honored Contributor
Copying a database isn't something I would do while inside an edit session started on that database.  Have you tried it outside of the edit session?  Also, in VB.NET you can use My.Computer.FileSystem.CopyDirectory to copy the database folder and all of its contents to a new location.
0 Kudos
AlexanderGray
Honored Contributor
Copying a database isn't something I would do while inside an edit session started on that database.  Have you tried it outside of the edit session?  Also, in VB.NET you can use My.Computer.FileSystem.CopyDirectory to copy the database folder and all of its contents to a new location.

Thanks Neil,

Yes I tried calling stopediting(true) before the backup also but I still got the same sort of problems.

I hesitate to use My.Computer.FileSystem.CopyDirectory because of the exception handling of exceptions on individual files.  Also I can't filter out the schema lock files that way.  The reason I wanted to use the workspace factory copy was that esri would presumably handle the lock files and future files that don't yet exist in geodatabase.
0 Kudos
DuncanHornby
MVP Notable Contributor
Alexander,

Just an idea but have you tried calling the Copy geo-processing tool to copy the workspace?

Duncan
0 Kudos
AlexanderGray
Honored Contributor
Duncan,  I hadn't really considered that.  I am more of an AO monkey.  I still shudder at how bad the GP was in early 9.x versions.  I was also worried that the lock files might cause problems.  If I did that I wouldn't have to do synchronously (not a bad thing.)  I put the file copy change into the staging environment, if it passes regression and UAT and makes into prod, I will let sleeping dogs lie, if not I will give that a go.
0 Kudos
LeoDonahue
Deactivated User
Speaking to Java:  Calling geoprocessing tools in ArcObjects, using Add-ins, have side effects.  Your TOC and Data view are always updating, flashing during those GP routines.  I have to remember not to call map.refresh when using GP tools in Add-ins.
0 Kudos
AlexanderGray
Honored Contributor
Ah yeah, I don't use java, I use .net.   This project started at 9.3 and is a little too complicated for addins...  I find a lot of the flakiness with the com interop for .net, I assume it is the same for java.
0 Kudos