Reputation: 14984
I released an update of my app and am getting a ton of errors from users and I can't recreate it or pin-point the problem.
Two errors I'm getting:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase
java.lang.IllegalStateException: database not open
The error is happening somewhere in the following code:
public DBUserC getUser(int _id){
synchronized (DBAdapter.LOCK){
M.openDB(context);
Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
DBUserC user;
if (cursor.moveToFirst()){
user = cursorToInfo(cursor);
} else{
user = newUser();
}
cursor.close();
M.closeDB();
return user;
}
}
In this version, there is a database upgrade, in which I perform three queries via db.execSql
. It is not in a separate thread.
On every call to the database (except in the onUpgrade
), I synchronize, then open it, run my code, then close it. I was having no problems until this upgrade and can't find the issue.
Any help would be greatly appreciated.
EDIT: To open my databas, I do:
if (helper==null)
helper = new DBAdapter(context);
if (database==null){
database = helper.getWritableDatabase();
} else if (!database.isOpen())
database = helper.getWritableDatabase();
and to close:
helper.close();
helper = null;
database = null;
Example method for getting info:
public DBUserC getUser(int _id){
synchronized (DBAdapter.LOCK){
openDB(context);//this is the open code above
Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
DBUserC user;
if (cursor.moveToFirst()){
user = cursorToInfo(cursor);//does not contain DB operations
} else{
user = newUser(); ////does not contain Database operations
}
cursor.close();
closeDB();//This is the close code above
return user;
}
}
AHost.onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
M.initializeDB(context); //synchronized call to the openDB code I posted above
M.openDataDB(context); //opens a different database by a different file name. This DB is not an issue
//irrelevant ui setup
int _id = pref.getInt(P.eLastUser, VUser.ID_NEW);//row id of user
M.requeryUser();//synchronized, access database
M.switchUser(_id);//synchronized, access database
}
Upvotes: 2
Views: 384
Reputation: 5651
Obviously we need to see the on-upgrade method seems to be the root cause of your issue. I use the Adams Upgrade method I named it for the blog I found it oun.
note: Each upgrade is done in the upgradeTo <= newVersion loop. Maybe someday I'll put a progress bar on this loop.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
int upgradeTo = oldVersion + 1;
while (upgradeTo <= newVersion) {
switch (upgradeTo) {
// update old resrawid's to constants
// this was caused when I saved R values in the database
// and then later realized they would change.
// the old resrawid is changed to a constant.
case 42:
ContentValues values;
@SuppressWarnings("unused")
// used for debugging so I can see the value of the update.
int res;
int rs;
rs = 2130968576;
values = new ContentValues();
values.put(
KEY_RESRAWID,
com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_DRAGON);
res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?",
new String[] { String.valueOf(rs) });
rs = 2130968577;
values = new ContentValues();
values.put(
KEY_RESRAWID,
com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_M119);
res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?",
new String[] { String.valueOf(rs) });
rs = 2130968578;
values = new ContentValues();
values.put(
KEY_RESRAWID,
com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_MEDINA);
res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?",
new String[] { String.valueOf(rs) });
break;
case 43:
// new column added for last_viewed.
db.execSQL(VERSION_43_ALTER);
break;
case 44:
// new index on KEY_DATE_UPDATE DESC
db.execSQL(VERSION_44_CREATE_INDEX);
}
upgradeTo++;
}
}
Good Luck Danny117
Upvotes: 0
Reputation: 394
I have faced the same issue in one of my app, and how i solved that issue is through removing the cursor.close(). Hope it will help :)
Upvotes: 1
Reputation: 14984
I could not recreate issue because in testing, I was upgrading from DB version 3 to version 4. Many of my users were upgrading from version 2 to version 4. Some of the code for upgrading from 2 to 3 was method-based. I ended up changing some of those methods for version 4, which broke the version 3 upgrade. I then was getting a caught exception from a cursorToObject() method I had, which caused the database.close to be skipped and then I got the sqlite exception
Upvotes: 0