Database is locked

Source: Internet
Author: User

Original link: http://blog.csdn.net/zsg2063/article/details/21014721?utm_source=tuicool&utm_medium=referral

These days write threading operations, which involve multithreaded operations on the database. One read thread keeps reading the database data, and the other service constantly collects the sensor data and writes it. The above error occurred during mobile phone operation. There are a few references to this problem on the Internet, but the solution I have given is basically not understood.

Finally find senior, introduced a singleton model, probably knocked a few lines, on the solution.

The specific singleton model is still to be studied in depth, I will simply write down the meaning of my personal understanding.

Examples like sqlitedatabase (or sqlitedbhhelper) can only have one. That is, the link to the Operational database (Connection) can only have one, otherwise the above error will occur.

There are a lot of people who mention ContentProvider, and now the level of knowledge does not understand what this thing is for.

Whether it is a lightweight database of SQLite, or a large database such as SQL Server itself is "thread-safe", that is to say, like locking, unlock, prevent reading dirty data, the basic constraints of the database has been guaranteed by the SQLite database itself, Java or Android program Ape is not about whether it is unlocked, and, if your program is correct, regardless of the frequency of operation is high, can run, rather crash, there will be no "dirty data" situation.

Then there are two threads in my program, because of the security constraints of the SQLite database, which causes the thread to read and write the database conflict, one thread cannot read and write to the database that has been locked by the other threads.

The solution is to use singleton mode to strictly control the Sqlitedatabase instance, that is, to instantiate only one Sqlitedatabase object, no matter how many threads, only use this one sqlitedatabase (or Dbhelpler) to read and write threads. This way, sharing a database link does not result in multiple links modifying the data at the same time.

The above is purely personal understanding.

[Java]View Plaincopy
  1. Public class Motionanalysisdatabase extends sqliteopenhelper{
  2. <span style="color: #ff0000;" > Public motionanalysisdatabase (context context) {
  3. //database named Motionanalysisdatabase, version number 1
  4. Super (context, "Motionanalysisdatabase", null, 1);
  5. }
  6. Private volatile static motionanalysisdatabase uniqueinstance;
  7. public Static Motionanalysisdatabase getinstance (context context) {
  8. if (uniqueinstance = = null) {
  9. synchronized (motionanalysisdatabase. Class) {
  10. if (uniqueinstance = = null) {
  11. Uniqueinstance = New Motionanalysisdatabase (context);
  12. }
  13. }
  14. }
  15. return uniqueinstance;
  16. }
  17. </span>
  18. @Override
  19. public void OnCreate (Sqlitedatabase db) {
  20. String createacctable = "CREATE TABLE acc_table" + "(" + acctable.acc_id
  21. + "INTEGER primary key AutoIncrement," + acctable.date + "TEXT," +
  22. ACCTABLE.ACCX + "float," + acctable.accy + "float," +acctable.accz + "float," + Acctable.flag + "Integ ER); ";
  23. System.out.println ("create statement for Motion Acceleration data table" + createacctable);
  24. Db.execsql (createacctable);
  25. }
  26. @Override
  27. public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
  28. //TODO auto-generated method stub
  29. Db.execsql ("DROP TABLE IF EXISTS acc_table");
  30. }
  31. }

In the thread, if you refer to Motionanalysisdatabase, you can write this:
[Java]View Plaincopy
    1. Private Motionanalysisdatabase mdatabase;
    2. Private Sqlitedatabase DB;
[Java]View Plaincopy
    1. Mdatabase = Motionanalysisdatabase.getinstance (accdatacollectorservice.   this);
    2. db = Mdatabase.getwritabledatabase ();

So again, there is no report of this mistake.

Database is locked

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.