Deploy­ing a Weblogic Data­Source at appli­ca­tion Level

Deploy­ing an Ora­cle Weblogic Data Source at appli­ca­tion level requires that you pro­vide a file end­ing with –jdbc.xml suf­fix to your appli­ca­tion, con­tain­ing the Data Source def­i­n­i­tion. For exam­ple, if you are going to deploy your Data Source along with your EAR appli­ca­tion, 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

Writ­ing the Ora­cle Weblogic Data Source’s XML file

The XML file deployed along with your appli­ca­tion uses pretty much the same for­mat of the XML files which are cre­ated by Ora­cle WLS when you con­fig­ure a Data Source mod­ule (for exam­ple using the Con­sole). So, our sug­ges­tion is to use as tem­plate one JDBC mod­ule. (JDBC mod­ules are stored in the config/​jdbc sub­di­rec­tory of the domain folder). Just open the JDBC mod­ule file in an edi­tor and check the fol­low­ing elements:

  • name: change it to a name that is unique within the domain.
  • jndi-​name: change it accord­ing to your appli­ca­tion requirements.
  • scope: can be option­ally added to limit access to the data source just to the con­tain­ing appli­ca­tion. Can be achieved by adding in thejdbc-​data-​source-​params sec­tion the fol­low­ing ele­ment:<scope>Application</scope>

Here’s a sam­ple 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 mod­ule 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 declar­ing a ref­er­ence to your Data Source in all the mod­ules that are using it. Let’s see how to ref­er­ence it from a Web appli­ca­tion or an EJB module:

Using the Data Source in a Web application

If the appli­ca­tion level Data Source is going to be used by a Web mod­ule in your appli­ca­tion, you need to include a ref­er­ence 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 ref­er­ence 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 appli­ca­tion level Data Source in your EJB project, then you should ref­er­ence 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 appli­ca­tion, it will be enlisted along among the appli­ca­tion server mod­ules. For exam­ple, here’s the out­put from the admin­is­tra­tion con­sole of an Enter­prise appli­ca­tion which ships with a JDBC module:

oracle weblogic book oracle weblogic books