Oct 22, 2012

Configuring and retrieving a DataSource object

In the JDBC API, a DataSource object is a means to access a database. A DataSource object is identified by set of properties which is used to communicate with the server. These properties include information such as the location of the database server, the name of the database, the protocol used to communicate with the server, the username and password of the privileged database user.
In the application server, a data source is known as JDBC Resource

The rest of the post demonstrates how to configure a JDBC Resource in GlassFish application server and retrieve the same data source using JNDI through a simple web servlet

Setting up a JDBC Resource for MySQL Server in GlassFish application Server

1. Download and copy the jar file containing the JDBC driver for MySQL database server in the lib directory of the GlassFish application domain.Restart the GlassFish domain by executing asadmin restart-domain

2. Login to the Admin Console by hitting the url http://localhost:4848


3. Goto Resources > JDBC > JDBC Connection Pools. Click New.... Fill a pool name, select resource type as javax.sql.DataSource and MySQL as the database driver vendor. Click Next

4. Specify the properties User, Password, DatabaseName and the Url property for the successful JDBC connection in the Step 2 of creating the JDBC connection. On clicking Finish the new connection pool is added to the list of pools. Select the connection pool just created and click on Ping

5. Goto Resources > JDBC > JDBC Resources. Click New.... Add a JDBC Resource name. This name will be used to look up the DataSource object.

package com.j2eepractice.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.appserv.jdbc.DataSource;

/** 
 * File: DataSourceServlet.java
 *
 * The DataSourceServlet tries to retrieve the JDBC resource configured above
 * and retrieve the connection object using the data source object
 */

@WebServlet("/DataSourceServlet")
public class DataSourceServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private Connection connection = null;

  public DataSourceServlet() {
    super();
  }

  public void initConnection() throws ServletException {
    try {
      Context ctx = new InitialContext();
      DataSource ds = (DataSource) ctx.lookup("jdbc/Application");
      connection = ds.getConnection();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    initConnection();

    String msg =
        connection != null
            ? "Datasource initialized successfully"
            : "Datasource cannot be initialized";

    response.setContentType("text/html");
    PrintWriter printWriter = response.getWriter();
    printWriter.println("

"); printWriter.println(msg); printWriter.println("

"); } }


Further Reading:
DataSource Objects And Connection Pools

No comments :

Post a Comment