|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.j256.ormlite.android.apptools.OpenHelperManager
public class OpenHelperManager
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()
.
Constructor Summary | |
---|---|
OpenHelperManager()
|
Method Summary | ||
---|---|---|
static OrmLiteSqliteOpenHelper |
getHelper(android.content.Context context)
Deprecated. Should use getHelper(Context, Class) |
|
static
|
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 |
---|
public OpenHelperManager()
Method Detail |
---|
public static void setOpenHelperClass(Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
OrmLiteBaseActivity
type classes then you will need to call this in a static
method in your code.
public static void setHelper(OrmLiteSqliteOpenHelper helper)
public static <T extends OrmLiteSqliteOpenHelper> T getHelper(android.content.Context context, Class<T> openHelperClass)
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.
@Deprecated public static OrmLiteSqliteOpenHelper getHelper(android.content.Context context)
getHelper(Context, Class)
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.
@Deprecated public static void release()
releaseHelper()
.
public static void releaseHelper()
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |