B4J Question KVS and concurrent access

marcick

Well-Known Member
Licensed User
Longtime User
I have several point in my app that uses KVS (timers, RDC handler, etc). What happens if at the same time two subs try to access to the same KVS file ?
If I'm not wrong KVS is based on SQlite and, if I'm not wrong, SQlite is more fragile in concurrent access.
 

marcick

Well-Known Member
Licensed User
Longtime User
Yes, my app is a server app, it's a mix of JRDC2 and PushServer.
I'm investigating on some errors and I see that the same KVS file is accessed by a timer sub in main code and also when a remote client make a RDC connection.
I suspect this could be the cause of this error for example

B4X:
(SQLException) java.sql.SQLException: [SQLITE_IOERR]  Some kind of disk I/O erro
r occurred (disk I/O error)
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
This way ?

B4X:
'KeyValueStore: v2.00
Sub Class_Globals
    Private sql1 As SQL
    ''''Private ser As B4XSerializator                        '<<<<< removed
End Sub

'Initializes the store and sets the store file.
Public Sub Initialize (Dir As String, FileName As String)
    If sql1.IsInitialized Then sql1.Close
#if B4J
    sql1.InitializeSQLite(Dir, FileName, True)
#else
    sql1.Initialize(Dir, FileName, True)
#end if
    CreateTable
    sql1.ExecNonQuery("PRAGMA journal_mode = wal")            '<<<<< added line
End Sub

Public Sub Put(Key As String, Value As Object)
    Dim ser As B4XSerializator                                '<<<<< added line
    sql1.ExecNonQuery2("INSERT OR REPLACE INTO main VALUES(?, ?)", Array As Object(Key, ser.ConvertObjectToBytes(Value)))
End Sub

Public Sub Get(Key As String) As Object
    Dim rs As ResultSet = sql1.ExecQuery2("SELECT value FROM main WHERE key = ?", Array As String(Key))
    Dim result As Object = Null
    If rs.NextRow Then
        Dim ser As B4XSerializator                            '<<<<< added line
        result = ser.ConvertBytesToObject(rs.GetBlob2(0))
    End If
    rs.Close
    Return result
End Sub
 
Upvote 0
Top