SQLite locking in multiple threads during iPhone Development

Source: Internet
Author: User

Data transferred from:

Http://blog.csdn.net/yifanernei/article/details/5642127

SQLite supports three thread modes:
1. Single-thread modeSqlite_config_singlethread
In this mode, mutual exclusion is not performed, and multithreading is insecure.
2. MultithreadingSqlite_config_multithread
In this mode, using a single database connection in multiple threads is not safe, otherwise it is safe.(Note: you cannot share database connections among multiple threads)
3. Serial ModeSqlite_config_serialized
In this mode, SQLite is thread-safe..Note: use the same database connection without mutual exclusion in multiple threads)

Select thread mode during running
If the single-thread mode is not specified at the time of compilation and startup, each database connection can be separately specified as a multi-thread mode or a serial mode at the time of creation, but cannot be specified as a single-thread mode. If it is set to single-thread mode during compilation or startup, you cannot specify multi-thread or serial mode during connection creation.

When creating a connection, use the third parameter of the sqlite3_open_v2 () function to specify the thread mode. Sqlite_open_nomutex identifies a connection to create a multi-threaded connection
Sqlite_open_fullmutex identifies the connection to create a serial mode. If no identifier is specified, or sqlite3_open () or
Sqlite3_open16 () function to create a database connection. The thread mode specified during compilation or startup will be used as the default thread mode.


In the iPhone, write at startupCode:

 
Sqlite3_config (sqlite_config_serialized); // This sentence does not have this effect //.....

Data consistency should be guaranteed.

 


// Sqlite3_config (sqlite_config_serialized );
// Sqlite3_exec (Database, "Pragma cache_size = 8000;", null );
// Sqlite3_exec (Database, "Pragma synchronous = off", null );
Sqlite3_busy_timeout (Database, 500); // The Code takes effect, and the effect of using a thread lock...

The current solution is to use the thread lock together with the code.

Currently, the database operation does not use a singleton. That is, the database will be connected to each read operation. The effect of using a singleton is the same ..

Continuous learning ing

Related Article

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.