【问题标题】:Android Db lockedAndroid Db 已锁定
【发布时间】:2012-03-06 01:51:10
【问题描述】:

使用 Sqlite db,我正在填充我的列表。该列表每分钟刷新一次(使用计时器)。我也在更新/插入 Sqlite db(使用服务),检查新数据。

这也每分钟都在发生。这是有效的,但有时“数据库被锁定”。我怎样才能管理这个而不是得到那个错误。感谢您的意见。

public class Services extends Service {

Context context;
int NOTIFICATION_ID = 1;
NotificationManager notificationManager, chatNotificationManager;
boolean connected = false;
boolean notconnected = false;
int numChat = 0;
int numTask = 0;
List<String> list =  new ArrayList();
String android_id, syncDecision;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override 
public void onCreate() {
    super.onCreate();
} 

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);

    context = getBaseContext();

    /*
     * THIS TIMER CONTAINS ANY SERVICE THAT ACCESSES THE SQLite database
     */
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            Log.d(TAG, "starting to do work in timer1");
            synchronizeSQLiteDatabases();
        }
    }, 0, 60000);


private void synchronizeSQLiteDatabases() {
    try {

        if (Functions.isOnline(context) )
        {
        // ---------------------------------- COMMENT UPLOAD ----------------------------
        if (!((GlobalMobileVianet) this.getApplication()).getUserid().equals("000000")) {
             try
             {
                Log.i(TAG, "uploading comment data");
                DataUploader_Comment commentupdater = new DataUploader_Comment(((GlobalMobileVianet) this.getApplication()).getUserid(), 
                        ((GlobalMobileVianet) this.getApplication()).getPassword(), context);
                commentupdater.run();
             }
             catch(Exception e)
             {
                    Log.i(TAG, "    CATCH uploading comment data" + e.toString());

             }
        }
        else
        {
            //Functions.logout(getApplicationContext());
            Log.i(TAG, "    NOT uploading comment data");
        }
        //--------------------------------------- TASK UPLOAD --------------------------------
        if ( !((GlobalMobileVianet) this.getApplication()).getUserid().equals("000000")) {

                try{
                    Log.i(TAG, "uploading task data");
                    DataUploader_Task taskuploader = new DataUploader_Task(((GlobalMobileVianet) this.getApplication()).getUserid(), 
                        ((GlobalMobileVianet) this.getApplication()).getPassword(), context);
                    taskuploader.run();
                }
                catch(Exception e)
                {
                    Log.i(TAG, "    CATCH uploading task data" + e);

                }
        }
        else
        {
            //Functions.logout(getApplicationContext());
            Log.i(TAG, "    NOT uploading task data");
        }

        // ------------------------------------------AC DOWNLOAD ---------------------------------
        if (!((GlobalMobileVianet) this.getApplication()).getUserid().equals("000000")) {


                try{
                    Log.i(TAG, "running aircraft data download");
                    DataLoader_Aircraft acloader = new DataLoader_Aircraft(((GlobalMobileVianet) this.getApplication()).getUserid(), 
                        ((GlobalMobileVianet) this.getApplication()).getPassword(), context, "N");
                    acloader.run();
                }
                catch(Exception e)
                {
                    Log.i(TAG, "    CATCH running aircraft data download" + e.toString());

                }
        }
        else
        {
            //Functions.logout(getApplicationContext());
            Log.i(TAG, "    NOT running aircraft data download");
        }

        // ------------------------------------ TASK DOWNLOAD ------------------------------------
        if (  !((GlobalMobileVianet) this.getApplication()).getUserid().equals("000000")) {

            try{
                Log.i(TAG, "running task data download");
                DataLoader_Task tasksloader = new DataLoader_Task(((GlobalMobileVianet) this.getApplication()).getUserid(), 
                        ((GlobalMobileVianet) this.getApplication()).getPassword(), context, "N");
                tasksloader.run();
            }
            catch(Exception e)
            {
                Log.i(TAG, " CATCH running task data download" + e.toString());

            }
        }
        else
        {
            //Functions.logout(getApplicationContext());
            Log.i(TAG, "    NOT running task data download");
        }

        // -------------------------------- EMPLOYEE DOWNLOAD -----------------------------------
        if (!((GlobalMobileVianet) this.getApplication()).getUserid().equals("000000")) {

            try
            {
                Log.i(TAG, "running employee data download");
                DataLoader_Employee employeeloader = new DataLoader_Employee(((GlobalMobileVianet) this.getApplication()).getUserid(), 
                        ((GlobalMobileVianet) this.getApplication()).getPassword(), context, "N");
                employeeloader.run();
            }
            catch(Exception e)
            {
                Log.i(TAG, "    CATCH running employee data download " + e.toString());

            }
        }
        else
        {
            //Functions.logout(getApplicationContext());
            Log.i(TAG, "    NOT running employee data download");
        }

        // ------------------------------------COMMENT DOWNLOAD -----------------------------------------
        if (!((GlobalMobileVianet) this.getApplication()).getUserid().equals("000000")) {

            try{
                Log.i(TAG, "running comment data update");
                DataLoader_Comment commentloader = new DataLoader_Comment(((GlobalMobileVianet) this.getApplication()).getUserid(), 
                        ((GlobalMobileVianet) this.getApplication()).getPassword(), context, "N");
                commentloader.run();
            }
            catch(Exception e)
            {
                Log.i(TAG, "    CATCH running comment data update" + e.toString());

            }
        }
        else
        {
            //Functions.logout(getApplicationContext());
            Log.i(TAG, "    NOT running comment data download");
        }

        } 
    } catch (Exception e) {
        Log.e(TAG,
                "synchronizeSQLiteDatabases exception: " + e.toString());
    }

}

