B4J Question Database Connections Pooling and Threading

aeric

Expert
Licensed User
Longtime User
The pool is responsible for maintaining the connections.
An alternative for a connections pool is a process global SQL variable. However for it to work properly all the handlers should be single threaded handlers.
Referring to the tutorial
'[Server] Building web servers with B4J'
https://www.b4x.com/android/forum/threads/server-building-web-servers-with-b4j.37172/

I am now confused with this statement. No problem to use connection pool for MySQL and set the SingleThreaded to False by default.

B4X:
srvr.AddHandler("/hello", "HelloPage", False)

But then now if I use SQLite for the backend, should I set SingleThreaded as True?
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Hi aeric, the single thread is only for the server handler it has nothing to do with the database, the driver and the pooler are in charge of managing that info,
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
But then now if I use SQLite for the backend, should I set SingleThreaded as True?
No. Just don't use pooling with SQLite. For SQLite, use the InitializeSQLite method to retrieve your SQL object. Use this single object for all your DB requests. @Erel has provided the code to ensure that only one NonQuery statement will be executed at any given time. This is how I do it in my modified jRDC2 server.

Note: you also need to enable WAL mode

Sources:
WAL mode: Thread '[WebApp] Concurrent access to SQLite databases' https://www.b4x.com/android/forum/threads/webapp-concurrent-access-to-sqlite-databases.39904/
B4J's SQL: https://github.com/AnywhereSoftware...SQL/src/anywheresoftware/b4j/objects/SQL.java
My jRDC2 mod: https://www.b4x.com/android/forum/threads/modded-jrdc2-w-sqlite-support-and-more.85578/
 
Upvote 1

aeric

Expert
Licensed User
Longtime User
So the short answer is No. I understand about pooling, just confused after I read the tutorial again.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
So the short answer is No. I understand about pooling, just confused after I read the tutorial again.
On server apps always set SingleThreaded to false as it refers to the threading method uses on the handler. Setting it to true means that the handler code will run on the main thread. There maybe use cases where it is set to true but I have not encountered this so far.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…