RandomAccessFile.close() 内部调用 FileChannel.force() 吗?

发布于 2024-07-26 03:57:08 字数 298 浏览 6 评论 0原文

我正在使用 RandomAccessFile 对文件执行一些写入作为事务的一部分。 在提交事务之前,我想绝对确定数据已写入磁盘。 在 RAF 的 FileChannel 上调用 force(boolean) 似乎提供了这种保证,但是当我关闭文件时它是隐式调用的,还是必须手动调用它?

另外,是否有人了解 force() 的实际作用以及它的可信度? 操作系统是否可能报告数据已写入磁盘,而实际上数据仍然位于某个缓存中? 这在多大程度上依赖于操作系统/硬盘/文件系统?

I am using RandomAccessFile to perform some writes to a file as part of a transaction. Before I commit my transaction, I want to be absolutely sure that the data is written to disk.
Calling force(boolean) on the RAF's FileChannel appears to provide this guarantee, but is it called implicitly when I close the file, or do I have to call it manually?

Also, does anybody have any insight into what force() actually does, and how far it can be trusted? Is it possible that the OS may report that the data has been written to disk, when in fact it is still sitting in some cache somewhere? To what extent is this OS/HDD/filesystem-dependent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2024-08-02 03:57:08

这完全取决于 FS。 但如果您没有收到 (IO)Exception,您应该信任 JVM 并提交您的事务。 你是否可以信任你的FS是另一个问题。 例如,如果您使用激活了延迟分配的 ext4,即使 RandomAccessFile#force(boolean) 成功返回,您也可能会在瞬间断电时丢失数据。 JVM依赖于你的操作系统的FS-API,并不关心实际的实现和配置。

是的,您必须在关闭之前调用 RandomAccessFile#force(boolean) 。 #close 是否调用#force 取决于FileChannel 的实现。

This is completely FS-dependent. But if you do not get an (IO)Exception, you should trust the JVM and commit your transaction. Whether you can trust your FS is the other question. For example, if you use ext4 having delayed allocation activated, you might lose data on a instant power loss even after RandomAccessFile#force(boolean) returned successfully. The JVM relies on the FS-API of your operating system and does not care about the actual implementation and configuration.

And yes, you have to call RandomAccessFile#force(boolean) before closing. Whether #close invokes #force is up to the FileChannel's implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文