Wednesday, June 23, 2010

Accessing A DataSource In Jboss EWS

Share it Please

JBoss web server provides a single deployment platform for all light weight java integrates Apache Tomcat, Apache Web Server and all of the common connectors.It provides deployment support for java server pages and java servlet technologies ,Php and CGI.It uses a genuine high performance hybrid technology that incorporates the best of the most recent OS technologies for processing high volume data, while keeping all the reference Java specifications.
In this articles we will see how we can access a DataSource Configured in Jboss Web Server by uisng Jndi.
The Sample application that we create connects to Microsoft Access using ODBC . JBoss web provides a Context implementation for all the web applications running under it.there are some elements that are to be configured in WEB-INF/web.xml in order to refer the resources.
Resources referenced in web.xml can be defined in 2 ways
either as a global resource which can defined in CATLINE_HOME/conf/context.xml or
for a application specific which is defined in Application/META-INF/context.xml

in this articles we will define the Datasource which connects to ODBC and defined it in CATLINE_HOME/conf/context.xml.
lets see how we can configure a ODBC DataSource .
  1. Click Start, point to Control Panel, double-click Administrative Tools, and then double-click Data Sources(ODBC).
  2. Click the User DSN tab, and then click Add.
  3. Select "Microsoft Access Driver" and Finish
  4. in Datasource name "mySource" and select the database "student".[ student is also   available to download]
5.    in advanced options provide the username and password which will  be used when defining the resource

Once datasource is configured , we will create the resource in CATLINE_HOME/conf/context.xml

<Resource name="jdbc/StudentDB" auth="Container"
            type="javax.sql.DataSource" username="jagadesh" password="jagadesh"
            driverClassName="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:mySource"
            maxActive="8" maxIdle="4"/>

we just added a resource to the context.xml file which is now available to all applications and can be obtained by Jndi . lets see the Resource element

name=the name of the dataSource which is accessed from Jndi.
auth=Specify whether the web Application code signs on to the corresponding resource manager programatically, or whether the Container will sign on to the resource manager on behalf of the application. The value of this attribute must be
Application or Container.
type=the java class name which the application expects when it performs a lookup.
username=userrname to connect to datasource.
password=Password to connect to datasource.
driverClassName=Class name of the driver to connect.
url=the url to connect.
maxActive: Maximum number of dB connections in pool.
maxIdle: Maximum number of idle dB connections to retain in pool.

so now we have defined the Resource in the CATALINE_HOME/conf/context.xml file. we need to refer the resource in our web.xml file

so in web.xml we have ,

     <description>Data Source For MSAcess DataBase</description>

The resource-env-ref element contains a declaration of a Web application's reference to an administered object associated with a resource in the Web application's environment.

resource-env-ref-name - the resource reference name
resource-env-ref-type - the resource type

Now we have defined the resource and we also made a reference to the resource from our web we will see how can we access the resource from the application code .

here is a simple serlvet to access the resource ,

package com.myCompany;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

 * Servlet implementation class SamServlet
public class SamServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static {
        try {
        } catch(ClassNotFoundException e) {
            System.err.println("JdbcOdbc Bridge Driver not found!");
     * @see HttpServlet#HttpServlet()
    public SamServlet() {
        // TODO Auto-generated constructor stub

     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            System.out.println("in Get Method");
           Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource bean = (DataSource) envCtx.lookup("jdbc/StudentDB");
            Statement st=bean.getConnection().createStatement();
            ResultSet myresult=st.executeQuery("select * from Details");

            }catch(Exception e){

    /** Creates a Connection to a Access Database */
    public static Connection getAccessDBConnection() throws SQLException {
        Connection con=null;
        try {
        }catch(Exception  e){
        return con;

We have seen how we can configure a datasource in CATLINE_HOME/conf/context.xml file and refer it in the web.xml . we also seen how we can access the resource in application code.
Here are the Links to the Documents 
More Articles To Come , So Happy Coding....

No comments :

Post a Comment