com.j256.ormlite.android.apptools
Class OpenHelperManager

java.lang.Object
  extended by com.j256.ormlite.android.apptools.OpenHelperManager

public class OpenHelperManager
extends Object

This helps organize and access database connections to optimize connection sharing. There are several schemes to manage the database connections in an Android app, but as an app gets more complicated, there are many potential places where database locks can occur. This class allows database connection sharing between multiple threads in a single app. This gets injected or called with the OrmLiteSqliteOpenHelper class that is used to manage the database connection. The helper instance will be kept in a static field and only released once its internal usage count goes to 0. The SQLiteOpenHelper and database classes maintain one connection under the hood, and prevent locks in the java code. Creating multiple connections can potentially be a source of trouble. This class shares the same connection instance between multiple clients, which will allow multiple activities and services to run at the same time. Every time you use the helper, you should call getHelper(Context) or getHelper(Context, Class). When you are done with the helper you should call releaseHelper().

Author:
graywatson, kevingalligan

Nested Class Summary
static interface OpenHelperManager.SqliteOpenHelperFactory
          Deprecated. We are using other mechanisms now to inject the helper class. See getHelper(Context).
 
Constructor Summary
OpenHelperManager()
           
 
Method Summary
static OrmLiteSqliteOpenHelper getHelper(android.content.Context context)
          Create a static instance of our open helper.
static OrmLiteSqliteOpenHelper getHelper(android.content.Context context, Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
          Like getHelper(Context) but sets the helper class beforehand.
static void release()
          Deprecated. This has been renamed to be releaseHelper().
static void releaseHelper()
          Release the helper that was previously returned by a call getHelper(Context) or getHelper(Context, Class).
static void setOpenHelperClass(Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
          If you are _not_ using the OrmLiteBaseActivity type classes then you will need to call this in a static method in your code.
static void setOpenHelperFactory(OpenHelperManager.SqliteOpenHelperFactory factory)
          Deprecated. You should either use setOpenHelperClass(Class) or call getHelper(Context).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OpenHelperManager

public OpenHelperManager()
Method Detail

setOpenHelperFactory

@Deprecated
public static void setOpenHelperFactory(OpenHelperManager.SqliteOpenHelperFactory factory)
Deprecated. You should either use setOpenHelperClass(Class) or call getHelper(Context).

Set the manager with your own helper factory.


setOpenHelperClass

public static void setOpenHelperClass(Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
If you are _not_ using the OrmLiteBaseActivity type classes then you will need to call this in a static method in your code.


getHelper

public static OrmLiteSqliteOpenHelper getHelper(android.content.Context context)
Create a static instance of our open helper. This has a usage counter on it so make sure all calls to this method have an associated call to releaseHelper(). This should be called during an onCreate() type of method when the application or service is starting. The caller should then keep the helper around until it is shutting down when releaseHelper() should be called.

If multiple parts of your application need the helper, they call can call this as long as they each call release when they are done.

To find the helper class, this does the following:
1) If the factory class (albeit deprecated) was injected it will be used to get the helper.
2) If the class has been set with a call to setOpenHelperClass(Class), it will be used to construct a helper.
3) If the resource class name is configured in the strings.xml file it will be used.
4) The context class hierarchy is walked looking at the generic parameters for a class extending OrmLiteSqliteOpenHelper. This is used by the OrmLiteBaseActivity and other base classes.
5) An exception is thrown saying that it was not able to set the helper class.


getHelper

public static OrmLiteSqliteOpenHelper getHelper(android.content.Context context,
                                                Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
Like getHelper(Context) but sets the helper class beforehand.


release

@Deprecated
public static void release()
Deprecated. This has been renamed to be releaseHelper().


releaseHelper

public static void releaseHelper()
Release the helper that was previously returned by a call getHelper(Context) or getHelper(Context, Class). This will decrement the usage counter and close the helper if the counter is 0.

WARNING: This should be called in an onDestroy() type of method when your application or service is terminating or if your code is no longer going to use the helper or derived DAOs in any way. _Don't_ call this method if you expect to call getHelper(Context) again before the application terminates.



Copyright © 2011. All Rights Reserved.