简体   繁体   English

如何在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. 所以问题是如何管理数据库和文件的事务。

In a Java EE environment, JTA is just able to manage database transaction. 在Java EE环境中,JTA只能管理数据库事务。 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. 此外,EJB容器中的文件操作违反了EJB规范。

What's your opinion? 你怎么看?

Access to external resources such as a file system should ideally go through a JCA connector . 理想情况下,访问外部资源(如文件系统)应通过JCA连接器 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连接器用于事务访问文件系统,所以我开始写一个:

Regarding other projects: 关于其他项目:

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 (eg database have another timeout mechanism for distributed transaction). 您不能低估这种复杂性(例如,数据库具有另一种分布式事务的超时机制)。

Another lightweight approach to consider is to use a SFSB that writes on the file system. 另一种需要考虑的轻量级方法是使用在文件系统上写入的SFSB If you implement SessionSynchronization interface, you get beforeCompletion and afterCompletion callbacks. 如果实现SessionSynchronization接口,则会获得beforeCompletionafterCompletion回调。 The later indicates whether the transaction was committed or rolled back and you do cleanup if necessary. 后者指示事务是已提交还是已回滚,并在必要时进行清理。 You can then implement a transactional behavior. 然后,您可以实现事务性行为。

JTA is not simply for Databases, it can be used long with any other resource if that resource supports XA transaction. JTA不仅仅适用于数据库,如果该资源支持XA事务,它可以与任何其他资源一起使用。 For example, XADisk enables one such integration of file-systems with XA transactions. 例如, XADisk可以通过XA事务实现文件系统的这种集成。 Hence, it can also solve the problem of file-system and database consistency which you have been trying to solve. 因此,它还可以解决您一直试图解决的文件系统和数据库一致性问题。

Hope that helps. 希望有所帮助。

Nitin 尼廷

manually. 手动。 You'll probably need to write compensatory transactions for this. 您可能需要为此编写补偿交易。

Maybe have a look at commons-transaction for transactional file access. 也许看看commons-transaction进行事务性文件访问。 Refer to: 参考:

In any case, you'll have to write files outside the EJB container or to interact with a JCA connector as pointed out by @ewernli. 在任何情况下,您都必须在EJB容器外部编写文件,或者与@ewernli指出的JCA连接器进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM