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

Constructor Summary
OpenHelperManager()
           
 
Method Summary
static OrmLiteSqliteOpenHelper getHelper(android.content.Context context)
          Deprecated. Should use getHelper(Context, Class)
static
<T extends OrmLiteSqliteOpenHelper>
T
getHelper(android.content.Context context, Class<T> openHelperClass)
          Create a static instance of our open helper from the helper class.
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 setHelper(OrmLiteSqliteOpenHelper helper)
          Set the helper for the manager.
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.
 
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

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.


setHelper

public static void setHelper(OrmLiteSqliteOpenHelper helper)
Set the helper for the manager. This is most likely used for testing purposes and should only be called if you _really_ know what you are doing. If you do use it then it should be in a static {} initializing block to make sure you have one helper instance for your application.


getHelper

public static <T extends OrmLiteSqliteOpenHelper> T getHelper(android.content.Context context,
                                                              Class<T> openHelperClass)
Create a static instance of our open helper from the helper class. 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.


getHelper

@Deprecated
public static OrmLiteSqliteOpenHelper getHelper(android.content.Context context)
Deprecated. Should use getHelper(Context, Class)

Similar to getHelper(Context, Class) (which is recommended) except we have to find the helper class through other means. This method requires that the Context be a class that extends one of ORMLite's Android base classes such as OrmLiteBaseActivity. Either that or the helper class needs to be set in the strings.xml.

To find the helper class, this does the following:
1) If the class has been set with a call to setOpenHelperClass(Class), it will be used to construct a helper.
2) If the resource class name is configured in the strings.xml file it will be used.
3) 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.
4) An exception is thrown saying that it was not able to set the helper class.


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.



This content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.