简体   繁体   English

最佳交易管理方式

[英]Best way to manage transactions

I've the JBoss and Hibernate based system. 我有基于JBoss和Hibernate的系统。 And I need to process two long operations. 我需要处理两个长时间的操作。 Operations can be probably longer than transaction's timeout. 操作可能比事务的超时时间更长。 It operations are persists many-many entities, in two different transactions. 它的操作是在两个不同的事务中保留许多实体的持久性。 And if something goes wrong, during this operations, I should rollback all changes of the transactions. 如果出现问题,在此操作期间,我应回滚所有事务更改。

What's the best way to resolve it? 解决该问题的最佳方法是什么?

I think, the best way is merge all operations to one transaction, but it requires to set LOng transaction timeout, and it unacceptable for our system. 我认为,最好的方法是将所有操作合并到一个事务中,但这需要设置LOng事务超时,这对于我们的系统而言是不可接受的。

Is the managing of many transactions better in this situation. 在这种情况下,对许多交易的管理是否更好? And how can I do it? 我该怎么办呢?

Can you use a JTA server (transaction coordinator) to do the transaction in multiple steps with XA transactions? 您可以使用JTA服务器(事务协调器)对XA事务分多个步骤进行事务吗? This posting has a bit of fan-out to a couple of open-source ones that might work for you. 这篇文章对一些可能对您有用的开放源代码有所帮助。

XA transactions allow you to do a bunch of smaller operations and commit or roll back them in one hit. XA事务允许您执行许多较小的操作,并在一击中提交或回滚它们。 Try googling for 'hibernate xa transactions'. 尝试搜索“休眠XA交易”。 Most modern RDBMS platforms (MySQL 5.x, PostgreSQL, SQL Server, Oracle, DB2, Sybase etc.) support XA transactions. 大多数现代RDBMS平台(MySQL 5.x,PostgreSQL,SQL Server,Oracle,DB2,Sybase等)都支持XA事务。

Using one transaction is good, robust and so on. 使用一次交易是好的,强大的等等。 Try again to convince your administrator ;-) 再试一次以说服您的管理员;-)


Otherwise, I can think of: 否则,我会想到:

  • using shorter transactions to insert fewer rows, using a marker to mean "incomplete ". 使用较短的事务插入较少的行,使用标记表示“未完成 ”。 This could be in an existing column or a new one (maybe it could be in a different table?). 这可以在现有列中,也可以在新列中(也许可以在其他表中?)。
  • if any transaction rollbacks, try it again ; 如果有任何事务回滚,请重试; if you prefer to abort the whole operation, you can choose delete the rows that have the "incomplete" marker. 如果您希望中止整个操作,则可以选择删除具有“不完整”标记的行。
  • when all transactions are commited, you can open a last transaction, just for marking all rows as "complete" (ex: update ... where ... ). 提交所有事务后,您可以打开最后一个事务,仅用于将所有行标记为“完成” (例如: update ... where ... )。 This should be much faster than the full insertion was, so the transaction should be short enough. 这应该比完整插入要快得多,因此事务应该足够短。

I had to do something similar in JPA (via hibernate). 我必须在JPA中执行类似的操作(通过休眠)。 I did the operation in N transactions rather than 1 big transaction. 我执行了N次交易,而不是1次大交易。 This operation ran rarely - it was an automated job - so there was no user waiting at a web page for a result. 此操作很少执行-这是自动化工作-因此没有用户在网页上等待结果。

How long do you expect the transaction to take? 您预计交易需要多长时间? How long do you expect the user to wait?? 您希望用户等待多长时间? How often do you expect this operation to occur. 您希望多久执行一次此操作。 Maybe you're requirements are not clear. 也许您的要求不清楚。

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

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