blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Using JNDI lookup for DataSource using resource-ref (websphere)

In admin console,
Resources -> JDBC -> JDBC providers



Note that the ojdbc6.jar jar is searched relative to the path location specified in variable ORACLE_JDBC_DRIVER_PATH.

Click finish.

Check if ORACLE_JDBC_DRIVER_PATH is set correctly.

Create Datasource

In admin console browse to,
Resources -> JDBC -> Datasources.



Frame the jdbc connection url based on the type of driver you use.
For Oracle java thin driver,
jdbc:oracle:thin:@[host]:[port]:[sid]


If you already have configured authentication details select it here. Else we will do it later.

Click finish and save.

Select the Datasource -> JAAS-J2C authentication data


Once saved select these in data source configuration page.


Check the logs to verify if binding is successful.
WSVR0049I: Binding Oracle datasource as jdbc/xe

Also test the connection from the admin console to verify if the datasource is able to communicate with DB.

Add Configuration in your application

In web.xml

<resource-ref>
    <description />
    <res-ref-name>ref/jdbc/xe</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

In ibm-web-bnd.xml

<resource-ref name="ref/jdbc/xe" binding-name="jdbc/xe" />

In servlet code,
Context initCxt = new InitialContext();
Context envContext = (Context) initCxt.lookup("java:comp/env");
DataSource dataSource = (DataSource) envContext.lookup("ref/jdbc/xe");
Connection conn =dataSource.getConnection();
Statement stmt = conn.createStatement();
//this query will work on all as it's an internal table
ResultSet rs = stmt.executeQuery("select count(*) from help"); 
String totalentries = null;
while(rs.next()){
    totalentries = rs.getString(1);
}
System.out.println(totalentries);

Notice the logs for codes starting in DSRA82.

Output:
SystemOut O 980

Continue reading: Writing a custom JNDI provider

No comments:

Post a Comment