views:

30

answers:

1

I'm developing an application that uses SQLite as the primary data storage method. I have two processes running for my app using an alternate entry point.

I need to access the same DB from the two different processes but as we all now SQLite is not like a server DB engine, it can only be accessed once at a time.

I wanted to know if there is a way to kind of "lock" the DB when it's being accessed by other process so that if the second process tries to acces the DB at the same time, it would wait until the first process finishes and then try to access it again.

How can this issue be treated?

+1  A: 

If you have not already, create a class that abstracts your database access out and store it in the RuntimeStore. From wherever you are going to interface with SQLite, get a reference to that class using the GUID you stored it with (RuntimeStore.get(long)) and synchronize the class however you would normally (member object lock, synchronized methods).

Do NOT just use the Wikipedia style singleton pattern as it is not a true singleton across processes on this platform.

See:

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/system/RuntimeStore.html

Sample:

class SQLManager {
    private static long GUID = 0xa178d3ce564cae69L; // hash of com.stackoverflow.SQLManager

    private SQLManager() {
        // ctor stuff here
    }

    public static SQLManager getInstance() {
        RuntimeStore rs = RuntimeStore.getRuntimeStore();

        SQLManager instance = rs.get(GUID);

        if (instance == null) {
            instance = new SQLManager();
            rs.put(GUID, instance);
        }

        return instance;
    }
}

You're still using the singleton "pattern" per se, but you're storing the object instance in the RuntimeStore on first getInstance call, and subsequently pulling it form the RuntimeStore - using a GUID that you specify.

Doug
Thanks Doug!, I just read the article at Wiki for singletons, was a new term for me but I think I now undesrtand it. Why is it that this doesn't work on Blackberry? Is this feature not usable in the Java ME?I will now read about the RuntimeStore since I'm not familiar with it. I would appreciate if you had some code sample but I will still be reading about the Runtime StoreThanks a lot!
Curro
By the way, have you used the SQLITE_BUSY flag @ Blackberry? I'm not sure if this can be a solution. Here is an examplehttp://stackoverflow.com/questions/2775595/sqlite-multi-process-access
Curro
Hey Doug, I read about the RuntimeStore but it's not very clear in this case which is the objet that I should be wrapping at the RuntimeStore. I created a class called SQLManager that handles all the DB events (open, insert, update, create, etc.), should I wrap this object? I think I undesrtand the RuntimeStore concept but it's not very clear the implementation in this case. =/ , any help would be very appreciated. Thanks in advance
Curro
I don't know about Java ME as a whole (A quick search did not turn up any concerns), but with BlackBerry the singleton instance is only unique in the scope of the process calling getInstance, so you end up with multiple singletons :) I have not used the SQLITE_BUSY flag, sorry. From the sounds of it, yes, you would put your SQLManager in the RuntimeStore. I will edit my answer with a code sample.
Doug
It worked just by storing a boolean flag at the RuntimeStore to indicate if the DB file is being used or not, thanks Doug!!
Curro