Freewind
Freewind

Reputation: 198388

How to create an web application running on websphere, and which will use the data source provided by websphere?

I'm setting up a web application on websphere, but it can't find the JNDI name of data source provided by webphere.

I'm new to websphere, and haven't used JDNI before, that I found it hard to work on this application directly.

How to create an simple web application which can run on websphere and which uses the data source provided by websphere. Or is there any existing demo I can try?

I think such an application will help me to locate the problem.

Upvotes: 1

Views: 5373

Answers (3)

user2015707
user2015707

Reputation:

If you use a Context lookup, you'll have to declare a new resource reference in the deployment descriptor (web.xml) :

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

But that's not all. You'll also have to bind this new reference to the server's JDBC resource (ibm-web-bnd.xml) :

<resource-ref name="jdbc/newReference" binding-name="jdbc/ressourceOnServer"/>

Note that this mechanism is quite similar to what is done on Glassfish with the sun-web.xml file, except that the binding is a little different :

<resource-ref>
    <res-ref-name>jdbc/newReference</res-ref-name>
    <jndi-name>jdbc/ressourceOnServer</jndi-name>
</resource-ref>

Look at this question : How do I connect to a Websphere Datasource with a given JNDI name?. If you need more details, I may give an example.

Upvotes: 4

Ravi Trivedi
Ravi Trivedi

Reputation: 2360

Open your Websphere admin console.

Click Resources >> Data Sources.

You will get the list of configured data sources. In the list there will be Default Datasource. Click that. In the configuration form, there is a field called JNDI name. That is the jndi name of the default datasource provided by Websphere.

You can use annotation injection to inject DataSource in your web component.

@Resource(name = "DefaultDatasource")
DataSource ds;

Tutorial is for tomcat but see servlet example and the use of datasource as annotation injection >> http://theopentutorials.com/tutorials/java-ee/servlet/servlet-datasource-resource-injection-in-tomcat/

Upvotes: 1

&#201;tienne Miret
&#201;tienne Miret

Reputation: 6660

When you setup a data source in websphere, you're being asked for the JNDI name you want. As far as I know, it is usually something like "jdbc/appname".

Then, if you use JPA, you just put the given name in your persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="appname" transaction-type="RESOURCE_LOCAL">

        <non-jta-data-source>jdbc/appname</non-jta-data-source>


        <!-- CLASS TO PERSIST -->

    </persistence-unit>
</persistence>

I now notice that I have the data source listed as non-jta in my application, although I believe WebSphere's data sources are JTA aware. You may want to check that.

Upvotes: 2

Related Questions