I am a newbie for SQLite and I have read that the Serialized mode of SQLite is thread-safe, and can be safely used by multiple threads with no restriction.
My question is: In a single-threaded app, is there any performance impact if I use one same global connection for all database operation comparing to using one connection per database operation?(Ignore the performance impact for building db connection)
To be specific, imagine such a scenario: in the same thread, I need build two prepared statement to query two table respectively in the same dababase, and I need use STEP() to retrieve data from the two statement alternatively. My question is which will have better performance: I. Using one same connection for both statement; II. one connection per statement?(not account for the performance impact occurred in the process of building connection) Or is it necessary to use a connection pool for performance benefit?
You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.
Connection PoolingThe underlying, native SQLite connections are now pooled by default. This greatly improves the performance of opening and closing connections.
Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore.
A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default).
In a single-threaded app, you can use either a single or multiple connections. In the latter case, it is possible to run multiple queries in parallel.
Please note that there is one transaction per connection. If you have multiple connections, one writer will block all readers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With