Posted on

Deploying a Weblogic DataSource at application Level

oracle weblogic book oracle weblogic books

Deploying an Oracle Weblogic Data Source at application level requires that you provide a file ending with –jdbc.xml suffix to your application, containing the Data Source definition. For example, if you are going to deploy your Data Source along with your EAR application, you will need to include the file newmodule-jdbc.xml file at the root of the EAR file. Here’s an Eclipse view of your application.

 

oracle weblogic book oracle weblogic books

Writing the Oracle Weblogic Data Source’s XML file

The XML file deployed along with your application uses pretty much the same format of the XML files which are created by Oracle WLS when you configure a Data Source module (for example using the Console). So, our suggestion is to use as template one JDBC module. (JDBC modules are stored in the config/jdbc subdirectory of the domain folder). Just open the JDBC module file in an editor and check the following elements:

  • name: change it to a name that is unique within the domain.
  • jndi-name: change it according to your application requirements.
  • scope: can be optionally added to limit access to the data source just to the containing application. Can be achieved by adding in thejdbc-data-source-params section the following element:<scope>Application</scope>

Here’s a sample newmodule-jdbc.xml:

<wls:jdbc-data-source >       <wls:name>JDBCOracleModule</wls:name>      <wls:jdbc-driver-params>            <wls:url>jdbc:oracle:thin:@localhost:1521:XE</wls:url>          <wls:driver-name>oracle.jdbc.xa.client.OracleXADataSource</wls:driver-name>          <wls:properties>              <wls:property>                  <wls:name>user</wls:name>                  <wls:value>scott</wls:value>              </wls:property>              <wls:property>                  <wls:name>password</wls:name>                  <wls:value>tiger</wls:value>              </wls:property>          </wls:properties>      </wls:jdbc-driver-params>      <wls:jdbc-data-source-params>          <wls:jndi-name>jdbc/OracleModule</wls:jndi-name>          <wls:global-transactions-protocol>TwoPhaseCommit</wls:global-transactions-protocol>      </wls:jdbc-data-source-params>  </wls:jdbc-data-source>

Next, declare the JDBC module into your EAR’s weblogic-application.xml:

<wls:module>          <wls:name>JDBCOracleModule</wls:name>          <wls:type>JDBC</wls:type>          <wls:path>newmodule-jdbc.xml</wls:path>  </wls:module>

Now the last effort would be declaring a reference to your Data Source in all the modules that are using it. Let’s see how to reference it from a Web application or an EJB module:

Using the Data Source in a Web application

If the application level Data Source is going to be used by a Web module in your application, you need to include a reference to it into your web.xml file:

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

You need to include as well a reference to it into weblogic.xml descriptor

<wls:resource-description>          <wls:res-ref-name>JDBCOracleModule</wls:res-ref-name>          <wls:jndi-name>jdbc/OracleModule</wls:jndi-name>  </wls:resource-description>

Using the Data Source in an EJB application

If, on the other hand, you are going to use the application level Data Source in your EJB project, then you should reference it in your ejb-jar.xml and weblogic-ejb-jar.xml files as follows:

<ejb-jar>    <enterprise-beans>       <session>          <ejb-name>ExampleEJB</ejb-name>          <ejb-class>com.sample.ExampleEJB</ejb-class>          <session-type>Stateless</session-type>          <resource-ref>             <res-ref-name>jdbc/JDBCModule</res-ref-name>             <res-type>javax.sql.DataSource</res-type>             <res-auth>Container</res-auth>          </resource-ref>       </session>    </enterprise-beans> </ejb-jar>

And this is your weblogic-ejb-jar.xml:

<wls:weblogic-ejb-jar >      <wls:weblogic-enterprise-bean>          <wls:ejb-name> ExampleSFSB </wls:ejb-name>          <wls:resource-description>            <wls:res-ref-name>JDBCOracleModule</wls:res-ref-name>            <wls:jndi-name>jdbc/OracleModule</wls:jndi-name>          </wls:resource-description>      </wls:weblogic-enterprise-bean>   </wls:weblogic-ejb-jar>

Once deployed the Data Source along with your application, it will be enlisted along among the application server modules. For example, here’s the output from the administration console of an Enterprise application which ships with a JDBC module:

oracle weblogic book oracle weblogic books