Results 1 to 8 of 8
  1. #1
    SaraHrry is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default Connection In Java

    Is it a good programming practice to open a connection to the database in a constructor in Java.

    Please advise.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Better move it to a method, like 'openConnection(database)'.
    It's cleaner.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    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.

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    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 ?"

  5. #5
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    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.

  7. #7
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    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 ?"

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    There is a small disadvantage if you close your Connection a.s.a.p. you lose your PreparedStatements when you close your Connection. A ConnectionPool that keeps your Connections alive keeps your PreparedStatements open but then again it leaves the Connection open as well.

    kind regards,

    Jos

Similar Threads

  1. Java-mysql connection
    By Kligham in forum New To Java
    Replies: 16
    Last Post: 11-25-2009, 01:28 PM
  2. java mysql connection
    By sysout in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 10:48 AM
  3. Establish VPN Connection in Java
    By charandevi in forum Networking
    Replies: 1
    Last Post: 10-27-2009, 03:57 PM
  4. I need help with java-oracle connection
    By digioleg in forum JDBC
    Replies: 1
    Last Post: 08-08-2009, 09:29 AM
  5. java-vnc connection to localhost
    By pdc124 in forum Java Applets
    Replies: 0
    Last Post: 07-26-2009, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •