Results 1 to 8 of 8
Thread: Connection In Java
- 01-06-2010, 08:54 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 1
- Rep Power
- 0
- 01-06-2010, 09:26 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Better move it to a method, like 'openConnection(database)'.
It's cleaner.I die a little on the inside...
Every time I get shot.
- 01-07-2010, 09:08 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Generally (and there are always exceptions, so do not take this is a hard and fast rule) connections should be opened and closed in the shortest window possible, to do their job. So opening one in a constructor for use over an unspecified period of time isn't usually a good idea.
The specifics of your situation may change this, but it's unlikely.
- 01-08-2010, 12:29 AM #4
tolls, can you explain this issue? why it is bad to maintain a connection the whole time?
"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 01-08-2010, 02:37 AM #5
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
Listen to Tolls suggestion.
You have to think about situation when very large number of users
need a DB connection at the same time - and you db driver supports
let's say 125 open connection in some short period of time.
You don't want them to wait for some other lazy user to finish his work
and close his connection.
So, create a separate ConnectionCreator class with methods for
connection creation and closing and be sure you use finally{} block
to close your connections in every method you work with them.
Nicely define your app methods and enclose your logic between open() and close() of db connections.
Takes some time and good practice to clear this things in your head.
regards
- 01-08-2010, 08:51 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
In addition to what FON says, there is the flip side in that connections do not remain open indefintely. They timeout (based on db settings, or driver settings). Consequently you'd have to stick in "connection repair" code in to ensure your single connection is maintained.
- 01-08-2010, 02:17 PM #7
ty,
i recently took a look to my serverstatus ... with hundreds open connections :-)
thanks for the tip"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 01-08-2010, 05:47 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Java-mysql connection
By Kligham in forum New To JavaReplies: 16Last Post: 11-25-2009, 01:28 PM -
java mysql connection
By sysout in forum New To JavaReplies: 5Last Post: 10-31-2009, 10:48 AM -
Establish VPN Connection in Java
By charandevi in forum NetworkingReplies: 1Last Post: 10-27-2009, 03:57 PM -
I need help with java-oracle connection
By digioleg in forum JDBCReplies: 1Last Post: 08-08-2009, 09:29 AM -
java-vnc connection to localhost
By pdc124 in forum Java AppletsReplies: 0Last Post: 07-26-2009, 01:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks