Use Connection Pool in Spring

Use Connection Pool in Spring


Note: Before going through this, learn what is connection pool and how to create it in Weblogic application server available at Creating Connection Pool & Data Source in Weblogic 8.1.

  1. Before doing this program, place weblogic.jar file in project space as follows.

    R-click on project –> Build Path –> Configure Build Path –> Libraries tab –> Add External Jars –> select weblogic.jar.

  2. weblogic.jar file is available in the following weblogic installation path.

    C:\bea\weblogic81\server\lib
  3. Before executing the client program, see that Weblogic server is started. Learn how to start and using the server in a separate notes of "creating connection pool data source".

1. Bean Progrfam: CPMBean.java

2. applicaitonContext.xml file

<property name="jndiName" value="SNRaoDataSource">

SNRaoDataSource is the JNDI name given for data source (attached to a connection pool) in weblogic server; refer my notes on "creating connection pool data source".

3. Client Program: Client.java (using IOC adopted by Spring)

Console screen when the above client program is executed.
image

Another style of client program with JNDI lookup, a general style adopted before Spring existing.

4. Client Program: JNDICPMClient.java

Notice the difference between this JNDICPMClient.java and Client.java. In JNDIClient, we followed the old style of getting a data source object (you must have practiced in EJB client programs) using the lookup() method Context interface. This is hard coding and I name it as JNDI lookup. In Client.java, data source object is obtained. We described how the data source object is needed in XML file. Spring container injected the data source object into the client program. This is exactly the inverse of JNDI lookup. This is known as Inversion of Control (IoC). It is infact a design pattern which achieves loose coupling between components involved in the application. Dependency injection (DI) is the style used by Spring to achieve IoC. Even though, many say IoC and DI both are same, but they are very different.

In the earlier programs, we used connection pooling very specific to Weblogic. If Websphere or JBoss are used, the classes will change in the XML file. Now let us explore the ways of getting data source object independent of application servers so that the code becomes universal. Here comes to the rescue Apache and Spring. Apache gives BasicDataSource interface and Spring gives DataManagerDataSource interface. Let us write programs using them. Code changes in only in XML file.

Following applicationContext.xml (modification of earlier file) file gives all the three data sources created from Weblogic, Apache and Spring.

Weblogic is used in the earlier code and now let us use the other two with console screens (here, Weblogic server is not needed).

Following is the applicationContext.xml code (same Client.java is used. uncomment the other):

Following is the console screen when Apache BasicDataSource is used.

image

Following is the console screen when Spring container’s DataManagerBasicDataSource is used.

image

Leave a Reply

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