Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:53:33+00:00 2026-06-17T19:53:33+00:00

I am creating an Android application using GreenDAO ORM, with a genericized crud service

  • 0

I am creating an Android application using GreenDAO ORM, with a genericized crud service layer on top of that. I have this single file that holds static references to my DaoMaster as well as my SQLiteDatabase:

public class DATABASE {
    private static final String TAG = "DATABASE";
    private static SQLiteDatabase db;
    private static DaoMaster.DevOpenHelper helper;
    private static DaoMaster master;

    public static void Initialize(Context context){
        GetHelper(context);
        GetDatabase();
        GetMaster();
    }

    private static DaoMaster.DevOpenHelper GetHelper(Context context){
        if(helper == null){
            Log.d(TAG, "Helper = null");
            helper =  new DaoMaster.DevOpenHelper(context, Constants.DATABASE_NAME, null);
        }
        return helper;
    }

    private static SQLiteDatabase GetDatabase(){
        if(db == null){
            Log.d(TAG, "Database = null");
            db = helper.getWritableDatabase();
        }
        return db;
    }

    private static DaoMaster GetMaster(){
        GetDatabase();
        if(master == null){
            Log.d(TAG, "Master = null");
            master = new DaoMaster(db);
        }
        return master;
    }

    public static DaoSession GetSession(){
        GetMaster();
        return master.newSession();
    }

    public static void CloseDatabase(){
        try{
            helper.close();
            db = null;
            master = null;
        } catch(Exception e){
            Log.d(TAG, "Failed to close database");
        }
    }
}

Upon launching the app, I call DATABASE.Initialize() and when my main Activity is Destroyed, I call DATABASE.CloseDatabase(). When the app first launches, it performs an initial sync and the entities retrieved are sent through their respective crud service instances, which deals with the respective DAO to persist the entities to this database like so..

public class CrudService<T> {
    private Class<?> _dtoType;
    private Class<T> _entityType;
    private AbstractDao<T, ?> dao;
    private Method _updateMethod;
    private DaoSession session;

    public CrudService(Class<T> entityType){
        _entityType = entityType;
        _dtoType = DtoMapping.getDtoType(entityType); //This gets a dto for the specified entityType via a HashMap - ie: blah -> blahDto

        if(_dtoType != null){
            try {
                _updateMethod = _entityType.getMethod("update", _dtoType); //This finds blah.java's update method that takes in a blahDto as a param - it must be there or you catch
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        session = DATABASE.GetSession();
        dao = (AbstractDao<T,?>) session.getDao(entityType);
    }

    public void Insert(T t){
        dao.insertInTx(t);
    }

    public void InsertOrReplace(T t){
        dao.insertOrReplaceInTx(t);
    }

    public void Update(T t){
        dao.update(t);
    }

    public void Delete(T t){
        dao.deleteInTx(t);
    }
    (etc)
}

However, my periodic syncs need access to this database even when the app is not running. Several of the syncs will run pretty often. When a sync runs, in the class that gets called from the syncs, I call DATABASE.Initialize(), but it gives me this error when it reaches my first crud service query:

01-24 18:10:55.304: WARN/System.err(7068): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.app/databases/database
01-24 18:10:55.304: WARN/System.err(7068): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
01-24 18:10:55.304: WARN/System.err(7068): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1310)
01-24 18:10:55.304: WARN/System.err(7068): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-24 18:10:55.304: WARN/System.err(7068): at de.greenrobot.dao.Query.unique(Query.java:131)
(the rest is where the crud service attempts to query the database and originates in the sync service - omitted for certain reasons)

If anybody can help me figure out what I’m doing wrong, it would be greatly appreciated. I basically just need to know 2 things:

1) Since my variables in DATABASE.java are static, they will be cached until GC runs. Could/should I invoke GC or perhaps finalize(), or will I just have to set them to null in CloseDatabase()?

2) Can I even close my database since my syncs rely upon it and if I don’t close it, won’t I run into leaks?

Thanks in advance.

EDIT: I have looked around and learned that the static variables in fact DO persist until GC runs. So, I have reworded #1.

EDIT 2: I have since updated my DATABASE.java and the calls to its Initialize method to use the application context and it seems to have cleared things up. I am now getting a NullPointerException in that first crud service query when the sync reruns.

EDIT 3: The null pointer exception has now been fixed. I accidentally caused it to open a new database and the entities didn’t exist in it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:53:34+00:00Added an answer on June 17, 2026 at 7:53 pm

    You should think about holding the DaoSession object in Application scope. It simplifies things.

    Passing correct context to greendao's OpenHelper constructor

    https://stackoverflow.com/a/14430803/551269

    PS.: Your CrudService looks a bit like DaoSession.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an android application using Java. I have a boolean variable called
i am creating a new android application.i am using the table layout. I have
When creating an Android application using Loaders, should every activity and fragment have its
I am creating an android application in flash builder 4 using 4.1 SDK on
I am creating an android application in which I am using table layout in
I'm creating an Android application (a game, to be more precise) and using AndEngine
I'm creating an Android application that requires the following: Data from RSS feed gets
I'm creating an Android application that requires me to create a bitmap of the
I am creating an android application that requires me to call is in different
I am creating android/iphone application using Titanium Studio and right now stack with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.