Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: sql server connection close?

  1. #1
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default sql server connection close?

    Hi,

    I am using Netbeans, i have a button where any user clicks, it will open for database connection and later if connected, it will go for other processes, later if user wants to connect to other database and connects it, now will the first database connection will be closed or what?
    i mean to say when we open second database connection will first database connection be closed automatically? because i am not closing it via code.

    Regards
    Sandeep
    Last edited by sandeep43; 09-05-2011 at 02:08 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: sql server connection close?

    If it's using the same connection variable, then the original will become eligible for garbage collection and so will (probably) be closed when collected.

    Not knowing what you're doing then it's hard to say. I would recommend closing the connections yourself in your code, rather than relying on either the finaliser or the database itself to clean up after you. It's only polite...
    sandeep43 likes this.

  3. #3
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default Re: sql server connection close?

    Hi tolls,

    i am using the same connection variables, even i want the same to close the connection and reopen it, i will explain

    i have one button, say already i have open my connection and it is connected to say "master" DB, later when he has to change the database which he wants to connect then he has to click the same button, how to validate whether he is connected to database or not i used
    Java Code:
    connection.isvalid()
    which was not successful, any logic or idea tolls ?

    Regards
    Sandeep

  4. #4
    sabarii is offline Member
    Join Date
    Sep 2011
    Location
    Pondicherry
    Posts
    7
    Rep Power
    0

    Default Re: sql server connection close?

    connection pooling not possible in switching from one db to another db. If you use same db means it possible to maintain the connection. better you close the connection before switching to another db. or maintain separate button for db.

  5. #5
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default Re: sql server connection close?

    i dont want to maintain different buttons for different connections, when second connection is made i can close first connection, there is no more use of it, but i am not able to close using any condition to pass and check and then close it, so only i posted asking will there be any problem when opening second connection and first will be closed auto.

    Sandeep

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: sql server connection close?

    Java Code:
    if (connection != null) {
        close the connection.
    }
    open the new connection
    Why not something like that in the button action code?
    I really wouldn't rely on something happening automatically. Your DBA won't thank you.

  7. #7
    sabarii is offline Member
    Join Date
    Sep 2011
    Location
    Pondicherry
    Posts
    7
    Rep Power
    0

    Default Re: sql server connection close?

    will make problem, run the program few times,then see the active connection in db server. lot of connection remain in server. at last you can't establish the connection at certain limit. If you use any framework means, auto close is possible. otherwise code wise close the connection.

  8. #8
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default Re: sql server connection close?

    Tolls i tired the same thing, but it was entering into catch loop and when i had written
    Java Code:
     
    System.out.println(ex.getmessage());
    it was showing null always, i did not get any idea as y it was displaying null, later i decided to pass a boolean value, after user is connected to any database, i declared a boolean variable to true and was passing it and further when checking the same if true close it (when user continues for second connection) else do nothing, same result i got, where program entered into catch loop and therefore displaying null again.

    Regards
    Sandeep

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: sql server connection close?

    In a catch block always printStackTrace().
    That way you'll at least know which line is causing the problem.

    And without some actual code (and the full stack trace and error message, and having the problem line pointed out) we can't really help you.

  10. #10
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default Re: sql server connection close?

    tolls, sure i will try it and later will post the code by tomo without fail. i dont have the code what i have written right now. i will do it later.

    Thanks tolls.

    Regards
    Sandeep

Similar Threads

  1. Do we requires to close the connection?
    By RichersooN in forum JDBC
    Replies: 1
    Last Post: 04-24-2011, 11:20 AM
  2. Close connection object
    By ersachinjain in forum JDBC
    Replies: 1
    Last Post: 05-30-2010, 12:30 AM
  3. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  4. Connection the sql server
    By Preethi in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 03:05 PM
  5. How to close JDBC Connection
    By Heather in forum JDBC
    Replies: 2
    Last Post: 07-15-2007, 01:07 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
  •