Consider using an Application subclass to manage a singleton handler that is threadsafe. I use something like the code below. Note that the onCreate and onUpgrade connections are special, one-time connections.
larry
-------------------------
public class AppHelper extends OrmLiteSqliteOpenHelper {
private Context context;
public AppHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
// do initialization using db and conn source but do NOT cache them
// do NOT call getWriteableDatabase() here
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
// do initialization using db and conn source but do NOT cache them
// do NOT call getWriteableDatabase() here
}
---------------------------------
http://kagii.squarespace.com/journal/2010/9/10/android-sqlite-locking.html
> clientCount.getAndIncrement();
> if (one == null)
> one = getHelper();
I notice that you are using AtomicInteger for the clientCount but that the DatabaseHelper one is neither AtomicReference, volatile, or synchronized. Seems like there are a number of race conditions around one and whether it has been initialized or closed.
> int count = clientCount.decrementAndGet();
> if (count == 0) {
> Log.i("prototype", "oneSource closeHelper closing helper");
> one.close();
For example, shouldn't 'one' be set to null here? What happens if it has been closed and another thread calls getHelper()?
> 01-10 12:06:07.953: ERROR/Error(23196):
> java.lang.IllegalStateException: you must call initialize() before you
> can use the dao
Can you post the entire stack trace? This indicates to me that the connectionSource has been closed in the OrmLiteSqliteOpenHelper already and it is null. Once it is null then the DAO thinks that it has not be initialized, hence the error.
gray