Announcement Announcement Module
Collapse
No announcement yet.
How do connections get released to a pool? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do connections get released to a pool?

    If you set the data source to say DBCP BasicDataSource and use a MappingSqlQuery, I assume that Spring calls bds.getConnection(), but how does that connection get released. Why don't we need some sort of bds.releaseConnection(Connection)?

    Please forgive my ignorance of Spring/DBCP here, but we're trying to evolve to Spring from a homegrown connection pool. The standard pattern of our pool is:

    try {
    conn = pool.allocateConnection()
    statement = conn.createStatement()
    ...
    } catch {
    ...
    } finally {
    statement.close();
    pool.releaseConnection(conn);
    }

    Thanks.

  • #2
    Basically, Spring gets a new connection when you call a peace of code wrapped by a transaction; Spring releases the connection to the pool after the code returns. You can configure transactions using declarative transaction demarcation into applicationContext.xml. Connection management will be taken care of, behind the scene, by Spring.
    One cool feature of Spring Transaction / Connection / Resource management, is that if you wrap two calls within the same transaction, Spring will use the same connection for both calls.
    HTH

    Comment


    • #3
      And if I don't have a transaction? I'm just doing read-only queries...

      Comment


      • #4
        and the other question I guess is just with the connection.close is that effectively just a release back to the pool? ... how does one know vs. is it actually a close of the connection?

        Comment


        • #5
          Originally posted by rpark68
          And if I don't have a transaction? I'm just doing read-only queries...
          You can still use transaction in read only mode, just quote them readOnly.

          Comment


          • #6
            Originally posted by rpark68
            and the other question I guess is just with the connection.close is that effectively just a release back to the pool? ... how does one know vs. is it actually a close of the connection?
            This depends on the datasource implementation you are using. In the case of Apache Commons DBCP, calling close on the connection will put the it back into the pool.

            Comment


            • #7
              thanks very much for the info

              Comment

              Working...
              X