如何将数据附加到JTA事务? (或唯一识别它)

时间:2021-07-24 23:58:11

I have a getStockQuote() function that will get a current stock quote for a symbol from the stock market.

我有一个getStockQuote()函数,它将从股票市场获得符号的当前股票报价。

My goal is that within a JTA transaction, the first call to getStockQuote() will fetch a stock quote, but all subsequent calls within the same transaction will reuse the same stock quote (e.g.: it will not try to fetch a new quote). If a different transaction starts, or another transaction runs concurrently, i would expect the other transaction to fetch its own stock quote on its first call.

我的目标是在JTA事务中,第一次调用getStockQuote()将获取股票报价,但同一交易中的所有后续调用将重复使用相同的股票报价(例如:它不会尝试获取新报价)。如果另一个事务开始,或者另一个事务同时运行,我希望另一个事务在第一次调用时获取自己的股票报价。

This is to try to ensure consistency within the transaction - so that all calculations within the transaction are based on the same stock price.

这是为了确保交易内的一致性 - 以便交易中的所有计算都基于相同的股票价格。

This would be similar to how you can configure JPA providers to only fetch a database row from the database once, and use the cached value for subsequent access to the same database row within the transaction.

这类似于您可以将JPA提供程序配置为仅从数据库中获取一次数据库行,并使用缓存值来对事务中的同一数据库行进行后续访问。

Does anyone have tips on how this can be achieved?

有没有人有关于如何实现这一目标的提示?

2 个解决方案

#1


0  

This would require some testing but I think that you could bind the quote to a ThreadLocal and make your beans implement SessionSynchronization to unbind the quote from the ThreadLocal after the commit of a transaction (and thus implement a kind of transaction-scoped context).

这需要一些测试,但我认为您可以将引用绑定到ThreadLocal并使您的bean实现SessionSynchronization以在提交事务之后解除ThreadLocal中的引用(从而实现一种事务范围的上下文)。

#2


0  

consider using spring for transaction management, it provides this functionality out of the box:

考虑使用spring进行事务管理,它提供了开箱即用的功能:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-propagation

#1


0  

This would require some testing but I think that you could bind the quote to a ThreadLocal and make your beans implement SessionSynchronization to unbind the quote from the ThreadLocal after the commit of a transaction (and thus implement a kind of transaction-scoped context).

这需要一些测试,但我认为您可以将引用绑定到ThreadLocal并使您的bean实现SessionSynchronization以在提交事务之后解除ThreadLocal中的引用(从而实现一种事务范围的上下文)。

#2


0  

consider using spring for transaction management, it provides this functionality out of the box:

考虑使用spring进行事务管理,它提供了开箱即用的功能:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-propagation