这是刷新列表的活动

public class TestExList extends ExpandableListActivity{

 String empid, ctext, fleet_name, rString;
 private DbAdapter_Assignment assignment;
 private DbAdapter_Task task;
 private DbAdapter_Comment comment;
 private DbAdapter_Aircraft aircraft;
 private Cursor accursor, taskcursor;
 private ExpandableListView expListView;
 private Timer taskTimer;
 private ListView lv;
 private Camera cam;
 private boolean onoff;

 private boolean testValue;

 private SimpleCursorTreeAdapter cursorTreeAdapter;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        empid = getIntent().getExtras().getString("EmpID");
        Log.d("testValue","from onCreate, onoff is " + onoff);

        setHeader();

        if(empid.equals(Functions.getLoginEmpID(this))){
            getData(empid);
        }
        taskTimer = new Timer();
        taskTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
            }

        }, 0, 60000);
        expListView.invalidateViews();
    } 


    private void TimerMethod()
    {
        this.runOnUiThread(Timer_Tick);
    }

    private Runnable Timer_Tick = new Runnable() {
        public void run() {
                if(Functions.getServiceStatus(getBaseContext()) != "No service")
                {
                    if(empid.equals(Functions.getLoginEmpID(getBaseContext())))
                    {
                        getData(empid);
                    }
                }
        }
    };
    @Override 
    public void onDestroy()
    {
        super.onDestroy();
        try
        {
            accursor.close();
            taskcursor.close();
            task.close();
            comment.close();
            aircraft.close();
            assignment.close();
        }
        catch(Exception e)
        {
            Log.e("ViaNetError", "Unable to close cursors.");
        }

        if(cam != null)
        {
            cam.release();
            cam = null;
        }
    }


    public void getData(final String empid)
    {
         Log.e("Testing", "Getting data for emp: " + empid);
        assignment =  new DbAdapter_Assignment(getBaseContext());
         assignment.open();
         task =  new DbAdapter_Task (getBaseContext());
         task.open();
         comment = new DbAdapter_Comment (getBaseContext());
         comment.open();

    }
    }

【问题讨论】:

  • 可能想看看这个:stackoverflow.com/questions/4302286/…
  • 您是否使用了多个 DbHelper?
  • 我不太明白你在做什么,但是 getData 重新分配 assignmenttaskcomment 而不关闭以前的值。如果你为之前打开的每个变量写 if(assignment != null) { assignment.close() } [...] 会发生什么?
  • 如果我每次都关闭它。它错误“试图阅读已关闭”
  • 我会试一试。谢谢。

标签: android


【解决方案1】:

我多年来一直在与这个完全相同的问题作斗争,随机使用我的应用程序打开数据库会得到一个“数据库被锁定”,没有任何理由或理由。经过几个月的努力,这是我的解决方案。

1) 在类中创建您的 dbopenhelper,其所有方法都与普通方法一样。

2) 在您的 APPLICATION 类中创建该类的实例。

3) 在 Application 类的 onCreate 中打开数据库,永远不要关闭它。

4) 通过应用程序类拥有的 dbhelper 副本运行所有查询。

我担心永远不会关闭数据库,但我的研究表明,许多 Android 开发人员(即谷歌的人)的例子就是这样。

希望它对你和我一样有效。

【讨论】:

  • 我尝试了这种方法但在我的情况下不起作用请看看我的问题link is here
【解决方案2】:

SQLite 告诉你数据库已经在另一个线程中打开。您必须以线程安全的方式使用 SQLite。

您能否在打开数据库和计时器的问题中添加代码?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2018-10-05
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多