How to use connection pool using service locator design Pattern in java

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.


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.

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”

jdbc/connectionTest is datasource name configured in conf/context.xml file, the lookup method successfully find the datasource name, it will assign to DataSource object. Using Datasource object,we can get the connection object. The above particular code should execute only once in the application because, the same connection object should use wherever db connection required. The entire code is placed in private constructor which means we can’t call this constructor in outside of the class.
The above method is a static method which returns the connection object. This method invokes the private constructor and creates the connection object. According to this method code, whenever invoke this method, first check the connection object availability. If connection object is not available, it invokes the private constructor to create new connection object. If connection object is already available, this method doesn’t invoke the private constructor instead it will return the available existing connection from the java in-memory. Through this way we can avoid the initializing new connection object for each DBs connection request.
The below code demonstrates that how to get connection object from ConnectionPoolService.
The above method invokes the following line which returns the connection object.
This method either directly returns connection from the java in-memory or creates the new connection based on availability of connection object.


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

Related Posts

  • 25 April

    Strategy design pattern in java

    Table of ContentsStrategy design pattern exampleStrategy pattern example In this post, we will see about Strategy design pattern in java. Strategy design pattern allows us to change algorithm implementation at runtime.Strategy design pattern provides multiple algorithms and client can choose algorithm based on their needs with the help of composition. Strategy design pattern example Let’s […]

  • 24 April

    Singleton design pattern in java

    Table of ContentsSingleton classEager initializationLazy initializationThread safe SingletonDouble check lockingBill Pugh Singleton ImplementationCreate singleton using EnumTesting singleton with ReflectionTesting singleton with SerializationTesting singleton with Cloning. In this post, we will see about Singleton design pattern in java. Singleton design pattern is one of the simplest design pattern but singleton property can be broken using multithreading, […]

  • 01 March

    Template method design pattern in java

    Table of ContentsExample:Used in java API:Source code: CodeProjectTemplate method pattern is a behavioral design pattern which provide base method for algorithm,called template method which defers some of its steps to subclasses So algorithm structure is same but some of its steps can be redefined by subclasses according to context. Template means Preset format like HTML […]

  • 27 February

    Observer design pattern in java

    Table of ContentsWhen to use it: UML diagram for observer design pattern:Components:Java in-built API for observer pattern:Example:Using java inbuilt APIs:Some important points about observer pattern:Source code: As the name suggests it is used for observing some objects.Observer watch for any change in state or property of subject.Suppose you are interested in particular object and want to […]

  • 29 September

    Builder design pattern in java

    Table of ContentsGeneric UML diagram for builder design pattern:Elements:When to use it:WorkFlow:Example: Builder design pattern allows to create complex object step by step and also enforces a process to create an object as a finished product.Construction of object should be such that same construction process can create different representations.Director controls the construction of object and […]

  • 28 September

    Proxy design pattern in java

    Table of ContentsWhen to use it:UML diagram for proxy design pattern:Elements:WorkFlow:Example:Source code: CodeProjectProxy design pattern allows you to create a wrapper class over real object.Wrapper class which is proxy,controls access to real object so in turn you can add extra functionalities to real object without changing real object’s code. As described by GoF: “Provide a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.