Table of Contents
In this post, you will learn about the Connection Pool. This post is intended by java application development experts for entire development community to let people learn and acknowledge about Connection Pool and how to use its object using Service Locator design pattern.
Introduction
Connection pool is a mechanism which applicable for all technologies in the world. We can use tomcat server to configure the connection pool and we should use Service locator design pattern for better connection pool performance.
What is connection pool?
Connection pool is mechanism which avoids to creating new connection with Database for each client request which is coming from Application. Connecting with DB via programming is too expensive. There are many configurable connection pool mechanism in Java. Using connection pool mechanism we can maintain connection object in-memory. Whenever connection object is required, we can reuse the available connection from the pool of memory, instead of creating new connection unless all connection are occupied by the application. This article demonstrate how to configure connection pool in Apache Tomcat server and how to use those connection pool object using Service Locator design pattern.
What is Service Locator design pattern?
Service locator is a J2EE design pattern which tells how to interact with reusable business component which used in across the Application. For example, once you configured the connection pool in tomcat server, all available connection are available in Tomcat server in memory.
How do you get DB connection from the pool for your application?
There are many of ways to get the DB connection which configured in Tomcat Server. The best way is Service Locator design pattern. If want to create a Java class which should have service locator behavior, your class must follow following.
• Your class constructor must be private
• Your class should have static method which return a connection object and delegates to client.
The responsibility of private constructor is to create necessary service which used by application. In our example service locator creates the DBconnection and make it available in the in-memory.
Responsibility of static method is to give the services to client application which created by private constructor.
Following are the configuration and code implementation of creating Connection pool.
DB connection and connection pool configuration in Tomcat server.
1 2 3 |
In order to configure DB connection in tomcat server, open conf/context.xml and add above resource tag. Apply mysql connection properties as in above xml code. Configure maximum active connection and idle connection pool. In the above example 10 is maximum connection pool and idle is 4. To get this connection object in you java program, you need to create Service locator java class which used globally in your application. Wherever dB connection is need, the Service locator provide the connection object from java in-memory.
Following is static variable in Service Locator class private static Connection connection; Following is a private constructor which create Datasource using lookup method. In this example our datasource name is “jdbc/connectionTest”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private ConnectionPoolService() { try{ Context initContext = new InitialContext(); Context envContext =(Context)initContext.lookup("java:/comp/env"); DataSource dataSource =(DataSource)envContext.lookup("jdbc/connectionTest"); connection = dataSource.getConnection(); } catch (NamingException e) { e.printStackTrace(); }catch(Exception ex){ ex.printStackTrace(); } } |
1 2 3 4 5 6 7 8 |
public static Connection getConnection() { if(connection == null) { new ConnectionPoolService(); } return connection; } |
The below code demonstrates that how to get connection object from ConnectionPoolService.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public Customer getCustomerData(long customerId) throws MyDAOException { Connection connection = null; Customer customer = null; try { ResultSet resultSet = null; connection = ConnectionPoolService.getConnection(); Statement statement = connection.createStatement(); String query = "SELECT * FROM customer where customer_id=" + customerId; resultSet = statement.executeQuery(query); customer = new Customer(); while (resultSet.next()) { customer.setCustomerId(Long.parseLong(resultSet.getString(1))); customer.setCustomerName(resultSet.getString(2)); customer.setEmailId(resultSet.getString(3)); } }catch(Exception ex){ ex.printStackTrace(); throw new MyDAOException("Error while connecting db"); } |
1 2 3 |
finally |
1 2 3 |
{ |
1 2 3 |
resultSet.close(); |
1 2 3 |
connection.close(); |
1 2 3 4 5 |
} return customer; } |
1 2 3 |
connection = ConnectionPoolService.getConnection(); |
Conclusion
Service locator is a J2EE Design pattern which use the concept of Java singleton design pattern. The use of singleton design pattern is to create a java object only once and used in across the application. In our case private constructor create connection object only once. If connection is already available, it reused by the application. Why it is called as Service locator? If private constructor creates only java instance, we can say it is singleton design pattern but the private constructor creating DB resource which is being serviced across the application so, it is called as Service Locator design pattern
Below diagram explains how service locator works to get connection pool object from the tomcat server.
Using service locator mechanism you can adopt this code for any service you want to pick from particular source.
Hope you have understood the use of Connection Pool object with Service locator design pattern in java application development. For any confusion, ask expert assistance.
Bio:- Aaron Jacobson is expertise in java application development. He has been working as a java developers in Technoligent for many years. He major contribute included the collection of web solutions like java, Python, Asp.Net, Mobile Apps etc…. He has extensive experience in java technology and object oriented programming language. You can follow me on twitter@Techno_Ligent and you can add friend on Facebook @TechnoLigent