Tuesday, November 6, 2012

oledbconnection pooling

Or when is a file close not actually a file close?

Had a snippet of code that first opened a file for shared access, then opened it as exclusive once it started a processing run

If the open routine found the file already open, it closed it, disposed the connection object, and set it to null
You would have thought that would have freed the file from use
No luck. The resulting error was less than clear: Couldn't use ; file already in use.

It was still open hiding in the connection pool

Here's the final solution


         If Not DatabaseConnection Is Nothing Then
            If DatabaseConnection .State = ConnectionState.Open Then
                Try
                    DatabaseConnection .Close()
                    DatabaseConnection .Dispose()
                    DatabaseConnection = Nothing
                    System.Threading.Thread.Sleep(TIME_TO_WAIT_STALL)
                    OleDbConnection.ReleaseObjectPool()  <-- Important line!!!!
                Catch ex As Exception

                End Try
            End If
        End If