This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I got one session bean responsible for registering and then calculations of transactions. When I fire the calculation, they can last up to 2 mins, updating customers balances. If within this timeframe customer will register another transaction, his balance is not updated by the values from calculation. This is so frustrating... I checked some materials over internet, books but I can't find the point to start. Earlier, I've been using EJB 2.1, which worked fine, but was very resource-consuming - blocked the customer balance table. Even stranger to me is the fact, that when I do those calculations, balance table is not updated (if customer reg. new transaction within timeframe), but another table when I insert info about calculation made works fine.
Trying to solve this problem, I added Thread.sleep(60000) to my calculation method (at the end) and tried to register new transaction myself. New transaction registration did not proceed until sleep finished (in another session, this is web app). I can't understand why it did not take updated balance if was waiting for finish of calculations anyway.
Any way to solve this nice way? Fx to force refresh of data during registration (i tried Entity Manager methods refresh, flush - no luck)?
If within this timeframe customer will register another transaction, his balance is not updated by the values from calculation. This is so frustrating...
Not much you can do about this. Assuming read committed transaction isolation other transactions cannot read data until the transaction has ended and been committed.
Trying to solve this problem, I added Thread.sleep(60000) to my calculation method (at the end) and tried to register new transaction myself
This is doing something the EJB specification explicitly bans.
Any way to solve this nice way?
I'd examine why the calculations take so long. 2 minutes is a very long time in an EJB application. What is the database doing that takes so long? Are there blocks? Missing indices? Is the database under specified? Are all the calculations necessary at login?
Jaikiran Pai wrote:How does the bean interact with the database? Does it use any datasources? What is the connection isolation level on the DB connection that's being used to interact with the DB?
yes, it uses postgres datasource with default connection isolation level - can't find the value but my bet is read uncommited. I tried commited and uncommited explicitly but also did not help.
I've tried SERIALIZABLE but this unfortunately resulted in error and double transaction registration on retry
Joined: Mar 14, 2006
one more thing I think could be problem:
In my session beans I've made a small mess with entity manager, not sure how it works now.
In one class I get entity manager through injection (@PersisentceContext), then sometimes I pass it as argument to constructor of another. But the thing that could be a problem is that in next place I use Persistence.createEntityManagerFactory("PU"). Of course I use the same persistence unit, but perhaps it creates different managers that got their own isolation levels?