B4J Question Best way in saving data

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am in the middle of writing a app in B4J and have a question on what people think on how I should do something..

First of all my B4J app will allow both Android and iOS to connect to this app written in B4J and poll for data from the B4J app. (ASCII message)

It will allow incoming connections from Telnet and allow the user to send a message to my server (B4J app).

eg:

User 1 sends:
User1.status1.testing1
User1.status2.this is a test

User 2 sends:
User2.status1.hello

User 3 sends:
User3.status2.hi

Based on the example above 3 users have sent a message and has updated status1 and status 2 based on there user login.

Now the Android & iOS app can send a message and request the value that has been set by the Telnet user.

Eg:
Android user sends a message to get the status of status1 from user1 it should reply with 'this is a test'

The question I have is, how should I save the status that the Telnet user sends?

Should I save each in a SQLite database and when the Android/iOS app connects and requests the value then connect to the SQLite database each time or should I save each in a Map and get the data from the Map or is there another (better) way I should do this?

(hopefully the above makes sense)
 

Roycefer

Well-Known Member
Licensed User
Longtime User
It depends on your requirements. If the data is indispensable, an SQLite database will persist even if your B4J program crashes. That is, the data will still be there when your B4J program starts up again, provided you didn't corrupt the db. If you had used a Map, a program crash would mean you lose all the data in the Map. Also, if you plan on running the B4J program for a long time and storing a lot of data, the Map will grow in size and consume ever more heap space. You might eventually get a java.lang.OutOfMemory Exception (depending, of course, on how big your heap is and how much data you are trying to store in your Map). With an SQLite db, this won't happen; the db is stored on your hard disk (though that means slower access). From a programming perspective, Maps are definitely easier to use than SQLite.

The threading model of your program will also have some effect on the optimal choice. SQLite automatically forces access to be serial in nature. This protects against many threading-related problems. Default B4J Maps aren't thread safe, though B4J does offer a thread safe Map you can use.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can store any object you like in the user session.
B4X:
dim session as HttpSession = req.GetSession
If session.HasAttribute("Key") Then
 Dim state As String = session.GetAttribute("Key")
 ...
End If

The session is managed by the server. It is not persistent but in most cases it doesn't matter (it means that if you restart the server then the session will be lost).
 
Upvote 0
Top