Browse DevX
Sign up for e-mail newsletters from DevX


Simple Solution for Complex Java Web Applications: Connection Pools and JDBC  : Page 2

To the extent that servlets enable you to store information between user requests, connection pools and JDBC can help generate Web content dynamically, quickly loading data from your database to your Web site without exhausting system resources.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Connection Pool Implementation
In the implementation in this article, a connection pool is a class with a number of JDBC Connection objects and access methods. You could also think of it as a container with a simple interface for controlling and managing database connections. To execute a SQL request, get data from the database, or change the data, open a database connection and get an object of Connection type. You will process all subsequent operations with this object. You can specify the size of your connection pool according to your available resources.

One class, SimpleConnectionPool, realizes a connection pool. It's comprised of a limited set of methods for database connection. In each case, functionality and class realization will vary depending on the existing task. This implementation acts as a basic skeleton for a connections storehouse.

The connection pool will do the following:

  • Enable a load-specific database driver for each database;
  • Get the reference to the SimpleConnectionPool object, which represents a connection pool exactly;
  • Get an available connection (of Connection type) from your storehouse;
  • Put a connection back into the storehouse;
  • Free all resources and closes all opened connections in your storehouse

Different implementations of a JDBC connection pool exist in different utility packages and frameworks (for example, the Jakarta Struts framework has org.apache.struts.util.GenericDataSource).

The SimpleConnectionPool class represents a uniform connections storehouse to a previously specified database. You need at least three arguments to access a database: the URL ID of the database, a user login, and a password. The URL ID consists of a protocol ID, a driver ID, and a database ID. The protocol ID is always jdbc, the driver ID could be odbc, oracle, mysql, etc., and the database ID depends only on the implementation of the database driver. For example, the URL ID "jdbc:odbc:test" can be used for database access via a JDBC-ODBC bridge, and the URL ID "jdbc:mysql:localhost/test" can be used to access a MySQL test database on localhost (some MySQL driver implementations require you to specify a user access login/password in the URL ID).

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date