DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Concurrency error while accessing to db

Open arturonaredo opened this issue 11 years ago • 5 comments

Hi,

an exception was raised to me while I was trying to access to the database trhough multiple threads, It's almost imposible to reproduce bug I have the trace if you need it or if is useful to you somehow.

java.lang.RuntimeException: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): retrycount exceeded at com.raizlabs.android.dbflow.runtime.DBTransactionQueue.run(DBTransactionQueue.java:75) Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): retrycount exceeded at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(SQLiteConnection.java) at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845) at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836) at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62) at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144) at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133) at com.raizlabs.android.dbflow.sql.SqlUtils.hasData(SqlUtils.java:209) at com.raizlabs.android.dbflow.sql.language.Where.hasData(Where.java:379) at com.glownet.next.entities.CurrentEvent$Adapter.exists(CurrentEvent$Adapter.java:109) at com.glownet.next.entities.CurrentEvent$Adapter.exists(CurrentEvent$Adapter.java:12) at com.raizlabs.android.dbflow.sql.SqlUtils.save(SqlUtils.java:227) at com.raizlabs.android.dbflow.structure.ModelAdapter.save(ModelAdapter.java:54) at com.raizlabs.android.dbflow.structure.BaseModel.save(BaseModel.java:54) at com.raizlabs.android.dbflow.runtime.transaction.process.SaveModelTransaction.processModel(SaveModelTransaction.java:33) at com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelHelper$1.run(ProcessModelHelper.java:33) at com.raizlabs.android.dbflow.runtime.TransactionManager.transact(TransactionManager.java:108) at com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelHelper.process(ProcessModelHelper.java:28) at com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelInfo.processModels(ProcessModelInfo.java:159) at com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelTransaction.onExecute(ProcessModelTransaction.java:43) at com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelTransaction.onExecute(ProcessModelTransaction.java:13) at com.raizlabs.android.dbflow.runtime.DBTransactionQueue.run(DBTransactionQueue.java:61)

arturonaredo avatar Aug 12 '15 10:08 arturonaredo

@arturonaredo this looks like a database concurrency issue. Not a DBFlow issue. In this case you'll need A. put everything on the DBTransactionQueue or B. Solve the concurrency issue and evaluate how the data is accessed.

agrosner avatar Aug 20 '15 22:08 agrosner

closing this

agrosner avatar Aug 20 '15 22:08 agrosner

While I really like the concept of DBFlow in general and love working with this library so far, I encountered this issue as well. I am aware that you solve concurrency issues by using your own DBTransactionQueue, but I prefer to keep thread management in the domain of my application code. Therefore I am using TransactionManager.transact to perform my write operations. According to my research and my previous experiences with Android databases, accessing (especially writing to) an Android SQLite database from multiple threads should be perfectly fine, as long as there is exactly ONE database connection used. Otherwise the mentioned SQLiteDatabaseLockedException is thrown. I looked at the source code and I think that synchronizing BaseDatabaseDefinition#getHelper() should fix the issue of having multiple connections (aka. SQLiteOpenHelpers). Currently I synchronize a function wrapping the transaction call in my database component. It works as long as all writing operation are performed using this wrapper method. Therefore I would really prefer DBFlow to be thread safe (even without using the queue). Any thoughts on that?

BernhardReu avatar Jan 12 '16 10:01 BernhardReu

@BernhardReu I apologize for not seeing this. It may have gotten buried under a thread on this issue being closed. That would make sense if you're accessing the DB from multiple threads. I will add that in next version.

agrosner avatar Mar 14 '16 17:03 agrosner

in another issue already.

agrosner avatar Mar 14 '16 17:03 agrosner