如何在Java EE环境中管理数据库和文件系统的事务? [英] How to manage transaction for database and file system in Java EE environment?
问题描述
我将文件的属性(大小,更新时间......)存储在数据库中。所以问题是如何管理数据库和文件的事务。
I store file’s attributes (size, update time…) in database. So the problem is how to manage transaction for database and file.
在Java EE环境中,JTA只能管理数据库事务。
如果更新数据库成功但文件操作失败,我应该为此编写文件回滚方法吗?此外,EJB容器中的文件操作违反了EJB规范。
In a Java EE environment, JTA is just able to manage database transaction. In case, updating database is successful but file operation fails, should I write file-rollback method for this? Moreover, file operation in EJB container violates EJB spec.
您的意见是什么?
推荐答案
理想情况下,访问外部资源(如文件系统)应通过 JCA连接器。虽然有几个帖子围绕讨论这个,我从来没有找到一个现成的JCA连接器用于事务访问文件系统,所以我开始写一个:
Access to external resources such as a file system should ideally go through a JCA connector. Though there are several posts around discussing this, I never found a ready-to-use JCA connector for transactional access to the file system, so I started to write one:
- 查看: JCA连接器:文件系统适配器 。这是相当基本的,但管理文件的提交/回滚。
- Have a look at: JCA connector: a file system adapter. It's fairly basic, but manages commit/rollback of files.
关于其他项目:
- 我不知道 commons-transactions ,对我来说似乎已经死了。
- 查看 JBoss Transactional File I / O 看起来很有希望。
- 还要看看 Filera:文件资源适配器,但我不认为它是事务性的
- I don't know the exact status of commons-transactions, it seems dead to me.
- Have a look at JBoss Transactional File I/O, looks promising.
- Have also a look at Filera: File resource adapter, but I don't think it's transactional
请注意,尽快你有多个交易参与者,应用程序。服务器确实需要使用分布式事务,事情变得更加复杂。你不能低估这种复杂性(例如数据库有分布式事务的另一种超时机制)。
Note that as soon as you have more than one transactional participant, the app. server really need to use distributed transaction and things get more complicated. You must not underestimate this complexity (e.g. database have another timeout mechanism for distributed transaction).
另一种考虑的轻量级方法是使用 SFSB 写在文件系统上。如果您实施 SessionSynchronization
界面,你得到 beforeCompletion
和 afterCompletion
回调。后者指示事务是已提交还是已回滚,并在必要时进行清理。然后,您可以实现事务性行为。
Another lightweight approach to consider is to use a SFSB that writes on the file system. If you implement SessionSynchronization
interface, you get beforeCompletion
and afterCompletion
callbacks. The later indicates whether the transaction was committed or rolled back and you do cleanup if necessary. You can then implement a transactional behavior.
这篇关于如何在Java EE环境中管理数据库和文件系统的事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!