java - Android SQLite Concurrency (trying to reopen an already closed connection) -


this type of question has been asked lot, not find answer satisfies me.

question
how should maintain multiple open sqlite connections on multiple threads?
possible check how many connections open , call close() if none are?

edit
accepted answer below sqlite fast , synchronizing sql api methods (only 1 run @ time) may not hurting.
being said, i'll still happy hear solution synchronize read()s , write()s, multiple read() can done @ once...

background
i'm building android app , in places use few internet connections (get requests).
when requests finish, threads access sqlite, writing.
when threads try connect simultaneously, error mentioned in title.

i know why happens:
way it, each "sqlite api" method (api wrote) starting opening connection , finishes closing it. write/read open() used whenever needed.

so when 1 thread closes connection (the api method finishes) , other still using cursor or whatever, error occurs , exception thrown.

here code example demonstrate:

public class mysql{     private sqliteopenhelper mhelper;     private sqlitedatabase mydatabase;     private static mysql myinstance;     ...      //singleton     public synchronized static mysql getinstance(context context){         if(myinstance == null){             myinstance = new mysql(context);         }         synchronized (myinstance) {             return myinstance;         }     }      public sqlitedatabase openread() throws sqlexception{         return mydatabase = mhelper.getreadabledatabase();     }       public sqlitedatabase openwrite() throws sqlexception{          return mydatabase = mhelper.getwritabledatabase();     }      //close helper     public void close(){         mhelper.close();     }      public static void createsomeentry(){          openwrite();          mydatabase.query(...);          ...          close();     }      public static void getsomeentry(){          openread();          cursor c = mydatabase.query(...);          ...//use cursor in loop          close();     } } 

so let's two-combination of these method run @ same time different threads.

as can see, try synchronize use/hold of 'myinstance' hope long 1 thread holds variable, it's 1 can use it.
doesn't work apparently, , slow down anyway, prefer better.

how fix logic?

the java driver sqlite not support true concurrent multithreaded access. it's "thread safe" in sense use multiple threads should not corrupt data. think best can open single connection, , have each thread lock connection while using it, giving lock when it's done. i'm not familiar android programming specifically, following might work:

public class mysql{     private static mysql myinstance;     private sqliteopenhelper mhelper;     private sqlitedatabase mydatabase;     ...      //initialize     public synchronized static void initialize(context context){         if(myinstance == null){             myinstance = new mysql(context);         }     }      public synchronized static void createsomeentry() {         myinstance.mydatabase = myinstance.mhelper.getwritabledatabase();         myinstance.mydatabase.query(...);         ...         myinstance.mhelper.close();     }      public synchronized static void getsomeentry() {         myinstance.mydatabase = myinstance.mhelper.getreadabledatabase();         cursor c = myinstance.mydatabase.query(...);         ...//use cursor in loop         myinstance.mhelper.close();     } } 

note replacement of getinstance() initialize function doesn't return anything, , elimination of other public functions except createsomeentry() , getsomeentry(). since public methods synchronized on class, 1 thread can use database @ time, should resolve issue of sqlite not supporting concurrent access.

if need concurrent access, think choices either figure out how access sqlite database multiple processes rather multiple threads, approach worked needs, or switch database. suppose make driver changes make driver multithreaded.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -