JDBC Connection Pooling in jaspersoft

How JDBC Resources and Connection Pools Work Together in Jasper Report’s server

 

JasperReports Server can access any database using the JDBC (Java DataBase Connectivity) API. We can configure two types of data sources in the repository:

  • JDBC data source – Establishes a direct connection to the database server using its JDBC driver. JasperReports Server configures and manages the connections to the database. By default, the maximum number of simultaneous connections for each data source is five
  • JNDI data source – Calls the JNDI (Java Naming and Directory Interface) service of the application server to locate a database connection. The application server manages the connections to the database and shares them between all applications that it hosts. The configuration of the application server determines the number of connections that are shared.

 

At runtime, here’s what happens when Jasper Report’s server connects to a database:

 

  1. The Jasper Server gets the JDBC resource (data source) associated with the database by making a call through the JNDI API.

Given the resource’s JNDI name, the naming and directory service locates the JDBC resource. Each JDBC resource specifies a connection pool.

  1. Via the JDBC resource, the Jasper Server gets a database connection.

Behind the scenes, the server retrieves a physical connection from the connection pool that corresponds to the database. The pool defines connection attributes such as the database name (URL), user name, and password.

  1. Now that it is connected to the database, the server can read the database.

The server 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.

Server doesn’t have to validate every single time the server uses the connection, which improves performance. So when user runs a report, the server requests a connection from the pool. When the thread is finished using the connection, it returns it to the pool, so that it can be used by any other threads.

Once the report is rendered and user does not perform any action on report like passing parameter etc, then connection sits idle. JDBC Connection pooling enables the idle connection to be used by some other thread. When the user does not access the DB, the server automatically returns the connection to connection pool after certain time. Once it’s back in the pool, the connection is available.

About multiple contexts of the Spring MVC Framework

Spring MVC and Multiple Spring Contexts

 

Spring framework is one of the widely used frameworks in Enterprise Java World. Using Spirng MVC requires a proper understanding of ‘Contexts’ of the framework.

Spring has provision for defining multiple contexts in parent/child hierarchy. Spring manages beans that belong to different contexts. The org.springframework.context.ApplicationContext is consisered as the root context or parent context for all the other contexts.

From the official documentation:

The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.

Again there is one more context called WebApplicationContext in Spring MVC, which is a child context. The FrameworkServlet of the spring framework i.e. DispatcherServlet will have this web context.

Again from the official docs:

In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.

The ApplicationContext related beans are loaded using one of the two following ways.

In web.xml we can have configuration for the org.springframework.web.context.ContextLoaderListener class, which is a servlet context listener in the following way. This listener will get invoked when a web app gets deployed on the server.

ContextLoaderListener configuration:


<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/application-context.xml</param-value>
</context-param>

This listener looks for a context-param named contextConfigLocation in the web.xml. If it finds one, then the xml meta data file will be scanned for the beans that belong to the root application context. If it doesn’t find any, then the listener will look for a file named applicationContext.xml in the class path for the configuration meta data.

Similarly the DispatcherServlet, which is configured in the web.xml will look for an init-param named contextConfigLocation in the servlet definition as shown below.

DispatcherServlet configuration:


<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

If the init-param is not configured, then a file named dispatcher-servlet.xml will be searched for in the class path. Tha file name being searched for is made up of the servlet name – in our case ‘dispatcher’ – and a string ‘-servlet.xml’ as suffix.

Note:
The child context i.e. the MVC context will have access to the beans in the parent or root context. But not vice versa.

So, in both the cases i.e. the ContextLoaderListener or in case of DispatcherServlet if the parameters are not found in the web.xml and the corresponding files applicationContext.xml or YOUR SERVLET NAME-servlet.xml are not found in the class path then a FileNotFoundException will be thrown and the application won’t start up.

Usually the model classes and other beans that belong to the entire application are configured as beans in the applicationContext.xml. And the classes annotated with @Controller and the beans that are related to the MVC layer are configured in the DispatcherServlet configuration file.

Most of the tutorials on web have Spring MVC configuration with a single file included in both the parent context as well as in the child context because of lack of understanding of how Spring works. Due to this all the beans configured in the corresponding xml file will be instantiated twice including any of those beans defined for connection pooling, in which case connection pooling will also be a problem.

If we have only one file with spring beans configuration meta data, then we can configure only that xml file in DispatcherServlet‘s web context, which is the only mandatory context for an MVC web application, in which case we can exclude the configuration for the servlet context listener ContextLoaderListener in the web.xml.

So, whenever one considers using Spring MVC the understanding of these contexts is a must. Hope this article helped you and wish you happy coding.

–Rajasekhar
Java BI Developer
Helical IT Solutions