B4J Question Not sure, if i can access one file with two apps

schimanski

Well-Known Member
Licensed User
Longtime User
I'm asking this, because I don't know, if this is a correct way to do it:

I'm using two server applications in the same directory. Both non-ui-apps have to access the same files:

File 1:
The first file is an RandomAccessFile which I load in both applications with

B4X:
Public Kontakte As Map
...
If File.Exists(File.DirApp, "Kontakte.eis") Then
    Dim Kontaktdatei As RandomAccessFile
    Kontaktdatei.Initialize(File.DirApp,"Kontakte.eis", False)
    Kontakte = Kontaktdatei.ReadB4XObject(0)
    Kontaktdatei.Close
Else
    Kontakte.Initialize
End If

and

write to it in both of the apps with

B4X:
Public Value As List
...
Kontakte.Put(key, Value)  
Dim Kontaktdatei As RandomAccessFile
Kontaktdatei.Initialize(File.DirApp, "Kontakte.eis", False)
Kontaktdatei.WriteB4XObject(Kontakte, 0)
Kontaktdatei.Close

File 2:
The second file is a keyvaluestore 1-file (sqlite), which I set to wal-mode:

In both apps I initialize the kvs with

B4X:
Public NutzerDatenbank As KeyValueStore
...
NutzerDatenbank.Initialize(File.DirApp, "NutzerDatenbank")

The first application reads and writes to this kvs, the second only has to remove sometimes a key with

B4X:
Public Sub DeleteUser (name As String) As Boolean
    Try
        NutzerDatenbank.Remove(name)
        Return True
    Catch
        Return False
    End Try
End Sub

The second file (kvs) seems to be access from both applications in a correct way. The first file (randomaccess) will sometimes be forged, when both apps are started.

I'm not sure, if I could do it in such a way. Even, if the first file will be written correctly, is it sure, that it will work with more traffic?

Perhaps, somebody has some experiences with such multi accessing. Thanks for each small advice...
 

schimanski

Well-Known Member
Licensed User
Longtime User
Not good. The files will become corrupted.

You have two options:
1. Why run two servers? Run a single server with all the handlers that you need.
2. Put all the data in a database server such as MySQL.

Thanks for answer. My software has been pen-tested and the pentesters has suggested me to split the user-operations from the admin-operations over different ips. Thanks to your work with the update jserver to version 2.52, i now use two different server apps. But this will only make sense, if both apps could read and write to the same files..

The two files, i have to write to are very small maps with 100 to 120 entries. An sql-server seems to be elaborated for this:rolleyes:
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @schimanski,
if you want to stick to your current design, I guess that a (risky) solution could be given by a semaphore file playing the lock role.
What I mean is that both your apps, when ready to access the "db data", should check for the existence of a lock imposed by the other one and, if there ain't none, first place a lock, then operate on the db-data shared files and finally remove the lock.
As for the lock, it could be a simple file (any type) since you only check for its existence.

There are risks associated with the above scenario.. just consider two of the most obvious:
- apps testing simultaneously for a lock
- app crashing before removing a lock

udg
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I think, that could be a good solution. The Admin-Server has to write to the files very seldom, for example, when a device is lost. So i think, that the risks you wrote are assessable. You are right, that i want to stick my design because i have to be mouthy with the different ips:D...
 
Upvote 0
Top