英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论