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
- Public class Motionanalysisdatabase extends sqliteopenhelper{
- <span style="color: #ff0000;" > Public motionanalysisdatabase (context context) {
- //database named Motionanalysisdatabase, version number 1
- Super (context, "Motionanalysisdatabase", null, 1);
- }
- Private volatile static motionanalysisdatabase uniqueinstance;
- public Static Motionanalysisdatabase getinstance (context context) {
- if (uniqueinstance = = null) {
- synchronized (motionanalysisdatabase. Class) {
- if (uniqueinstance = = null) {
- Uniqueinstance = New Motionanalysisdatabase (context);
- }
- }
- }
- return uniqueinstance;
- }
- </span>
- @Override
- public void OnCreate (Sqlitedatabase db) {
- String createacctable = "CREATE TABLE acc_table" + "(" + acctable.acc_id
- + "INTEGER primary key AutoIncrement," + acctable.date + "TEXT," +
- ACCTABLE.ACCX + "float," + acctable.accy + "float," +acctable.accz + "float," + Acctable.flag + "Integ ER); ";
- System.out.println ("create statement for Motion Acceleration data table" + createacctable);
- Db.execsql (createacctable);
- }
- @Override
- public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
- //TODO auto-generated method stub
- Db.execsql ("DROP TABLE IF EXISTS acc_table");
- }
- }
In the thread, if you refer to Motionanalysisdatabase, you can write this:
[Java]View Plaincopy
- Private Motionanalysisdatabase mdatabase;
- Private Sqlitedatabase DB;
[Java]View Plaincopy
- Mdatabase = Motionanalysisdatabase.getinstance (accdatacollectorservice. this);
- db = Mdatabase.getwritabledatabase ();
So again, there is no report of this mistake.
Database is locked