Results 1 to 2 of 2
  1. #1
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default JDBC DataBase Connection

    Hello

    What is the best way to manipulate a database connection?
    I have a .properties file with all necessary information to create a DataSource, after reading this file once, I have instance of SQLServerDataSource dataSource available.

    What I was doing was, for each method that interact with database , I create a Connection con = dataSource.getConnection() at start, and at the end I close that connection con.close().


    Is this the right way? Or should I always use the same connection and close it in the end of "application"?
    Ubuntu, Linux for human Beings!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I think the convention of a long lived database connection (e.g. application lifetime, or connection for user logged in session lifetime) is generally useful for when a very small number of concurrent users is expected, like perhaps 6 to 50 users at once. ever.
    or where perhaps the database is itself part of the application logic and it makes sense for the user to log into the database as their user name.

    Typically in web-based applications the connection per use (like request scope) where a connection is fetched from a pool, used, and then 'closed', which really doesn't close it but returns it to the pool; is a much more scaleable mechanism to allow for more concurrent users, and possibly higher thruput or performance. This also assumes the database connection would be an application user (e.g. not the requesting user's credentials).
    Though there are ways to have a connection per user logged in session in a web application as well. I would say to use the pool based connection per request unless there is a compelling reason to use conection per application.

Similar Threads

  1. JDBC connection
    By evermore in forum JDBC
    Replies: 1
    Last Post: 03-19-2010, 09:35 AM
  2. Replies: 0
    Last Post: 12-01-2009, 11:01 PM
  3. JDBC Connection...
    By onlysumitg in forum JDBC
    Replies: 3
    Last Post: 08-21-2009, 12:34 PM
  4. jdbc connection problem
    By Assaf A in forum JDBC
    Replies: 0
    Last Post: 12-01-2008, 06:08 PM
  5. JDBC connection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:39 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
  •