2013-09-05 1 views
1

내 프로그램은 sqlite 데이터베이스를 검사하고 변경 사항이 발견되면 업데이트하는 자체 업데이트 기능을 실행하는 많은 클래스로 구성됩니다. 아마도 다른 스레드가 DB에 대한 액세스를 쓰는 반면 SQLcommand.ExecuteNonQuery이 실행될 때Sqlite3 데드락 (동시 db 업데이트, VB.net)

Async Function update() As Task(Of String) 
. 
. 
    Await Task.Run(Sub() 
    . 
    .       
    End Sub) 
. 
. 
    helper.updatedb(par1, par2, par3, par4) 
. 
. 
End Function 

Sub updatedb(ByVal name As String, ByVal entry As String, ByVal smth As String, Optional ByVal random As String = vbNullString) 

      Dim SQLconnect As New SQLite.SQLiteConnection() 
      Dim SQLcommand As SQLiteCommand 
      SQLconnect.ConnectionString = "data source=" + dbpath 
      SQLconnect.Open() 
      SQLcommand = SQLconnect.CreateCommand 
      SQLcommand.CommandText = "SELECT ""entry"" FROM profiles WHERE ""name"" = @name" 
      SQLcommand.Parameters.AddWithValue("@name", LCase(name)) 
      Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader() 
      SQLreader.Read() 

      Dim result As String = SQLreader(0) 

       SQLcommand.Dispose() 
       SQLcommand = SQLconnect.CreateCommand 
       SQLcommand.CommandText = "UPDATE profiles SET ""entry"" = @entry WHERE ""name"" = @name" 
       SQLcommand.Parameters.AddWithValue("@name", LCase(name)) 
       SQLcommand.Parameters.AddWithValue("@entry", entry) 

       SQLcommand.ExecuteNonQuery() 

      SQLcommand.Dispose() 
      SQLreader.Close() 
      SQLconnect.Close() 
      SQLconnect.Dispose() 
    End Sub 

문제가 나타납니다. "System.Data.SQLite.dll에서 'System.Data.SQLite.SQLiteException'형식의 첫 번째 예외가 발생했습니다."

업데이트 명령에서 뮤텍스를 사용하여이를 반박 할 수있는 다른 스레드를 읽었습니다. 내 코드에 뮤텍스를 구현하는 방법은 무엇입니까? 이식성을 선호하고 그러한 동시성 문제가없는 sqlite 대안이 있습니까?

답변

0

... 파기하는 문장의 배치가 문제였다 같은데 이제 의도대로 작동합니다 ...

0

this online help 참조 :

당신은 잠금 변수를 만든 다음을 잠글; 하나의 스레드 만이 한 번에 잠금을 가질 수 있고 다른 사람이 자동으로 대기 :

Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader() 
->SQLcommand.Dispose() 
SQLreader.Read() 

Dim result As String = SQLreader(0) 
->SQLreader.Close() 

: 그럼 난 저주받을거야

Dim lockThis As New Object 

Sub updatedb(ByVal name As String, ...) 
    SyncLock lockThis 
     Dim SQLconnect As New SQLite.SQLiteConnection() 
     ... 
    End SyncLock 
End Sub 
+0

빠른 답장을 보내 주셔서 감사합니다. 불행히도 나는 여전히 같은 "데이터베이스 잠김"오류가 발생하고 있습니다. – kokotas