java Java中文件操作的事务模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1311050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:04:47  来源:igfitidea点击:

Transaction mode for file operations in Java

javatransactions

提问by Giroscopio

Perhaps what I'm trying to explain here doesn't make any sense, so I'd like to apologize in advance. Anyway, I will try.

可能我在这里解释的内容没有任何意义,所以我想提前道歉。不管怎样,我会努力的。

I'm trying to read from a file, perform some database operations and move the content to another file. I was wondering if it is possible to perform all this operations in an atomic way in Java, so if anything goes wrong in the list of actions, rollback the complete sequence and go back to the start point.

我正在尝试从文件中读取数据,执行一些数据库操作并将内容移动到另一个文件。我想知道是否可以在 Java 中以原子方式执行所有这些操作,因此如果操作列表中出现任何问题,请回滚完整序列并返回起点。

Thanks in advance for your help.

在此先感谢您的帮助。

采纳答案by Brian Agnew

Take a look at Apache Commons Transaction. It has the capability to manage files transactionally.

看看Apache Commons Transaction。它具有事务性管理文件的能力。

An archived articledetailed its use with the file system.

一篇存档文章详细介绍了它在文件系统中的使用。

update

更新

Be aware that the status on the front page says:

请注意,首页上的状态显示:

We have decided to move the project to dormant as we are convinced that the main advertised feature transactional file access can not be implemented reliably. We are convinced that no such implementation can be possible on top of an ordinary file system. Although there are other useful parts (as multi level locking including deadlock detection) the transactional file system is the main reason people use this library for. As it simply can not be made fully transactional, it does not work as advertised.

我们决定将项目转为休眠状态,因为我们确信主要宣传的功能事务文件访问无法可靠地实现。我们确信在普通文件系统之上不可能实现这样的实现。尽管还有其他有用的部分(如多级锁定,包括死锁检测),但事务文件系统是人们使用该库的主要原因。由于它根本无法完全交易,因此它不像宣传的那样工作。

回答by pjp

There is no standard Transaction File API however I beleive that there is an Apache project that implements what you want.

没有标准的事务文件 API,但是我相信有一个 Apache 项目可以实现您想要的。

http://commons.apache.org/transaction/file/index.html

http://commons.apache.org/transaction/file/index.html

The transactional file package provides you with code that allows you to have atomic read and write operations on any file system. The file resource manager offers you the possibility to isolate a number of operations on a set of files in a transaction. Using the locks package it is able to offer you full ACID transactions including serializability. Of course to make this work all access to the managed files must be done by this manager. Direct access to the file system can not be monitored by the manager.

事务文件包为您提供了允许您在任何文件系统上进行原子读写操作的代码。文件资源管理器为您提供了在事务中对一组文件隔离多个操作的可能性。使用 locks 包,它能够为您提供完整的 ACID 事务,包括可串行化。当然,要完成这项工作,所有对托管文件的访问都必须由该管理器完成。管理器无法监控对文件系统的直接访问。

update

更新

Be aware that the status on the front page says:

请注意,首页上的状态显示:

We have decided to move the project to dormant as we are convinced that the main advertised feature transactional file access can not be implemented reliably. We are convinced that no such implementation can be possible on top of an ordinary file system. Although there are other useful parts (as multi level locking including deadlock detection) the transactional file system is the main reason people use this library for. As it simply can not be made fully transactional, it does not work as advertised.

我们决定将项目转为休眠状态,因为我们确信主要宣传的功能事务文件访问无法可靠地实现。我们确信在普通文件系统之上不可能实现这样的实现。尽管还有其他有用的部分(如多级锁定,包括死锁检测),但事务文件系统是人们使用该库的主要原因。由于它根本无法完全交易,因此它不像宣传的那样工作。

回答by nitin

As XADisksupports XA transactions over file-systems, it should solve you problem. It can participate in XA transactions along with Databases and other XA Resources.

由于XADisk支持文件系统上的 XA 事务,它应该可以解决您的问题。它可以与数据库和其他 XA 资源一起参与 XA 事务。

In case your application is not in a JCA supportive environment, you can also use standalone Transaction Manager like Atomikos and carry out XA transactions involving both files (using XADisk) and Database.

如果您的应用程序不在支持 JCA 的环境中,您还可以使用独立的事务管理器(如 Atomikos)并执行涉及文件(使用 XADisk)和数据库的 XA 事务。

update

更新

The project's home page does not exist anymore and the last release on Maven was in 2013.

该项目的主页不再存在,Maven 的最后一个版本是在 2013 年。

回答by sleske

No, at least not with a simple call. Filesystems in general (and Java filesystem operations in particular) do not support a "rollback".

不,至少不是通过简单的调用。一般的文件系统(尤其是 Java 文件系统操作)不支持“回滚”。

You could however emulate this. A common way would be to first rename the file so that it is marked as being "in processing". Append some suffix for example.

但是,您可以效仿这一点。一种常见的方法是首先重命名文件,以便将其标记为“正在处理”。例如,附加一些后缀。

Then process it, then change the file. If anything goes wrong, just rollback the DB, rename all the file(s) with suffixes back to their original names and you're set.

然后处理它,然后更改文件。如果出现任何问题,只需回滚数据库,将所有带有后缀的文件重命名回它们的原始名称,然后就可以了。

As a bonus, on some FS a rename is even atomic, so you'd be safe even with concurrent updates (don't know if this is relevant for you). I do not know whether file renaming is atomic in Java though; you'd need to check.

作为奖励,在某些 FS 上,重命名甚至是原子性的,因此即使进行并发更新,您也是安全的(不知道这是否与您相关)。我不知道文件重命名在 Java 中是否是原子的;你需要检查一下。

回答by Adamski

You can coordinate a distributed transaction using Two-Phase Commit. However, this is fairly complex to implement and an approach I've often seen taken is to use single-phase commit, building a stack of transactions and then committing them all in quick succession, generating an error if one of the commit attempts fails but others succeed.

您可以使用两阶段提交来协调分布式事务。然而,这实现起来相当复杂,我经常看到的一种方法是使用单阶段提交,构建一堆事务,然后快速连续提交它们,如果其中一个提交尝试失败,则会产生错误,但是别人成功了。

If you chose to implement Two-Phase Commit you'd require Write-Ahead Loggingfor each participant in the transaction, where you log actions before you've taken them, allowing you to roll back any changes if the transaction fails. For example, you'd need to do this in order to potentially reverse any changes made to files (as sleske mentions).

如果您选择实施两阶段提交,则需要为事务中的每个参与者进行预写日志记录,您可以在其中记录操作之前的操作,以便在事务失败时回滚任何更改。例如,您需要这样做以潜在地撤销对文件所做的任何更改(如 sleske 所述)。

回答by Yves Martin

JBossTS proposes its own implementation for transactional file i/o, as part of the Narayanaproject (formerly called JBossTS).

JBossTS 提出了自己的事务文件 i/o 实现,作为Narayana项目(以前称为 JBossTS)的一部分。