I have a very strange problem. It only shows from time to time, on several devices. Can't seem to reproduce it when I want, but had it so many times, that I think I know where I get it.
So I have a Loader which connects to sqlite through a singleton SQLiteOpenHelper:
try {
Log.i(TAG, "Get details offline / db helper: "+DatabaseHelper.getInstance(getContext()));
SQLiteDatabase db=DatabaseHelper.getInstance(this.getContext()).getWritableDatabase();
Log.i(TAG, "Get details offline / db: "+db);
//doing some work on the db...
} catch(SQLiteException e){
e.printStackTrace();
return null;
} catch(Exception e){
e.printStackTrace();
return null;
//trying everything to grab some exception or whatever
}
My SQLIteOpenHelper looks something like this:
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper mInstance = null;
private static Context mCxt;
public static DatabaseHelper getInstance(Context cxt) {
//using app context as suggested by CommonsWare
Log.i("DBHELPER1", "cxt"+mCxt+" / instance: "+mInstance);
if (mInstance == null) {
mInstance = new DatabaseHelper(cxt.getApplicationContext());
}
Log.i("DBHELPER2", "cxt"+mCxt+" / instance: "+mInstance);
mCxt = cxt;
return mInstance;
}
//private constructor
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.mCxt = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
//some tables created here
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//upgrade code here
}
}
It really works great in most cases. But from time to time I get a log similar to this:
06-10 23:49:59.621: I/DBHELPER1(26499): cxtcom.myapp#407152c8 / instance: com.myapp.helpers.DatabaseHelper#40827560
06-10 23:49:59.631: I/DBHELPER2(26499): cxtcom.myapp#407152c8 / instance: com.myapp.helpers.DatabaseHelper#40827560
06-10 23:49:59.631: I/DetailsLoader(26499): Get event details offline / db helper: com.myapp.helpers.DatabaseHelper#40827560
06-10 23:49:59.631: I/DBHELPER1(26499): cxtcom.myapp#407152c8 / instance: com.myapp.helpers.DatabaseHelper#40827560
06-10 23:49:59.651: I/DBHELPER2(26499): cxtcom.myapp#407152c8 / instance: com.myapp.helpers.DatabaseHelper#40827560
This line Log.i(TAG, "Get details offline / db: "+db); never gets called! No Exceptions, silence. Plus, the thread with the Loader is not running anymore.
So nothing past the line:
SQLiteDatabase db=DatabaseHelper.getInstance(this.getContext()).getWritableDatabase();
gets executed.
What can possibly go wrong on this line?
Related
I'm trying to create a backup of my sqlite database and I want to flush the content of the WAL file in the db first.
Here is my SQLiteOpenHelper:
public class MyDBHelper extends SQLiteOpenHelper {
private Context mContext;
private static MyDBHelper mInstance = null;
private MyDBHelper(final Context context, String databaseName) {
super(new MYDB(context), databaseName, null, DATABASE_VERSION);
this.mContext = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static MyDBHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyDBHelper(context, DATABASE_NAME);
}
return mInstance;
}
private void closeDataBase(Context context) {
getInstance(context).close();
}
}
Now, my understanding is that after a checkpoint is completed, the mydb.db-wal file should be empty. Is that correct?
Here is what I've tried so far:
1.
public Completable flushWalInDB() {
return Completable.fromAction(new Action() {
#Override
public void run() throws Exception {
getInstance(mContext).getReadableDatabase().rawQuery("pragma wal_checkpoint;", null);
}
});
}
This doesn't throw an error but doesn't seem to do anything. After running this, I physically checked my mydb.db-wal file and had the same size. I also checked the db on the device and nothing was added in the database
After some digging around I found this
[https://stackoverflow.com/a/30278485/2610933][1]
and tried this:
2.
public Completable flushWalInDB() {
return Completable.fromAction(new Action() {
#Override
public void run() throws Exception {
getInstance(mContext).getReadableDatabase().execSQL("pragma wal_checkpoint;");
}
});
}
When running this it throws an error:
unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only.
And based on this answer [https://stackoverflow.com/a/19574341/2610933][1] , I also tried to VACUUM the DB but nothing seems to happen.
public Completable vacuumDb() {
return Completable.fromAction(new Action() {
#Override
public void run() throws Exception {
getInstance(mContext).getReadableDatabase().execSQL("VACUUM");
}
});
}
}
Whats is the correct way of flushing the WAL file in the DB before creating a backup?
Thank you.
PRAGMA wal_checkpoint(2) does copy all data from the WAL into the actual database file, but it does not remove the -wal file, and any concurrent connections can make new changes right afterwards.
If you want to be really sure that there is no WAL to interfere with your backup, run PRAGMA journal_mode = DELETE. (You can switch it back afterwards.)
To manually add checkpont use PRAGMA wal_checkpoint, after searching for 2 hours following code worked for me -:
SQLiteDatabase db = this.getWritableDatabase();
String query = "PRAGMA wal_checkpoint(full)";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
int a = cursor.getInt(0);
int b = cursor.getInt(1);
int c = cursor.getInt(2);
}
if (cursor != null) {
cursor.close();
}
I am using ORMlite database for the first time in my application. i have taken reference from a tutorial, but instead of doing all the things exactly same i am not able to resolve an error. My code is below:-
DatabaseHelper:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "qzeodemo.db";
private static final int DATABASE_VERSION = 1;
private Dao<ModifierDetails, Integer> itemsDao;
private Dao<ItemDetails, Integer> modifiersDao;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
#Override
public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
try {
// Create tables. This onCreate() method will be invoked only once of the application life time i.e. the first time when the application starts.
TableUtils.createTable(connectionSource,ItemDetails.class);
TableUtils.createTable(connectionSource,ModifierDetails.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e);
}
}
#Override
public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) {
try {
// In case of change in database of next version of application, please increase the value of DATABASE_VERSION variable, then this method will be invoked
//automatically. Developer needs to handle the upgrade logic here, i.e. create a new table or a new column to an existing table, take the backups of the
// existing database etc.
TableUtils.dropTable(connectionSource, ItemDetails.class, true);
TableUtils.dropTable(connectionSource, ModifierDetails.class, true);
onCreate(sqliteDatabase, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new "
+ newVer, e);
}
}
// Create the getDao methods of all database tables to access those from android code.
// Insert, delete, read, update everything will be happened through DAOs
public Dao<ItemDetails,Integer> getItemDao() throws SQLException {
if (itemsDao == null) {
itemsDao = getDao(ItemDetails.class);
}
return itemsDao;
}
public Dao<ModifierDetails, Integer> getMofifierDao() throws SQLException {
if (modifiersDao == null) {
modifiersDao = getDao(ModifierDetails.class);
}
return modifiersDao;
}
}
The line where i am using modifiersDao = getDao(ModifierDetails.class); is giving error
Error:(76, 30) error: invalid inferred types for D; inferred type does not conform to declared bound(s)
inferred: Dao
bound(s): Dao
where D,T are type-variables:
D extends Dao declared in method getDao(Class)
T extends Object declared in method getDao(Class)
Please help :(
Your declaration is wrong above:
private Dao< ItemDetails, Integer > modifiersDao;
but getMofifierDao() returns Dao< ModifierDetails, Integer>
I'm trying to get a pattern that doesn't fail for a multithreaded access to my sqlite database. Also, what is driving me nuts is that I can't reproduce the issue.
I have an app which uses a DB, but also Android Accounts and Android sync to sync my app's data. My guess is that when the two happen a the same time, it crashes. I'm getting a lot of errors like:
* android.database.sqlite.SQLiteDatabaseLockedException: database is locked
* android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
* android.database.sqlite.SQLiteDatabaseLockedException: error code 5: database is locked
* android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
* android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 778)
* android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/net.bicou.redmine/databases/redmine.db' to 'en_US'. \n Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
Maybe not all of them are related to the same root cause, however I'm kind of lost.
What I have is:
an abstract base class, DbAdapter, that is extended by subclasses which want to manage a single table
a class that manages the SQLite database, called DbManager, which contains a Lock
Right now the users have a version of the DbManager that is not a singleton. I'm planning to make DbManager a singleton, so that all threads share the same object. This shouldn't be a problem, because as far as I have understood/seen, the background sync and app share the same process.
Here are the classes (only the relevant parts):
public abstract class DbAdapter {
Context mContext;
protected DbManager mDbManager;
SQLiteDatabase mDb;
public static final String KEY_ROWID = "_id";
public DbAdapter(final Context ctx) {
mContext = ctx;
}
public DbAdapter(final DbAdapter other) {
mContext = other.mContext;
mDb = other.mDb;
mDbManager = other.mDbManager; // removed with singleton version
}
public synchronized DbAdapter open() throws SQLException {
if (mDb != null) {
return this;
}
mDbManager = new DbManager(mContext); // currently in production
mDbManager = DbManager.instance(mContext); // currently investigating this singleton solution
try {
mDb = mDbManager.getWritableDatabase();
} catch (final SQLException e) {
L.e("Unable to open DB, trying again in 1 second", e);
try {
Thread.sleep(1000);
} catch (final InterruptedException e1) {
L.e("Could not wait 1 second " + e1);
}
mDb = mDbManager.getWritableDatabase();// This may crash
}
return this;
}
public synchronized void close() {
mDbManager.close();
mDbManager = null;
mDb = null;
}
}
A class that needs to handle a database table will extend DbAdapter, and implement methods such as select, insert, delete, etc.
Here's the DB manager:
public class DbManager extends SQLiteOpenHelper {
private static final String DB_FILE = "db";
private static final int DB_VERSION = 15;
Context mContext;
Lock mLock = new ReentrantLock();
// Currently in prod
public DbManager(final Context context) {
super(context, DB_FILE, null, DB_VERSION);
mContext = context;
}
// singleton version will make this constructor private and add:
private static DbManager mInstance;
public static synchronized DbManager instance(Context context) {
if (instance == null) {
instance = new DbManager(context);
}
return instance;
}
#Override
public SQLiteDatabase getWritableDatabase() {
mLock.lock();
return super.getWritableDatabase();
}
#Override
public void close() {
super.close();
mLock.unlock();
}
#Override
public void onCreate(final SQLiteDatabase db) {
// ...
}
#Override
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
// ...
}
private void createTables(final SQLiteDatabase db, final String[] statements) {
for (final String sql : statements) {
try {
db.execSQL(sql);
} catch (final Exception e) {
L.e("Unable to create table: " + sql, e);
}
}
}
}
OK, now, the questions.
Is my lock properly implemented? I'm really new to this, I don't know if the ReentrantLock is a good choice, and if I'm locking/unlocking at the right moment
Are my synchronized method properly implemented? I mean, I have placed the synchronized keyword around methods that I don't want interrupted by concurrent threads. Is this right? Can you advice on my synchronized use?
How can I reproduce the issue? I have created a test that uses 3 threads that make concurrent read/write access to the DB, and use some Thread.sleep to ensure that the db open/close from each thread overlap, but it doesn't crash. This is really bugging me, I don't think there is a lot of people that have the issue, so I don't know how to reproduce.
Is my DbAdapter + DbManager technical choice a good idea? Is there a better pattern?
Is it a good idea to make DbManager a singleton?
For multiple threads accessing, it is advisable to use the singleton pattern.
Such a way, successive calls to the same database will be seamlessly serialised.
However, it's not impossible to have some NullPointerExceptions on inserts. So, to expand your "Thread.sleep" logic, you could use this code:
#Override
public SQLiteDatabase getWritableDatabase() {
while (true) {
try {
return super.getWritableDatabase();
} catch (SQLiteDatabaseLockedException e) {
System.err.println(e);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println(e);
}
}
}
I'm trying to change my DatabaseInterface implementation by opening it in MyApplication extends Application and basically never closing it (I did some research and a Google engineer recommends it, see the linked question Best place to close database connection). The modifications I have made are quite minor but now the system throws me the following error when opening the database:
E/SQLiteDatabase(15690): android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
Here is relevant code
MyApplication.java
#Override
public void onCreate() {
super.onCreate();
try {
DatabaseInterface.open();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
DatabaseInterface.java
public class DatabaseInterface extends SQLiteOpenHelper {
private final static String DB_PATH = Constants.DB_PATH;
private final static String DB_NAME = Constants.DB_NAME;
private static SQLiteDatabase mDatabase;
private Context mContext;
private static DatabaseInterface mInstance;
private DatabaseInterface(Context context) {
super(context, DB_NAME, null, 1);
this.mContext = context;
}
public static DatabaseInterface getInstance(Context context) {
if (mInstance == null) {
mInstance = new DatabaseInterface(context);
}
return mInstance;
}
public static void open() throws SQLException, IOException {
//Open the database
String path = DB_PATH + DB_NAME;
mDatabase = SQLiteDatabase.openOrCreateDatabase(new File(path), null);
}
}
Ok this was caused by me forgetting to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to my AndroidManifest.xml as my database reside on the phone's external storage.
When trying to open up a DB connection via the normal mechanism a nullpointerexecption is thrown at the point dbhelper.getWritableDatabase.
The problem seems to stem from the IntentService context I am passing in.
public AnalysisService() {
super("AnalysisService");
Log.d("ANALYSIS_SERVICE", "Service started");
try{
db = new DBAdapter(this)
db.openDB();
}catch(Exception e){
Log.d("ANALYSIS_SERVICE", Arrays.toString(e.getStackTrace()));
Log.e("ANALYSIS_SERVICE", e.toString());
}
}
The db.open method is executed here:
public class DBAdapter implements Database {
protected Context context;
protected SQLiteDatabase db;
private DatabaseHelper dbHelper;
public DBAdapter(Context context) {
this.context = context;
Log.e("DB_ADAPTER", "CREATED");
}
/**
* Opens a database connection.
* #return
* #throws SQLException
*/
#Override
public DBAdapter openDB() throws SQLException {
dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase(); //NULLP EXCEPTION THROWN HERE
return this;
}
And if it helps the constructor for the dbHelper is:
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.e("DB_HELPER", "created");
}
This has been puzzling me for the last day or so I can't understand why the context is invalid. Also I have logged all the constructors and all the objects are being created correctly, so it is not the case that DBHelper is null.
As well as using the context as this, I have also tried using getBaseContext() and getApplicationContext(), both resulting in the same error.
From the debug printing the stack trace, I get:
11-21 13:23:45.419: D/ANALYSIS_SERVICE(3930):
[android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221),
android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:166),
com.emotifi.database.DBAdapter.openDB(DBAdapter.java:42),
com.emotifi.analysis.AnalysisService.<init>(AnalysisService.java:45),
java.lang.Class.newInstanceImpl(Native Method),
java.lang.Class.newInstance(Class.java:1319),
android.app.ActivityThread.handleCreateService(ActivityThread.java:2234),
android.app.ActivityThread.access$1600(ActivityThread.java:123),
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201),
android.os.Handler.dispatchMessage(Handler.java:99),
android.os.Looper.loop(Looper.java:137),
android.app.ActivityThread.main(ActivityThread.java:4424),
java.lang.reflect.Method.invokeNative(Native Method),
java.lang.reflect.Method.invoke(Method.java:511),
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817),
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584),
dalvik.system.NativeStart.main(Native Method)]
You're setting the context too early in the IntentService lifecycle.
Override onCreate and set it there:
#Override
public void onCreate() {
super.onCreate();
Log.d("ANALYSIS_SERVICE", "Service started");
db = DatabaseFactory.getDefault(this);
}
Then open the database connection in onHandleIntent:
#Override
protected void onHandleIntent(Intent intent) {
// Logging
Log.d("ANALYSIS_SERVICE", "Intent handled");
try {
db.openDB();
} catch (Exception e) {
Log.d("ANALYSIS_SERVICE", "Unable to open database");
e.printStackTrace();
}
}