cancel
Showing results for 
Search instead for 
Did you mean: 

Ultralite for Android -> Multi-threading access to an UL database connection

Former Member
5,122

Ultralite C++ API documentation mentions: "If an application requires multiple threads accessing the UltraLite database, each thread requires a separate connection". But I´ve found nothing about multi-threading in UltraliteJ documentation and my tests do not fail when accessing the same connection through different threads.

I want to access (execute SELECT statements) the same UL connection in many concurrent threads in a Ultralite Android application, is it safe?

Accepted Solutions (1)

Accepted Solutions (1)

philippefbertrand
Participant

We do not recommend the concurrent use of connections. While individual API calls will behave correctly, conncurrent calls will mess up error tracking or cursor positions. UltraLiteJ for Android uses the C++ UltraLite under the covers and even for UltraLiteJ we do not recommend concurrent use of Connections.

You can can create a connection in on thread and "pass it" to another.

Former Member
0 Kudos

According to your answer, if I guarantee that API calls in different threads will not be concurrent (by implementing a mutex control) everything will work fine, right?

philippefbertrand
Participant

The reason why you did not see any issue is that you may have been using the very narrow scenario that lets you get away with it. If you are doing just selects of read only statements, not sharing prepared statements or result sets, you could get away with a connection for all the selects and not see any errors. You would want to use say a second connection (reserving the first for insert, update operations). Again, this is not recommended.

Since you are not sharing prepared statements and result sets, why share the connection? Use the maximum threading you can.

There is already a mutex for the library so adding another around a group of API calls sort of defeats the purpose of having multithreading.

If you are trying to keep the UI thread responsive, one can have a worker thread and queues up stuff for it.

Former Member

I want to share connections because my application use many threads. I can´t open one connection to each thread because there is a limit of 14 UL connections. But, I understand your point. I´m going to change my application to follow your recomendation. Thank you!

Answers (0)