6
\$\begingroup\$

I have a database-intensive app, almost every activity requires access to the database, both reading and writing.
I had a lot of problems with separate threads and activities on the stack closing connections to the database when the current activity was using it, even if I opened and closed the database immediately before and after use.

So I opted to use a single instance of the database. Now I have no problems, but, is this right?

Application class

public class ApplicationClass extends Application
{
 private DatabaseAdapter PrDatabaseAdapter; 
 public DatabaseAdapter getDatabaseAdapter()
 {
 return PrDatabaseAdapter;
 }
 @Override
 public void onCreate()
 {
 super.onCreate();
 PrDatabaseAdapter = new DatabaseAdapter(this);
 PrDatabaseAdapter.open();
 }
}

Adapter Class The database adapter just sits on top of the DatabaseManager (my SQLiteOpenHelper)

private Context context;
private SQLiteDatabase db;
private DatabaseManager dbManager;
/**
 * The DatabaseAdapter contains all interaction logic for the database
 * @param context
 */
public DatabaseAdapter(Context context)
{
 this.context = context;
}
/**
 * Opens writable database
 * @return DatabaseAdapter;
 * @throws SQLiteException
 */
public DatabaseAdapter open() throws SQLiteException
{
 dbManager = new DatabaseManager(context);
 db = dbManager.getWritableDatabase();
 return this;
}
/**
 * Closes database connection
 */
public void close()
{
 dbManager.close();
}

I'm still learning a lot about Java and Android and I would appreciate some insight to the pros and cons of this approach.

Malachi
29k11 gold badges86 silver badges188 bronze badges
asked Dec 12, 2011 at 11:19
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I'm not familiar with Android, so just some generic Java notes:

  1. PrDatabaseAdapter should be prDatabaseAdapter (with lowercase first letter). See Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions and The Java Language Specification, Java SE 7 Edition, 6.1 Declarations:

    Names of fields that are not final should be in mixed case with a lowercase first letter and the first letters of subsequent words capitalized.

  2. Javadoc comments like @param context aren't too useful, they say nothing more than the source code alone. I'd remove them.

  3. Does it make sense to create a DatabaseAdapter with null context? I'd check this in the constructor and throw and NullPointerException if it's null. See Effective Java, 2nd edition, Item 38: Check parameters for validity

answered Jul 17, 2012 at 8:07
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.