2013-05-15 70 views
1

我們正在從MS Access遷移到現代DBMS,但同時我遇到了一個我找不到解決方案或解釋的問題。OleDB鎖定重複.NET MS Access

通常情況下,打開OleDBConnection會生成一個鎖定文件,該文件在池關閉後的最後一次連接後掛起1分鐘。如果你檢查這個文件,它會給出一些關於鎖定過程的信息,等等。通常(在我們的代碼的所有其他部分),這隻會在鎖文件中產生一個條目,而不管連接打開和關閉多少次同樣的過程,很好。

我已經開發了一些新的功能,使用完全相同的連接字符串似乎在每個connection.open()的這個鎖文件中建立一個新的條目。我已經確定所有資源都在每個方法的finally塊中關閉。更糟糕的是,如果我重新加載頁面,它會產生一個額外的設置,直到達到255個打開的連接/鎖的硬限制。

我可以看到在這個和其他許多領域唯一的區別是我加載子對象之前到達finally塊。以下模式繼續下降約3個級別。我正在加載模板來構建數據輸入表單,所以我不關心效率問題。

有沒有人在他們的ldb/laccdb文件中遇到過這種相同條目的構建? 謝謝

Provider=Microsoft.ACE.OLEDB.12.0;Data Source='...\db.accdb';Persist Security Info=False;OLE DB Services=-1; 
... 
Public Overrides Function load(ByVal DB_ID As Integer) As Boolean 
     Dim connection As OleDbConnection 
     connection = New OleDbConnection(connStr) 
     Dim reader As OleDbDataReader = Nothing 
     Try 
      Dim loadCMD As New OleDbCommand(String.Format("SELECT * FROM {0} WHERE ID = @db_ID", tableName), connection) 
      loadCMD.Parameters.AddWithValue("@db_ID", DB_ID) 
      connection.Open() 
      reader = loadCMD.ExecuteReader() 
      If reader.Read() Then 
       ID = GetNullSafeValue(reader(Schema.FormSections.ID), GetType(Integer), failure) 
       FormID = GetNullSafeValue(reader(Schema.FormSections.FormID), GetType(Integer), failure) 
       SectionTitle = GetNullSafeValue(reader(Schema.FormSections.SectionTitle), GetType(String)) 
       Expanded = GetNullSafeValue(reader(Schema.FormSections.Expanded), GetType(Boolean)) 
       ServiceURL = GetNullSafeValue(reader(Schema.FormSections.ServiceURL), GetType(String)) 
       SectionOrder = GetNullSafeValue(reader(Schema.FormSections.SectionOrder), GetType(Integer), failure) 
       Rows = FormRow.loadAllForSection(ID, config) 
       Return True 
      End If 
     Catch ex As Exception 
      ExceptionHandler(ex, config) 
     Finally 
      If reader IsNot Nothing Then 
       reader.Close() 
      End If 
      connection.Close() 
     End Try 
     Return False 
    End Function 
... 
Public Shared Function loadAllForSection(ByVal db_SectionID As Integer, ByVal cfg As ReportManagerConfiguration) As List(Of FormRow) 
    Dim retList As New List(Of FormRow) 
    Dim connection As OleDbConnection 
    connection = New OleDbConnection(cfg.RM_LabConfig.connString) 
    Dim reader As OleDbDataReader = Nothing 
    Try 
     Dim loadAll As New OleDbCommand(String.Format("SELECT ID FROM {0} WHERE SectionID = @db_sID ORDER BY RowNumber ASC", cfg.RM_LabConfig.FormRowsTable), connection) 
     loadAll.Parameters.AddWithValue("@db_sID", db_SectionID) 
     connection.Open() 
     reader = loadAll.ExecuteReader() 
     While reader.Read 
      Dim thisRow As New FormRow(cfg) 
      thisRow.load(GetNullSafeValue(reader(Schema.FormRows.ID), GetType(Integer), failure)) 
      If thisRow.ID <> failure Then 
       retList.Add(thisRow) 
      End If 
     End While 
    Catch ex As Exception 
     ExceptionHandler(ex, cfg) 
    Finally 
     If reader IsNot Nothing Then 
      reader.Close() 
     End If 
     connection.Close() 
    End Try 
    Return retList 
End Function 

回答

0

從實驗中,我已經能夠確定鎖構建是由外部調用釋放連接之前在其他函數調用中創建新連接引起的。

解決方案是重寫它,以確保連接在進入下層之前關閉。

唯一沒有解釋的是爲什麼重新運行查詢會進一步構建列表。這些連接最終關閉了,所以我預計它會在下一次使用這些插槽。

0

我不知道這個,但我想回答你的問題。

嘗試關閉您的Try Block和In finally finally塊中的連接,檢查連接是否已經打開。如果已經打開,然後關閉它。

+0

感謝您的回覆,但這似乎沒有效果。 – IKx2cf