Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveAndroid across multiple processes #311

Open
stabib opened this issue Dec 6, 2014 · 2 comments
Open

ActiveAndroid across multiple processes #311

stabib opened this issue Dec 6, 2014 · 2 comments

Comments

@stabib
Copy link

stabib commented Dec 6, 2014

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)>

@jlhonora
Copy link

jlhonora commented Dec 6, 2014

I have the same setup, and I use the following to check if the db is locked:

if (ActiveAndroid.inTransaction() || ActiveAndroid.getDatabase().isDbLockedByCurrentThread()) {
    return;
}

Besides:

  • Try to make transactions as short as possible
  • I use Update() instead of save() if I want to save a few columns, seems to be faster.

Note that there might be a much better way, I'm not sure if this is the best way to handle this.

@stabib
Copy link
Author

stabib commented Dec 8, 2014

@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 file
            File file = new File(LOCK_FILE);
            FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

            // Use the file channel to create a lock on the file.
            // This method blocks until it can retrieve the lock.
            FileLock lock = channel.lock();
            ActiveAndroid.beginTransaction();
            try{
                //do something
                ActiveAndroid.setTransactionSuccessful();
            }finally{
                ActiveAndroid.endTransaction();
            }

            // Release the lock
            lock.release();

            // Close the file
            channel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants