Reputation: 3805
As I know connection is kind of very expensive operation. Which approach should I follow to handle few connections? I have seen a lots of examples where author talks about only one connection or uses connection pool(from JBoss or something). My code looks like this:
1) make new connection
2) do some logic
3) close connection
DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
con = DriverManager.getConnection(URL, username, password);
if(con!=null)
System.out.println("Connection Successful !\n");
//logic
if(con!=null)
con.close();
But I've got few similar methods based on this approach. So do I really have to make new connection or should I use the only one? If I use one connection, do I need to close it after?
PS. App is not a servlet.
Upvotes: 0
Views: 73
Reputation: 4654
You can use a connection pool like http://sourceforge.net/projects/c3p0/ or http://jolbox.com/ to abstract from this problem.
If you do not close connections after use, you will have lots of opening connections that are not in use (until database cleans up).
Upvotes: 3