Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread programming approach correct or not ??  RSS feed

 
Sunitha Mudidani
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi in my application i have 2 threads which will be running parallely.
One thraed, Thread1 will query the datbase after certain interval and processes it.
After processing it will sleep for the interval.
Based on the logic it may need to query the database again(process is repeated without going to sleep)

The connection object is created in the main method and passed to Thread1.
the logic is all implemented in while(true) loop in run()method.

public void run(){

while(true){
//business logic
}

}


Only when connection is lost i will try to get a new connection.

My question is "Is this approach correct or is there any better way to do it"

Pls give your suggestions
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't hold a connection any longer than I need it to do the immediate database work. If it's going from one query right on to another operation that's fine. But if it's going to sleep for a while, I'd close the connection or return it to a connection pool. Are you running in an environment that supports connection pooling?
 
Sunitha Mudidani
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes we are using DataSource to obtain the connections

but the interval would be in the order of msec(probably 15msec which is configurable ) as the data in the DB need to be obtained in real time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!