如何在文件系统和数据库之间实现协调? [英] How to bring coordination between file system and database?

查看:137
本文介绍了如何在文件系统和数据库之间实现协调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在线文件管理项目。我们在数据库(sql server)上存储引用,并在on文件系统上存储文件数据。

I am working on a online file management project. We are storing references on the database (sql server) and files data on the on file system.

在我们上传文件时以及在删除文件时,我们面临着文件系统和数据库之间协调的问题。首先,我们在数据库中创建一个引用或者在文件系统上存储文件。

We are facing a problem of coordination between file system and database while we are uploading a file and also in case of deleting a file. First we create a reference in the database or store files on file system.

问题是,如果我先在数据库中创建一个引用,然后在文件系统上存储一个文件,但在文件系统上存储文件时发生任何类型的错误,则该文件的引用在数据库中创建,但文件系统上不存在文件数据。

The problem is that if I create a reference in the database first and then store a file on file system, but while storing files on the file system any type of error occur, then the reference for that file is created in the database but no file data exist on the file system.

请给我一些解决方案如何处理这种情况。

Please give me some solution how to deal with such situation. I am badly in need of it.

这种情况也发生在我们删除文件的时候?

This case happens also while we deleting a file?

推荐答案

访问文件系统确实不是事务性的。您将需要自己模拟全或无分布式事务:如果数据库中的提交失败,请删除文件系统上的文件。相反,如果写入文件失败,回滚数据库事务(这将是一个更复杂,但这是一个粗略的草图)。

Access to the file system is indeed not transactional. You will need to simulate an all-or-nothing distributed transaction yourself: if the commit in database fails, delete the file on file-system. Inversely, if writing file fails, rollback database transaction (That will be a bit more complicated, but that's a rough sketch).

请注意,当文件更新时,它可能会变得很复杂。您需要首先复制它,以便如果数据库事务在覆盖文件后失败,您仍然可以还原旧版本的文件。

Note that it can get pretty complicated when a file is updated. You need first to copy it, so that if the database transaction fails after you've overwritten the file you can still restore the old version of the file. Whether you want to do this depends on the level of robustness that is desired.

尝试强制执行所有操作通过您的应用程序(创建,写入,删除文件)。如果你不能这样做,你不能阻止一个文件被直接访问文件系统(也许删除),我看到没有其他方法,定期同步数据库与文件系统:检查哪个文件被删除和删除数据库中的条目。

Try to enforce that all manipulations go through your application (create, write, delete of files). If you can not do this and you can not prevent a file from being accessed directly on the file system (and maybe deleted), I see no other way than to periodically synchronize the database with the file system: check which file was removed and delete the entry in database. You could create a job that runs each X minute for that.

我也建议储存哈希例如MD5)。花一点时间来计算它,但这对我来说非常有用的检测问题,例如。如果文件在文件系统上通过错误重命名,但不在数据库中重命名。这也允许定期运行一些完整性检查,以验证没有被拧。

I would also suggest storing a hash (e.g. MD5) of the file in database. Take a bit of time to compute it, but that has been immensely useful for me to detect problems, e.g. if the file is renamed on file system by error but not in database. That also allows to run some integrity check periodically, to verify nothing was screwed.

如果这种方法不够(例如,你希望它更健壮),我看到没有其他办法把二进制存储在LOB的数据库。那么它将是真正的事务和安全。

If this approach is not sufficient (e.g. you want it to be more robust), I see no other way than to store the binary in the database in LOB. Then it will be really transactional and safe.

这篇关于如何在文件系统和数据库之间实现协调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