Connecting to a datasource from Websphere

These are the steps that I had to do to connect to a database datasource from my Websphere application:

1) This is Java code:
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx
.lookup("jdbc/sqlServerDS");
Connection connection = ds.getConnection();

2) My web.xml has this entry:

 

 

 

 

 

 

<

resource-ref id=“ResourceRef_1231963089452”> 

 

<description></description> 

 

<res-ref-name>sqlServerDS</res-ref-name> 

 

<res-type>javax.sql.DataSource</res-type> 

 

<res-auth>Container</res-auth> 

 

<res-sharing-scope>Shareable</res-sharing-scope> 

 

</resource-ref>

3) Make sure that your ibm-web-bnd.xmi file has this entry that contains jndiName:

 

 

<

resRefBindings xmi:id=“ResourceRefBinding_1231963089452” jndiName=“jdbc/sqlServerDS”> 

 

<bindingResourceRef href=“WEB-INF/web.xml#ResourceRef_1231963089452”/> 

 

</resRefBindings>

NOTE: You can set resource bindings by opening web.xml in your iRAD, and going to References tab. You can Add a new reference with the details from above.

4) On your local server, you have to setup your datasource’s JNDI name as: jdbc/sqlserverDS

The most common errors are:
javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context “java:”

Solution: do not use “java:comp/env/jdbc/sqlServerDS, use only jdbc/sqlServerDS)

Advertisements
This entry was published on January 15, 2009 at 2:02 pm and is filed under Database, Java/J2EE. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: