36

What is the maximum number of connections for a SQLite3 database?

Why can't I use it for very big websites, for example with 3 million users?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Michael harris
  • 966
  • 2
  • 14
  • 25
  • 11
    sqlite doesn't allow concurrent write access (i.e. only one write transaction can be on going at a time), which is why it shouldn't be used for websites with write heavy workloads. – Dan D. Jan 26 '12 at 11:58

3 Answers3

42

http://sqlite.org/whentouse.html explains "Situations Where Another RDBMS May Work Better":

SQLite uses reader/writer locks on the entire database file. That means if any process is reading from any part of the database, all other processes are prevented from writing any other part of the database. Similarly, if any one process is writing to the database, all other processes are prevented from reading any other part of the database. For many situations, this is not a problem. Each application does its database work quickly and moves on, and no lock lasts for more than a few dozen milliseconds. But there are some applications that require more concurrency, and those applications may need to seek a different solution.

6

There is actually no pre-defined limit for number of concurrent connections in sqlite for the same process. This is upto your system's performance. The quotation given by user647772 is about limit of concurrent processes or applications using the same sqlite DB, not valid for concurrent threads in the same process.

ayhan
  • 129
  • 1
  • 4
5

Under different system, this value may be different, the python test code:

import sqlite3
import sys

# connect to multiple databases
def multi_connect(conn_num):
    dbs = []
    for i in range(0, conn_num):
        try:
            con = sqlite3.connect(str(i) + '.db')
        except Exception as e:
            print('connect to %d.db failed' % i)
            sys.exit(-1)


# multiple connections to single database
def multi_connect2(conn_num):
    db_name = 'x.db'
    conns = []
    for i in range(0, conn_num):
        try:
            conn = sqlite3.connect(db_name)
        except Exception as e:
            print('connect failed at %d' % i)
            sys.exit(-1)

Under ubuntu, the failed count is 1021, you can test it under different OS.

coanor
  • 3,746
  • 4
  • 50
  • 67
  • what were the results of your test. 1021 connections to multiple databases or to a single database? – Bruno Bieri Aug 18 '17 at 08:11
  • 21
    This isn't an sqlite3 restriction. What you've run up against here is the default limit on the number of file handles you can have open at once in a single linux process. The default limit is 1,024. What you're seeing is 1,021 handles holding open the database and one file handle each for stdin, stdout and stderr. This is only a default value, however, and can be readily changed. – Michael Speer Mar 26 '18 at 20:34