Using TransactionScope with SQLite causes database locked exception

*爱你&永不变心* 提交于 2019-12-23 13:33:27

问题


I am trying to adapt a c# code that uses TransactionScope and works with Oracle to work also with SQLite. My code is structured in a way that every method called within the transaction scope that accesses the SQLite database creates its own SQLiteConnection object.

Now I have run into a problem when trying to open a second connection within the TransactionScope block. I am taking care to properly dispose of all the unused SQLiteConnection objects by either wrapping them in a using statement or calling Dispose in a finally block. No matter what I do the second call to SQLiteConnection.Open times out after a while with database is locked exception.

Example code:

using(var transaction = new TransactionScope(TransactionScopeOption.Required))
{
    using(var connection1 = new SQLiteConnection(_connectionString))
    {
        connection1.Open();
        ... // Do stuff with the open connection

    } // Closes the connection 

    using(var connection2 = new SQLiteConnection(_connectionString))
    {
        // database locked exception
        connection2.Open();          
        ... 
    } 
}

All I have found out about the issue so far is in this article:http://elegantcode.com/2010/07/02/using-transactionscope-with-sqlite/ which says:

The TransactionScope holds an exclusive writer lock, even when disposing the first connection. Because the first connection cannot fully close itself, opening the second connection results in a lock error.

Unfortunately the article doesn't offer any workaround. I'd very much like to stick to using TransactionScope since having to pass SQLiteConnection and SQLiteTransaction objects to every method in my code would be a lot of rewrite effort not to mention the code would look much more messy. Any advice?

来源:https://stackoverflow.com/questions/32182033/using-transactionscope-with-sqlite-causes-database-locked-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!