Sunday 3 March 2013

What is Connection pooling? What are the advantages of using a connection pool?


  • A connection pool operates by performing the work of creating connections ahead of time.
  • In the case of a JDBC connection pool, a pool of Connection objects is created at the time the application server starts.
  • These objects are then managed by a pool manager that disperses connections as they are requested by clients and returns them to the pool when it determines the client is finished with the Connection object.
  • When the connection pool server starts, it creates a predetermined number of Connection objects. 
  • A client application would then perform a JNDI lookup to retrieve a reference to a DataSource object that implements the ConnectionPoolDataSource interface.
  • When the client application requests a connection from the ConnetionPoolDataSource, the data source implementation would retrieve a physical connection to the client application.
  • The ConnectionPoolDataSource would return a Connection object that implemented the PooledConnection interface.
  • The PooledConnection interface dictates the use of event listeners. 
  • These event listeners allow the connection pool manager to capture important connection events, such as attempts by the client application to close the connection.
  • When the driver traps a close-connection event, it intercedes and performs a pseudo-close operation that merely takes the Connection object, returns it to the pool of available connection.
  • The operation of the connection pool should be completely transparent to the client application.
  • The triggering of connection events, the manipulation of the object pool, and the creation and destruction of physical connections are all managed by the pool manager.


Context.xml

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="" driverClassName="com.mysql.jdbc.Driver"               url="jdbc:mysql://localhost:3306/cdcol"/>

 //This should be added in the servers context,xml file. For example if you are using apache server then the context.xml will be found in C:\apache-tomcat-6.0.26\conf\Context.xml

web.xml

  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

//This should be added in the web.xml of the local project. (Not in server's web.xml).

For implementation example please refer to: 

Download DB values as CSV file with JNDI Connection Pool Method

No comments:

Post a Comment