aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Dirty read (?) problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Dirty read (?) problem" Watch "Dirty read (?) problem" New topic
Author

Dirty read (?) problem

Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
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)?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
it's not during login, but once per few hours. The calculation takes long time because it takes transactions of around 1500-2000 customers and uses multiple tables.

well, if you say I can't do anything about this, I guess the only solution is to calculate 1 ejb transaction = 1 customer transaction. But anyway, is it possible at least to do it this way:

- still I run one session bean with submethods like:

bean.calculate() and inside i got method calculateSingleCustomer() which I run inside loop.

I mean, can I solve it by using some combination of ejb transaction attributes for methods?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


it's not during login, but once per few hours. The calculation takes long time because it takes transactions of around 1500-2000 customers and uses multiple tables

In which case does this need to be done in an EJB? Could you not do it in batches via a timer bean or some similar mechanism? That might lower the load on the database.
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
actually to be precise, it's triggered by admin in web application once per few minutes - EJB is needed to rollback modifications to all tables affected when any error pops up.


did I mention now I use ejb3? It shouldn't be that big load like earlier in case of 2.1
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10051
    
163

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?

[My Blog] [JavaRanch Journal]
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
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
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
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?
 
wood burning stoves
 
subject: Dirty read (?) problem