The operation cannot be completed because there is no current transaction when inserting into the database

In my Android application, I have to insert data into several SQLite tables from different threads (one thread to insert into one table and 5 tables). There is a lot of data, so I use beginTransaction()setTransactionSuccessful()endTransaction();in each thread, and all threads start at the same time, but in the second or sometimes third thread I always got this exception:

enter image description here

I am using a single SQLite (singleton) connection, as mentioned here , but this problem still remains. So I hope for some help. Thanks in advance!

PS And if I have race conditions, what other way to use for multi-threaded insertion?

+5
source share
1 answer

I think your problem is the state of the race. Since you have several threads that start and end a transaction, you can get something like the following operations:

beginTransaction()
// this may be a noop because a transaction is already open
beginTransaction()
setTransactionSuccessful()
setTransactionSuccessful()
endTransaction()
// this may throw because the previous end closes the transaction
endTransaction()

I do not think that Sqlite supports several transactions open in one connection, and, as you indicate, several connections are impossible.

, , . - , synchronized, , , . , , BlockingQueue, .

+5

All Articles