my dog learned polymorphism*
The moose likes JDBC and the fly likes Db connection in a process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Db connection in a process" Watch "Db connection in a process" New topic
Author

Db connection in a process

angel sar
Greenhorn

Joined: Aug 29, 2009
Posts: 17
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

Joined: May 14, 2008
Posts: 5812
    
    7

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.


JBoss In Action
angel sar
Greenhorn

Joined: Aug 29, 2009
Posts: 17
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: May 14, 2008
Posts: 5812
    
    7

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:

 
Don't get me started about those stupid light bulbs.
 
subject: Db connection in a process
 
Similar Threads
Hibernate re-writting dirty data
JDBC inserts too slow
JDBC insert too slow
Need advice on improving performance
B&S-How to deal with some magic number?