SQLite数据库备份ASP.NET Core文件被锁定。

huangapple go评论50阅读模式
英文:

SQLite database backup ASP.NET Core file locked

问题

我有一个使用SQLite的ASP.NET Core 6应用程序。我有一个定期运行的Hangfire作业,我想使用SQLite的在线备份API来备份数据库。最后,我想将备份数据库文件上传到Azure Blob存储。代码如下:

public void CreateBackup()
{
    string backupPath = string.Empty;
    using (var originalConnection = new SqliteConnection(this.configuration.GetConnectionString("Database")))
    using (var backupConnection = new SqliteConnection(this.configuration.GetConnectionString("BackupDatabase")))
    {
        originalConnection.Open();
        originalConnection.BackupDatabase(backupConnection);
        backupPath = backupConnection.DataSource;
    }

    BlobClient client = new BlobClient(this.configuration.GetConnectionString("AzureBackup"), "tanusitvanytar", $"database-{DateTime.Now.ToString("yyyy-MM-dd")}");
    client.Upload(backupPath);
}

但是当执行上传时,我收到一个异常:

System.IO.IOException: '进程无法访问文件。

使用块应该关闭对备份数据库的连接,所以还在使用它的是什么?我应该如何解决这个问题?

英文:

I have an ASP.NET Core 6 application that uses SQLite. I have a recurring Hangfire job that I'd like to use to back up the database using the online backup API of SQLite. Finally, I'd like to upload the backup database file to Azure blob storage. Code:

public void CreateBackup()
{
    string backupPath = string.Empty;
    using (var originalConnection = new SqliteConnection(this.configuration.GetConnectionString("Database")))
    using (var backupConnection = new SqliteConnection(this.configuration.GetConnectionString("BackupDatabase")))
    {
        originalConnection.Open();
        originalConnection.BackupDatabase(backupConnection);
        backupPath = backupConnection.DataSource;
    }

    BlobClient client = new BlobClient(this.configuration.GetConnectionString("AzureBackup"), "tanusitvanytar", $"database-{DateTime.Now.ToString("yyyy-MM-dd")}");
    client.Upload(backupPath);
}

But when the upload is executed I get an exception:

> System.IO.IOException: 'The process cannot access the file.

The using block is supposed to close the connection to the backup database, so what is still using it? How can I fix this?

答案1

得分: 0

要么通过在连接字符串中添加 Pooling=false 来禁用池化,

要么在希望关闭连接的地方调用 SqliteConnection.ClearAllPools();

英文:

Either disable pooling by adding 

Pooling=false to the connection string

or call

SqliteConnection.ClearAllPools();

at the point where you want the connection to be closed.

huangapple
  • 本文由 发表于 2023年7月4日 21:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定