2010-07-03 20 views
24

Ho fatto alcune operazioni ripetitive nella mia applicazione (test), e improvvisamente ricevo un errore di strano:OperationalError: database è bloccato

OperationalError: database is locked 

ho riavviato il server, ma l'errore persiste . Di cosa si tratta?

risposta

41

Da django doc:

SQLite is meant to be a lightweight database, and thus can't support a high level of concurrency. OperationalError: database is locked errors indicate that your application is experiencing more concurrency than sqlite can handle in default configuration. This error means that one thread or process has an exclusive lock on the database connection and another thread timed out waiting for the lock the be released.

Python's SQLite wrapper has a default timeout value that determines how long the second thread is allowed to wait on the lock before it times out and raises the OperationalError: database is locked error.

If you're getting this error, you can solve it by:

Switching to another database backend. At a certain point SQLite becomes too "lite" for real-world applications, and these sorts of concurrency errors indicate you've reached that point.

Rewriting your code to reduce concurrency and ensure that database transactions are short-lived.

Increase the default timeout value by setting the timeout database option optionoption

http://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errorsoption

+1

Specificare un più-che-default timeout può aiutare ad alleviare il problema: 'create_engine ('sqlite: /// {}' .format (xxx), connect_args = { 'timeout': 15}) ' –

+0

Nel mio caso ho aperto il file con' SQLiteDatabaseBrowser.exe' ... – citynorman

13

La ragione pratica per questo spesso è che i gusci di pitone o Django hanno aperto una richiesta al DB e non è stato chiuso correttamente; uccidere l'accesso al terminale spesso lo libera. Ho avuto questo errore nell'esecuzione di test da riga di comando oggi.

Modifica: ottengo upvotes periodici su questo. Se vuoi uccidere l'accesso senza riavviare il terminale, quindi da riga di comando che si può fare:

from django import db 
db.connections.close_all() 
+1

come ripararlo senza uccidere il terminale? Qualche idea? – neuronet

+0

@neuronet chiudi la tua connessione nella shell? –

5

Nel mio caso, è stato perché ho aperto il database dal Browser SQLite. Quando lo chiudo dal browser, il problema è scomparso.

+0

Grazie ha funzionato per me. – an0nh4x0r

-1

provare questo comando:

sudo fuser -k 8000/tcp 
+0

-1, Downvoted in quanto non offre alcuna spiegazione come ciò che questa soluzione fa e come, pur facendo ipotesi sulla porta che viene utilizzata – helplessKirk

+0

Ha aiutato comunque? –

Problemi correlati