• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and Waiting for DB connection

 
jesse harris
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dana Hanna
Ranch Hand
Posts: 227
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic