# Database is locked after encryption

**URL:** <https://discuss.zetetic.net/t/database-is-locked-after-encryption/3679>\
**Category:** SQLCipher\
**Created:** [May 16, 2019, 6:50pm UTC](https://discuss.zetetic.net/t/database-is-locked-after-encryption/3679 "2019-05-16T18:50:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtissington](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mtissington](https://discuss.zetetic.net/u/mtissington)\
**Post date:** [May 16, 2019, 6:50pm UTC](https://discuss.zetetic.net/t/database-is-locked-after-encryption/3679/1 "2019-05-16T18:50:39Z")

</div>

Database opened in WAL mode. I get an odd error Database is locked when trying to detach the database backup. The following code is executed on a different thread.

ATTACH DATABASE ‘filename’ AS backup KEY ‘key’;  
SELECT sqlcipher\_export(‘backup’);  
PRAGMA backup.user\_version = 23;

// error on this line “database is locked”  
DETACH DATABASE backup;

On the UI thread the main database maybe being queried with a large SELECT statement but from my understanding this should not cause a lock.

So why is the database locked after I have exproted an encrypted database?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [May 16, 2019, 9:04pm UTC](https://discuss.zetetic.net/t/database-is-locked-after-encryption/3679/2 "2019-05-16T21:04:06Z")

</div>

@mtissington are you using different/independent database handles on each thread, or a single database handle that is shared between two threads?

---

<div class="post-metadata">

**Author:** ![mtissington](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mtissington](https://discuss.zetetic.net/u/mtissington)\
**Post date:** [May 16, 2019, 9:40pm UTC](https://discuss.zetetic.net/t/database-is-locked-after-encryption/3679/3 "2019-05-16T21:40:23Z")

</div>

I’m using c# [System.SQLite.NET](http://System.SQLite.NET) to create connections which are shared between threads.

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [May 17, 2019, 1:28pm UTC](https://discuss.zetetic.net/t/database-is-locked-after-encryption/3679/4 "2019-05-17T13:28:33Z")

</div>

@mtissington Since you are sharing connections between threads the most likely explanation is that, when [serialized](https://www.sqlite.org/threadsafe.html) the activities in the UI thread are starting a transaction and the export thread is trying to detach while the transaction is running. It is not possible to attach or detach while in a transaction. If that is the case you could either use separate connections (i.e. one connection for your UI thread, a separate connection to he same database on your export thread), or you could establish a lock to prevent the connection from being used on the UI thread while the export is running.
