2013-03-07 4 views
0

위젯 및 서비스와 같이 differens 스레드 및 프로세스에서 sqlite 데이터베이스를 사용하는 Android 애플리케이션을 개발 중입니다. 연결을 부여 싱글 톤 패턴을 사용데이터베이스에 연결할 수 없습니다.

, 나는 무작위이 오류를 얻을 : 앱이 getWriteableDatabase() 방법에 붙어 남아 있고,이 경고가 로그

W/SQLiteConnectionPool(2021): The connection pool for database '/data/data/xyz' has been unable to grant a connection to thread xyz (xyz) with flags 0x1 for xyz seconds. 
W/SQLiteConnectionPool(2021): Connections: 0 active, 1 idle, 0 available. 

이 오류는 몇 시간 후 일반적으로 발생에 나타나는 응용 프로그램이 닫히고 다시 열어도이 버그를 재현 할 수있는 공식적인 방법은 없습니다.

은 둘 다이 접근 사용하려고하지만 운과 :

class DB extends SQLiteOpenHelper { 

    DB databaseHelper; 
    SQLiteDatabase database; 

    /** 
    * Open a Database Helper. 
    * 
    * @param c the Context used to open the Database. 
    * @return the Database Helper. 
    */ 
    public static DB getInstance(final Context c) { 
     if (databaseHelper == null) { 
      databaseHelper = new DB(c.getApplicationContext()); 
     } 
     return databaseHelper; 
    } 

    int active_connections; 

    /** 
    * @return the Database connection to use 
    * @see closeReadableDataBase() 
    */ 
    public synchronized SQLiteDatabase getDatabase() { 
     if (database == null || !database.isOpen()) { 
      if(database!=null) database.close(); 
      database = getWritableDatabase(); 
      active_connections=0; 
      database.setLockingEnabled(false); 
      TransitionHelper.execSQL(database, "PRAGMA read_uncommitted = true;"); 
      TransitionHelper.execSQL(database, "PRAGMA synchronous=OFF;"); 
     } 
     active_connections++; 
     return database; 
    } 

    /** 
    * Closes the database connection. 
    * @see openDatabaseForRead() 
    */ 
    public synchronized void closeDatabase() { 
     active_connections--; 
     if(active_connections==0 && database!=null){ 
      database.close(); 
      database=null; 
     } 
    } 
} 

그리고 : 당신이 다른 프로세스에서 SQL 데이터베이스를 사용하여 자신을 발견 할 경우

class DB extends SQLiteOpenHelper { 

    DB databaseHelper; 
    SQLiteDatabase database; 

    public static DB getInstance(final Context c) { 
     if (databaseHelper == null) { 
      databaseHelper = new DB(c.getApplicationContext()); 
     } 
     return databaseHelper; 
    } 

    public synchronized SQLiteDatabase getDatabase() { 
     if (database == null || !database.isOpen()) { 
      if(database!=null) database.close(); 
      database = getWritableDatabase(); 
     } 
     return database; 
    } 

    public synchronized void closeDatabase() { 
     if(database!=null){ 
      database.close(); 
     } 
    } 
} 
+0

난 당신이 링크가 도움이 될 dB의 싱글 디자인을 필요가 있다고 생각이 http://stackoverflow.com/questions/6905524/using-singleton-design-pattern-for- sqlitedatabase – DjHacktorReborn

+0

[콘텐츠 공급자] (http://developer.android.com/guide/topics/providers/content-providers.html) 사용을 고려하십시오. ... –

+0

@DjHacktorReborn 두 번째 코드 블록에서 볼 수 있듯이 싱글 톤 패턴을 사용합니다. – Spotlight

답변

1

, 당신은 정말 캡슐화 고려해야한다 콘텐츠 제공 업체에서 로더와 함께 콘텐츠 공급자는 프로세스, DataObservers 등의 액세스를 용이하게합니다. 을 만들기위한 API 가이드도있다 : Creating a Content Provider

+0

문서별로, 모든 액티비티가 단일 애플리케이션의 일부인 경우 컨텐트 제공자가 필요하지 않습니다. (서비스 포함). – strange

관련 문제