You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have two processes, one is my main app, and the other is a SyncAdapter that runs in the 'sync' process. Both of these processes are writing to the database, and I end up getting a database locked exception (stack trace below). What's the best way to handle this situation? Is there a way to check if the database is locked before starting a transaction?
<12-05 18:06:35.249: E/AndroidRuntime(25298): FATAL EXCEPTION: SyncAdapterThread-1 12-05 18:06:35.249: E/AndroidRuntime(25298): Process: com.example.test.prod:sync, PID: 25298 12-05 18:06:35.249: E/AndroidRuntime(25298): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:555) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteSession.beginTransactionUnchecked(SQLiteSession.java:323) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteSession.beginTransaction(SQLiteSession.java:298) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:505) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:416) 12-05 18:06:35.249: E/AndroidRuntime(25298): at com.activeandroid.ActiveAndroid.beginTransaction(ActiveAndroid.java:64) 12-05 18:06:35.249: E/AndroidRuntime(25298): at com.example.contacts.ContactSyncAdapter.onPerformSync(ContactSyncAdapter.java:33) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)>
The text was updated successfully, but these errors were encountered:
@jlhonora I ended up using FileLocks to lock a file shared across two processes, code below. Also, thanks for the tips! I'll take a look at them.
try {
// Get a file channel for the fileFilefile = newFile(LOCK_FILE);
FileChannelchannel = newRandomAccessFile(file, "rw").getChannel();
// Use the file channel to create a lock on the file.// This method blocks until it can retrieve the lock.FileLocklock = channel.lock();
ActiveAndroid.beginTransaction();
try{
//do somethingActiveAndroid.setTransactionSuccessful();
}finally{
ActiveAndroid.endTransaction();
}
// Release the locklock.release();
// Close the filechannel.close();
} catch (Exceptione) {
e.printStackTrace();
}
I have two processes, one is my main app, and the other is a SyncAdapter that runs in the 'sync' process. Both of these processes are writing to the database, and I end up getting a database locked exception (stack trace below). What's the best way to handle this situation? Is there a way to check if the database is locked before starting a transaction?
<12-05 18:06:35.249: E/AndroidRuntime(25298): FATAL EXCEPTION: SyncAdapterThread-1 12-05 18:06:35.249: E/AndroidRuntime(25298): Process: com.example.test.prod:sync, PID: 25298 12-05 18:06:35.249: E/AndroidRuntime(25298): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:555) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteSession.beginTransactionUnchecked(SQLiteSession.java:323) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteSession.beginTransaction(SQLiteSession.java:298) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:505) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:416) 12-05 18:06:35.249: E/AndroidRuntime(25298): at com.activeandroid.ActiveAndroid.beginTransaction(ActiveAndroid.java:64) 12-05 18:06:35.249: E/AndroidRuntime(25298): at com.example.contacts.ContactSyncAdapter.onPerformSync(ContactSyncAdapter.java:33) 12-05 18:06:35.249: E/AndroidRuntime(25298): at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)>
The text was updated successfully, but these errors were encountered: