aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads and Waiting for DB connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and Waiting for DB connection" Watch "Threads and Waiting for DB connection" New topic
Author

Threads and Waiting for DB connection

jesse harris
Ranch Hand

Joined: Oct 02, 2000
Posts: 62
I have a problem with one of my apps, when I attempt to get a DB connection or execute a query sometimes a table is locked, or the resource is unavailable. When this happens the application grinds down to a crawl because the requests are queing up, I have an alternate data source that i can access when my primary resource is unavailable. So i was thining threads are the solution to this problem. I assume I would have the call to the DB in a thread and also have a timer thread, so that if the DB call doesnt return by the time the timer thread wakes, then i teminate the DB call. Is this the proper way of thinking about this problem?
Here are some issues I am running into,
first if my Db class extends thread, then the only way for me to have it execute in its own thread is to call start, and have a run method. This would mean that each class that extends Thread can only execute the code in its run method and the code run calls(and still be in its own thread) right?
Wouldnt this be a problem, for example I have a DB manager class that you pass a SQL statement to and it will execute a query or an update, or an insert, how would I pass the statement if run doesnt take arguments?
am I totally lost here?
thanks guys
Jesse
jesse harris
Ranch Hand

Joined: Oct 02, 2000
Posts: 62
how about something like this
//generator would make the DB call
Generator g = new Generator();
g.start();

try
{
Thread.sleep(1000);
if (!g.done)
{
g.stop();
g = null;
}
}
catch (InterruptedException e)
{

e.printStackTrace();
}

so in this instance, we would be in the method of the DB mgr class ther statement would be passed to that method, how would the generator class get valuse passed to it, should there be instance variables in the generator class that I set, then it uses those valuse to do its work?
jesse harris
Ranch Hand

Joined: Oct 02, 2000
Posts: 62
Also stop is deprecated, how should I be killing the stuck thread, if my call to the resource is one line and it halts on that line then how could I stop it other than calling stop(), there is no looping and there is no way to set a flag and detect it right?
thanks
Jesse
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

Originally posted by jesse harris:
This would mean that each class that extends Thread can only execute the code in its run method and the code run calls(and still be in its own thread) right?

Any method that run() calls executes on the new thread of execution. There's a couple of interesting tricks you can perform with Thread/Runnable. Check out the Java Tutorial: Threads trail for the basics. Since threads take time and resources to start, it is rarely worth it to launch a thread to perform a single task, say perform a database query. You probably want to create a producer-consumer pattern with a timer so if the query doesn't return in a reasonable amount of time you can return an error. Forget about terminating the query thread, that's a Bad Idea. Just let it run it's course and return it to a pool of ready execution threads.
[ November 26, 2003: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
The java.sql.Statement object has a cancel method, and it works quite well! I've used it with DB2, UDB, Oracle, MySQL, ODBC Bridge.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Threads and Waiting for DB connection