This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes What is best way to wait for something in the db? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "What is best way to wait for something in the db?" Watch "What is best way to wait for something in the db?" New topic

What is best way to wait for something in the db?

Dan Wong

Joined: Sep 23, 2003
Posts: 6
I am writing a Servlet which amongst other things, needs to wait for a specified time for something that happens in the database. Would appreciate advice on which is the best way to do this.
Also some help on this please. I tried using a Swing timer thread which queries the database. The querying thread also displays a JProgressBar which shows the user it hasn't crashed.
Meanwhile, the parent thread must wait for the querying thread. To test, I did while (!done) {} which worked pretty well but I am not sure whether this is good practice in a multi-user environment especially when I noticed that the JProgressBar slowed down significantly.
So I tried doing a wait() in replacement of the while loop and a notify() where the program updated the global boolean "done". The error was something like wait did not have possession of the monitor.
Should I pursue what I was doing or is there a better way? Seems like such a general problem it should be the first thing introduced in db 101.
Thanks in advance.
Joel McNary

Joined: Aug 20, 2001
Posts: 1817

Unless I'm totally misunderstanding your architecture here, your approach won't work for a couple of reasons (unrelated to a database).
First of all, the progress bar that you see would be showing up on the server, not the client. (The code is called from the servlet, yes? And the servlet runs on the drinking and driving, it's never a good idea to mix Swing and Servlets)
Second, calls from a web browser do actually time out. If your process takes longer than (typically) 5 minutes, the web browser will decide that the web site is just not available. This may be configurable, but unless you have control over all computers that may visit your site, actually reconfiguring the browser is not a feasible approach.
What you can do is run your process from the servlet and then poll it at regular intervals (1/2 sec, 1 sec, 5 sec, whatever is appropriate for the process). If it's not complete, you will have to send a response back to the client indicating that it's not complete (you coauld even indicate the percent that it is complete, if you can get that data). Ideally, then, as long as the process is incomplete, the client can poll the servlet asking for the most recent status (this can be accomplished through JavaScript).
At any rate, this doesn't sound like this is actually a database question. It sounds as though you have a couple of issues here; you might try splitting your question apart and look in our Servlets, Threads and Synchronization, and/or HTML and JavaScript forums.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60810

All of Joel's points are spot-on. Since this is essentially a web application design issue, I'm moving it along to the Servlets Forum for any further discussion.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dan Wong

Joined: Sep 23, 2003
Posts: 6
You are right and thanks for the correction.
How would u wait for the database to be polled?
Thanks and Regards.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
How much delay are we talking about here? I think that any waiting longer than 60 sec or so should not be done inside a request/response cycle.
If it is a short time, your while(!done) could work if you have the servlet Thread sleep instead of continually looping.
Thread.sleep( 100 ) ; // 50 - 100 milliseconds is reasonable.
There have been a large number of discussions about how to handle waiting for slow database processes in the servlet and JSP forums. Browsing around or using the search facility will turn them up.
Dan Wong

Joined: Sep 23, 2003
Posts: 6
The wait is about 2 minutes.
I did look through the forum but apparently there are many things that people wait for and there is little about waiting for a database event. I think this is particularly important for a database because many
people share access so if I say keep querying the db every 1 sec., and there
are 1000 people using the program and other parts of the program also query and execute on the db ......
That is why I need advice on this and put it in the JDBC section. Even plain opinions would help.
I agree. Here's the link:
subject: What is best way to wait for something in the db?
Similar Threads
How to put ProgressMonitor bar while saving a file
design a web based fund search screen
JProgressBar is shown after data retrival
Simulating JProgressBar progress
Querying a Database from a session bean using JDBC