Connection Pool - Yash-777/LearnJava GitHub Wiki

Understanding Connection Pools

To store, organize, and retrieve data, most applications use relational databases. Java EE applications access relational databases through the JDBC API.

  • JDBC resource. A JDBC resource (data source) provides applications with a means of connecting to a database. Typically, the administrator creates a JDBC resource for each database accessed by the applications deployed in a domain. (However, more than one JDBC resource can be created for a database.) Each JDBC resource has a unique JNDI name.

  • JDBC Connection pool. A JDBC connection pool is a group of reusable connections that the application server maintains for a particular database. When an application closes a connection, the connection is returned to the pool. Connection pooling reduces the transaction time of connecting to a database by means of sharing connection objects providing access to a database source, thus avoiding a new physical connection being created every time a connection is requested.

At runtime, this is what happens when an application connects to a database:

  • Lookup JNDI name of JDBC resource. To connect to a database, the application looks up the JNDI name of the JDBC resource (data source) associated with the database. The JNDI API enables the application to locate the JDBC resource.

  • Locate JDBC connection pool. The JDBC resource specifies which connection pool to use. The pool defines connection attributes such as the database name (URL), user name, and password.

  • Retrieve connection from connection pool. The application server retrieves a physical connection from the connection pool that corresponds to the database. Now that the application is connected to the database, the application can read, modify, and add data to the database. Applications access the database by making calls to the JDBC API. The JDBC driver translates the application's JDBC calls into the protocol of the database server.

  • Close connection. When it is finished accessing the database, the application closes the connection. The application server returns the connection to the connection pool. Once it is back in the pool, the connection is available for the next application.

TIP « When you change the Context Path in the Project Properties dialog box, the IDE updates the Tomcat Web Server's context descriptor (META-INF/context.xml) or the GlassFish application server's deployment descriptor (WEB-INF/glassfish-web.xml) to match.