Java.lang.IllegalStateException:attempt to re-open an Already-closed object

Source: Internet
Author: User
Tags sqlite database

Finally, I solved the problem by using singleton mode and "do not close sqlitedatabase for a short period of time".
In the custom DBHelper class (most people are defined as Databasehelper):
public static synchronized DBHelper getinstance (context context) {if (dbinstance = = null) {dbinstance = new DBHelper ( Context.getapplicationcontext ());} return dbinstance;}

In the Custom dboperations class:
Constructor public Dboperations (context context) {DBHelper = new DBHelper (context). getinstance (context);} The open method of encapsulating SQLite public void open (context context) throws Sqlexception{db = Dbhelper.getinstance (context). Getreadabledatabase ();} Package SQLite Close Method 0public void Close (context context) {Dbhelper.getinstance (context). Close ();}


Android development, the correct management of your sqlitedatabase, if you have a good solution, you can leave a message, communicate together

The following are the reference solutions:
correctly managing your SQLite Database
One thing that I've noticed other Android developers have trouble with are properly setting up their sqlitedatabase. Often times, I come across questions on StackOverflow asking about error messages such as,
E/database (234): Leak found E/database (234): caused By:java.lang.IllegalStateException:SQLiteDatabase created and Never Closedas you had probably figured out, this exception was thrown when you had opened more sqlitedatabase instances than you have closed. Managing the database can complicated when first starting off with Android development, especially to those who is jus T beginning to understand the Activity lifecycle. The easiest solution is to make your database instance a singleton instance across the entire application ' s lifecycle. This would ensure that no leaks occur, and would make your life a IoT easier since it eliminates the possibility of Forgetti ng to close your database as you code.
Here is the examples that illustrates three possible approaches in managing your singleton database. These'll ensure safe access to the database throughout the application.
Approach #1: Use a Singleton to instantiate Thesqliteopenhelper
Declare your database helper as a static instance variable and use the Singleton pattern to guarantee the Singleton proper Ty. The sample code below should give you a good idea on how to go about designing the Databasehelper class correctly.
The static getinstance () method ensures that is only one databasehelper would ever exist at any given time. If the Sinstance object has not been initialized, one'll be created. If one has already been created then it'll simply be returned. You should not initialize your helper object using with new Databasehelper (context)! Instead, always use databasehelper.getinstance (context), as it guarantees the only one database helper would exist across The entire application ' s lifecycle.
public class Databasehelper extends Sqliteopenhelper {private static databasehelper sinstance; private static Final Strin G database_name = "database_name"; private static final String database_table = "table_name"; private static final int database_version = 1; public static synchronized Databasehelper getinstance (context context) {//Use the application Context, which would ensure That's//don ' t accidentally leak an Activity ' s context. See this article for more information:http://bit.ly/6lrzfx if (sinstance = = null) {sinstance = new Databasehelper (con Text.getapplicationcontext ()); } return sinstance; }/** * Constructor should is private to prevent direct instantiation. * Make call to static method "getinstance ()" instead. */Private Databasehelper (context context) {Super (context, database_name, NULL, database_version);}} Approach #2: Wrap the sqlitedatabase in a ContentProvider
This was also a nice approach. For one, the new Cursorloader class requirescontentproviders, so if you want a Activity or Fragment to Implementloaderman Ager. Loadercallbacks<cursor> with a cursorloader (as discussed inthis post), you'll need to implement a contentprovider F or your application. Further, you don ' t need to worry about making a singleton database helper withcontentproviders. Simply call Getcontentresolver () from the Activity and the system would take care of everything for you (in other words, th ere is no need for designing a Singleton pattern to prevent multiple instances from being created).
Leave a comment if this helped or if you had any questions!
Http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html


Java.lang.IllegalStateException:attempt to re-open an Already-closed object This error occurs because I called another database query method in a database query method, My database Query method is to get the Sqlitedatabase object at the beginning, close the Sqlitedabse object at the end, the results of the internal database query method at the end of the direct shutdown of the Sqlitedatabase object, Cause outside the database query operation error, here everyone do not think more get a few sqlitedatabase object on it, each thread can only use one sqliteopenhelper, It also makes each thread use a Sqlitedatabase object (the multithreaded operation database will error); The solution is that I no longer close the Sqlitedatbase object of the Internal database query method or integrate that method directly into the outside Query method, of course, To ensure that this query method will only appear in other database query methods, if you use this method alone, but because the Sqlitedatabase object is not closed and error;
http://blog.csdn.net/zhufuing/article/details/14455823
The following method uses the idea of async, but it doesn't solve my problem. "Translator" secure access to the database under Android multithreading
http://zhiweiofli.iteye.com/blog/2041977 Original: https://github.com/dmytrodanylyk/dmytrodanylyk/blob/gh-pages/ Articles/concurrent%20database%20access.md


Examples of reasons for common exceptions to Android SQLite

Cause:

If you have a, B two asynchronous thread operation SQLite database. A is read, B is written, and when a completes the read call Close (), and b at this point the method that is executing writes the following exception. Some people say that removing the singleton pattern can solve this problem, but you can't forget how you use the database or the same one in a single case, and you can't avoid it.

Workaround:

If you need to repeat the operation of the database for a certain amount of time, do not call the close () method and close the cursor. Call the Colse () method of the database when you are logged out of activity or when you really no longer need it.

http://blog.csdn.net/aaren_jiang/article/details/11781155


Java.lang.IllegalStateException:attempt to re-open an Already-closed object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.