Time for action – setting a JNDI/JDBC connection pool

We will now create a JDBC connection pool, which will be available via JNDI. Within our project, we will create an XML file and edit it to contain information about the previously mentioned pool. Finally, we will copy MySQL's JDBC driver into our project.

  1. We will head to the Project Explorer panel, which is located on the left of the UI, and navigate through prd5ch14 | WebContent | META-INF; once there, we will go to New | Other... and lastly select XML | XML File. We will then click on Next >, save the file with the name context.xml, and click on Finish to continue.
  2. Once the XML file is created, we will automatically be presented with an XML editor in the central part of the UI. We will click on the Source tab to edit our code as shown in the following screenshot:
    Time for action – setting a JNDI/JDBC connection pool
  3. We will now enter the following XML code into our file:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
      <Resource auth="Container" description="Sakila DB Connection" 
        name="jdbc/sakila" type="javax.sql.DataSource" 
        password="root" driverClassName="com.mysql.jdbc.Driver"
        maxIdle="2" maxWait="5000" validationQuery="select 1"
        username="root" 
        url="jdbc:mysql://localhost:3306/sakila" maxActive="4" />
      <WatchedResource>WEB-INF/web.xml</WatchedResource>
      <WatchedResource>META-INF/context.xml</WatchedResource>
    </Context>
  4. Then, we will save our changes by either clicking on the save (Time for action – setting a JNDI/JDBC connection pool) button or pressing Ctrl + S. The contents of this file define a resource of the type javax.sql.DataSource, which is a Java object implementing the JDBC connection pool. This resource can be accessed by its name jdbc/sakila by means of Java Naming and Directory Interface (JNDI) that provides an abstraction level; this allows us to modify connection parameters without having to modify the source code and recompile it. It is mandatory to complete the username and password fields with values that are appropriate for the MySQL instance being used.

    Note

    To obtain further information on JNDI, you can visit the Wikipedia page at http://en.wikipedia.org/wiki/Java_Naming_and_Directory_Interface.

  5. We should now copy MySQL's JDBC driver into our project. We will search for the file named mysql-connector-java-x.x.xx-bin.jar, which should be placed at [PRD_HOME]/lib/jdbc, and copy it (Ctrl + C). Then in Eclipse, we will select the node named prd5ch14 by going to WebContent | WEB-INF | lib and press Ctrl + V to paste it there.

    At this stage, the contents of the nodes META-INF and WEB-INF/lib should look similar to the following screenshot:

    Time for action – setting a JNDI/JDBC connection pool

Note

It is recommended to use connection pools on environments in which a medium or high concurrency is expected. Since they implement the efficient management of database connections, they are highly configurable and reduce the overhead produced every time a new connection is required.

What just happened?

We created the context.xml file within the META-INF node and edited it to contain the configuration for our MySQL connection pool, which is accessible via JNDI. We then copied MySQL's JDBC driver (mysql-connector-java-x.x.xx-bin.jar) into the WEB-INF/lib node.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset