如何在 Java EE 环境中管理数据库和文件系统的事务? [英] How to manage transaction for database and file system in Java EE environment?

查看:22
本文介绍了如何在 Java EE 环境中管理数据库和文件系统的事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文件的属性(大小、更新时间...)存储在数据库中.所以问题是如何管理数据库和文件的事务.

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:

  • Have a look at: JCA connector: a file system adapter. It's fairly basic, but manages commit/rollback of files.

关于其他项目:

  • 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 接口,你得到 beforeCompletionafterCompletion 回调.后者指示事务是提交还是回滚,如有必要,您可以进行清理.然后,您可以实施交易行为.

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屋!

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