How can I bind data to a JTA transaction? (or uniquely identify it)

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

My goal is that in a JTA transaction, the first call to getStockQuote () will receive a stock quote, but all subsequent calls within the same transaction will reuse the same stock quote (for example: it will not try to get a new quote). If another transaction starts or another transaction is running at the same time, I expect the other transaction to receive its own stock quote when it is called for the first time.

This is an attempt to ensure transaction consistency - so that all calculations in a transaction are based on the same stock price.

This will be similar to how you can configure JPA providers only once from the database row from the database and use the cached value to subsequently access the same database row in the transaction.

Does anyone have any tips on how this can be achieved?

+4
source share
2 answers

This will require some testing, but I think you could bind the quote to ThreadLocal and make your beans a href = "http://java.sun.com/javaee/5/docs/api/javax/ejb/SessionSynchronization.html" rel = "nofollow noreferrer"> SessionSynchronization to cancel a quote from ThreadLocal after the transaction (and thus implement a kind of context with the transaction scope).

0
source

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

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

0
source

Source: https://habr.com/ru/post/1310053/


All Articles