1

I think I may not be the first one with this problem. Sometimes, the user submits a bunch of data to the server, and these data is going to be displayed in the response page. In order to give users the illusion that the data submission and process is fast. We usually do this asynchronously. Now the problem is, for some reason, these data need to go to database first, and be fetched to appear in the response page. If the response page is displayed to the user too fast, asynchronous submission may not finish; Now I call

Thread.sleep();

before I call I setResponsePage().

but native thread is not recommended in EJB. Anyone knows alternatives ? Thanks

Wangge
  • 462
  • 1
  • 4
  • 9

1 Answers1

0

It's just been discussed in this question: Thread.sleep() in an EJB.

I'd split the logic into two EJBs: one for inserting the user data into DB, and one for fetching it. Your web layer would call one after the other, resulting in two separate transactions, which should be ordered properly by the database (still, it might depend on other factors, like transaction isolation).

EDIT

The problem with sleep() is that you never know how long to wait, so it's almost always a bad idea. I see a case here for Ajax push — your EJB should return immediately with a page to which data will be pushed when processing is complete. I won't advise you further on this topic, as I'm far from expertise in this area.

A still imperfect, but better than sleep(), could be syncing on database locks: first EJB would insert data and lock some record in its transaction, the second EJB would try to lock the same record and read the data. This way the second EJB would wait for minimal time that's needed.

Community
  • 1
  • 1
MaDa
  • 10,511
  • 9
  • 46
  • 84
  • It makes sense to separate the logic into 2 transactions, and order them appropriately. The problem is that, the inserting may take quite a while. For example, we insert meta data of images into database while uploading several copies of different sizes to the cloud. Still, we can separate this into two steps: update DB and uploading images. I am using EJB 3.0, which supports org.jboss.ejb3.common.proxy.plugins.async.AsyncUtils.mixinAsync(). I don't know how soon the task is performed when it's called via the asynchronous proxy. So I need to pause the process for a reasonable period. – Wangge Nov 24 '11 at 19:32