|
It's been recently discussed using the <META
REFRESH=...> tag to refresh a page while it's executing; you probably want to
spin off a Thread to do the actual query, but you might want to check your
servlet engine's documentation before jumping into this head-first. A number of
Web servers are starting to do some clustering/scaling behind the scenes, and
spinning off a thread can really screw up their algorithms (which is why EJB
forbids Beans from starting a Thread).
Spinning a Thread off is as simple as creating
an anonymous Runnable class to do the work:
Connection conn =
getTheConnectionFromSomeplace();
Statement stmt =
conn.createStatement();
String SQL = "...";
Thread workerThread = new Thread(new Runnable()
{
public void run()
{
try
{
stmt.executeQuery(SQL); // the usual JDBC thing in here
}
catch (Exception
x)
{
....
}
}
});
workerThread.start();
while (workerThread.isAlive())
{
// Thread is still running, so keep sending
back "Please wait" pages....
}
Because Runnable.run() doesn't declare any throws clause, you
need to catch all Exceptions within run() and handle them in there. Then, when
your query completes and stores the result set someplace, workerThread
"dies", and the isAlive() test comes back false.
You may want to make the anonymous Runnable class a
full-fledged public class; anonymous inner classes sometimes have
seemingly-strange rules regarding access of members of the enclosing class. It
also permits you to reuse the class in a variety of places and
situations.
HTH.
Ted
Neward
Patterns/C++/Java/CORBA/EJB/COM-DCOM spoken here http://www.javageeks.com/~tneward "I don't even speak for myself; my wife won't let me." --Me
|
- Waiting for a query Kenia Nimesh
- Re: Waiting for a query Steven J. Owens
- Re: Waiting for a query Jean-Michel Augusto
- Re: Waiting for a query Ted Neward
- Re: Waiting for a query Andras Balogh
