Android Db锁定

时间:2012-03-05 17:33:13

标签: android

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

每分钟都会发生这种情况。这是有效的,但有时“数据库被锁定”。我该如何管理它而不是出错。感谢您的意见。

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();

    }
    }

2 个答案:

答案 0 :(得分:2)

我用这个完全相同的问题打了很长时间,随机使用我的应用程序开放数据库会得到一个“数据库被锁定”,没有任何时间或原因。经过几个月的挣扎,这是我的解决方案。

1)在课堂上创建dbopenhelper,其所有方法都是正常的。

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

3)打开Application类的onCreate中的DB,永远不要关闭它。

4)通过Application类拥有的dbhelper副本运行所有查询。

我担心永远不会关闭数据库,但我的研究显示Android开发人员(IE Googles人员)的许多实例都表明了这一点。

希望它对你有用,对我来说也是如此。

答案 1 :(得分:0)

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

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