• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cleanup Order

 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using mysql 5.x with java 6, I use DriverManager to get db connection, no connection pooling.
My db is external (I use external IP).
My run() has a infinite loop wherein I get db connection, query the db for a resultsut and work on it.
I have my own getConnection() which checks if the connection object is not null, only if null it creates a new connection.
So I basically try to use just one connection forever untill it starts acting up. Then I just create a new connnection and keep using it.
Sometimes at executestatement() my connection is broken.
I have following code..

I am not sure if the order of close()s I am doing it ok. I cannn't con.close() in finally because I reuse it forever. Should I rather rs.close()and pstmnt.close() in catch{} also in addition to finally?
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a downside to this approach? Can I just keep using the same connection for as long as I can? I just poll the db regularly (half a sec) to get a resultset. That is the only place where I hit the db, save one more. So all I have is just two connections open to db forever ( or as long as possibl).
My application runs as a single instance (only one machine ever-so only chance of two connections from the app to the db at most at any given time. ). Although, the database will be hit by other applications.
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please take a look at this and comment? My am at my wit's end on this.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looping forever is never a good thing. Have you tried a more advanced approach like using a quartz scheduler?
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott,

Quartz scheduler looks much better. However, changing to a different solution is not an option right now, I will make sure to check it first next time.
But my problem is more with the database connection than anything else. I am not able to smoothly recover from a lost connection.


I just want someone to confirm that what I have there is acceptable and reasonable.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, you should never keep connections open for long periods of time if you don't need them. It's far better (safer) to close them as soon as you are done with them.
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I do kinda need the connection forever.

The thread is a polling thread that hits the db for a resultset change, so that we handle teh change as real time as possible. I have my polling set to twice a second.
The entire application has only two singleton classes that do thsi kind of polling. Only one instance of my app runs at any time. there is another web app that hits the db, but my app adds only two connections in terms of overhead.

I didn't want to implement connection pooling for such a simple scenario (as far as my app is concerned. web connections etc depend on user load ), and I didn't want to 'getConnection', a new one every half a second. So I am just using the current two connections for as long as they last.

Is it bad? Should I rather get new connection everytime? so that would around four new connections (apart from web side db connection) per second.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic