N
N
nnikolyaa2020-08-27 18:45:43
Python
nnikolyaa, 2020-08-27 18:45:43

How to close all valid connections in sqlite3?

How it is possible to close ALL connections. Not a single, but all connections.
Found with django, but it doesn't work.

from django import db
db.connections.close_all()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2020-08-28
@nnikolyaa

The above code with django works because django has information about all connections opened by django and can close them.
With the direct opening of bases, of course, information about them does not add up anywhere. To get this on your own, you can put the connection in the list every time you open the sqlite database, and at the right time iterate over it and close everything:

db_connections = []

db1 = sqlite3.connect('baza1.db')
db_connections.append(db1)
db2 = sqlite3.connect('area51.db')
db_connections.append(db2)

...

for db in db_connections:
  db.close()

PS: Some time ago I wrote a script that generates batches of sqlite files (cache for CAC.Planet), and there is a special class that opens new files and closes the extra ones when a certain limit is reached: https://github.com/shurshur /tms2sqlite/blob/master...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question