Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Db connection in a process

 
angel sar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I have this scenario: A java process will run every minute. This process read a file, make alot of logic with the file and then insert and update records of a table in a data base. Since it is a process (a daemon) and it doesnt have a container (like tomcat) to store a connection pool, how would i mange the DB connection? I think its a bad idea to open and close a connection every minute that my process runs.
Any suggestions??? Thanks a lot.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this java process starts, does db work, then terminates, you have no choice but to establish a new connection each time it runs and connection cannot be held open across process boundaries.

You might consider converting it into an app deployed to a container and using a timer service, such as Quartz, to start the app every minute. Then the app could make use of the connection pool provided by the container.
 
angel sar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer. You mean i have to deploy the application like a war?? If so, how my timer service can call any class of my application? Please give me more details.

Thanks.
 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you start down that road, consider the cost of opening a database connection once a minute. It's probably minuscule compared to a minute. Don't just "think" that it's a bad idea, investigate how bad it actually is. You may save yourself a lot of work.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul's advice is good advice, heed it before you go any further.

I don't think a WAR is a goo idea, probably an EJB would be better.

However, you will then have traded the cost of establishing a database connection every minute with the cost of continuously running an app server which will definitely have larger memory requirements than your app.

Another possibility: instead of firing off your app every minute, why not have the app run continuously, pausing a minute between database accesses. Here is pseuduocode:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic