Hi,

Jay SenSharma

Jay SenSharma

Here we are going to see how we can use the DataSources inside our EJB3.0 Application. Basically there are two Options to get the DataSource object inside the EJB3 Beans.

Option-1). Using Traditional Way we can get the dataSource Object by Looking up in the JNDI tree of the Server to get teh dataSource object.
Option-2). Using the resource Injection techniqueue we can get the DataSource object in two lines of codes…(it reduces the code length as well)
In the below demonstration we will use two fidderent methods to get the DataSource objects in above two ways using two different Business methods.

Step1). Please refer to the following Link to Create a DataSource through the Admin Console…http://middlewaremagic.com/weblogic/?p=2050

Step2). Now Create a Directory somewhere in your file system like “C:Resource_Injection” this directory is going to become our EAR Application.

Step3). Now Create another Directotry “MyEJB” inside “C:Resource_Injection”.

Step4). Write the EJB Interface “CalculatorRemote.java” inside “C:Resource_InjectionMyEJB” like following:

package calculator;
import javax.ejb.*;
import javax.sql.DataSource;
@Remote
public interface CalculatorRemote
{
public String getDataSourceUsingTraditionalWay(String query);
public String getDataSourceUsingResourceInjection(String query);
}

Step4a). Write the EJB Stateless Session Bean class “CalculatorBean.java” inside “C:Resource_InjectionMyEJB” like following:

package calculator;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import javax.annotation.*;

@Stateless(mappedName="calc")
public class CalculatorBean implements CalculatorRemote
{
@Resource(mappedName="TestDSJndi")
DataSource injectedDataSource;
public String getDataSourceUsingResourceInjection(String query)
{
System.out.println("inside betterWayToGetDataSourceUsingResourceInjection() method ");
System.out.println("betterWayToGetDataSourceUsingResourceInjection() got DS = "+injectedDataSource);
String result=this.executeSQL(query,injectedDataSource);
return result;
}

public String getDataSourceUsingTraditionalWay(String query)
{
DataSource traditionalDS=null;
try{
InitialContext ic=new InitialContext();
traditionalDS=(DataSource)ic.lookup("TestDSJndi");
System.out.println("nt Inside getDataSourceUsingTraditionalWay.....Got DataSource = "+traditionalDS);
}
catch(Exception e)
{
System.out.println("nt Unable to get DataSource using getDataSourceUsingTraditionalWay: "+e);
}
String result= this.executeSQL(query,traditionalDS);
return result;
}

public String executeSQL(String query,DataSource ds)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String resultString="";
try{
con=ds.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(query);
int tableCount=1;
System.out.println("nt Following Tables Are Available in Systables of Pointbase..");
while(rs.next())
{
String tableName=rs.getString(2);
System.out.println("t Table-"+tableCount+":t"+tableName);
tableCount++;
resultString=resultString+"<BR>"+tableName;
}
}
catch(Exception e)
{
System.out.println("nt Exception Occured: "+e);
}
finally
{
try { if(rs!=null) rs.close(); }
catch (Exception rse) {}
try { if(stmt!=null) stmt.close(); }
catch (Exception sse) {}
try { if(con!=null) con.close(); }
catch (Exception cse) {}
}
return "Result Of Query: "+query+" is Following: "+resultString;
}
}

Step5). Open a command prompt then run “setWLSEnv.cmd” here and then compile the EJBs like following:

Compiling EJB  Classes

Compiling EJB Classes

Step6). Now Create a Directory “MyWebApp” inside “C:Resource_Injection”

Step7). Now Create “WEB-INF” Directory and provide the following “web.xml” file inside “C:Resource_InjectionMyWebAppWEB-INF”

<?xml version='1.0' encoding='UTF-8'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</web-app>


NOTE: Make Sure that you are using the Latest “web.xml” Schema because the Resource Injection and EJB Injection is supported from Servlet Specification 2.5…So make sure that u are not using any “web.xml” file which is using DTD declarations.

Step8). Provide the following “index.jsp” file inside “C:Resource_InjectionMyWebApp” directory.

<%@ page import="javax.sql.*,calculator.*, javax.naming.*" %>
<%
InitialContext ic=new InitialContext();
CalculatorRemote remote=(CalculatorRemote)ic.lookup("calc#calculator.CalculatorRemote");
String result=remote.getDataSourceUsingTraditionalWay("SELECT * FROM SYSTABLES");
System.out.println("nt Result TRADITIONAL WAY: "+result);
out.println("<h2>Result TRADITIONAL WAY</h2><BR> "+result);

System.out.println("<h2>Standard Way of Getting DataSource Using Resource Injection BELOW</h2>");
String resultInjected=remote.getDataSourceUsingResourceInjection("SELECT * FROM SYSTABLES");
System.out.println("nt Result RECOMMENDED INJECTED WAY: "+resultInjected);
out.println("<h2>Result RECOMMENDED INJECTED WAY</h2><BR> "+resultInjected);
%>

Step9). Create a “META-INF” directory inside “C:Resource_Injection” to provide the EAR deployment descriptors inside it. Create a file “application.xml” inside “C:Resource_InjectionMETA-INF” like following:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4">
<module>
<ejb>MyEJB</ejb>
</module>
<module>
<web>
<web-uri>MyWebApp</web-uri>
<context-root>MyWebApp</context-root>
</web>
</module>
</application>

Step10). Your Application is ready for deployment…Open Admin Console and then Deploy the “Resource_Injection” there….this is your EAR application.

Step11). Hit the Application like : http://localhost:7001/MyWebApp/index.jsp

.
.
Thanks
Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.