I've created an Android IntentService that downloads data from a remote server and stores in a SQLiteDatabase. When I run the service the first time everything runs perfectly but the second time I try to run it ends up hanging and I'm not too sure why.
c = db.rawQuery("SELECT * FROM abc WHERE def = ? AND ghi = 1", new String[]{Integer.toString(uid)});
It's this line where it hangs so I've a feeling it's something to do with the database being locked and it's wait for it to unlock but I could be totally wrong. I had previously been getting locking exceptions but they appear to have gone. It's nothing to do with the query take a while to execute because normally it executes instantly and I've waited 5 minutes on the second run and it is still hanging
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
private SQLiteDatabase mDatabase;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 10);
this.context = context;
}
public SQLiteDatabase getDB() {
if ((mDatabase == null) || (!mDatabase.isOpen())) {
mDatabase = this.getWritableDatabase();
}
return mDatabase;
}
MyApplication.java
public class MyApplication extends Application {
public static DatabaseHelper helper;
public static SQLiteDatabase database;
public MyApplication() {
super();
}
@Override
public void onCreate(){
super.onCreate();
helper = new DatabaseHelper(this);
database = helper.getWritableDatabase();
}
@Override
public void onTerminate() {
super.onTerminate();
if (database != null && database.isOpen()) {
database.close();
}
}
}
All database access is done using the one database object in MyApplication. Any insight into the query hanging would be appreciated.
Aucun commentaire:
Enregistrer un commentaire