Setting up a datasource with WebSphere Liberty Profile 8.5
Asked Answered
T

3

9

My web app is getting a data source from JNDI with:

javax.naming.InitialContext ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) 
    ctx.lookup("java:comp/env/jdbc/db");

In the app's WEB-INF/web.xml, I have:

<resource-ref>
    <description>DataSource</description>
    <res-ref-name>jdbc/db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

In the app's WEB-INF/ibm-web-bnd.xml, I have:

<web-bnd
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
    version="1.0">
    <virtual-host name="default_host"/>
    <resource-ref name="jdbc/db" binding-name="jdbc/db"/>
</web-bnd>

In WebSphere Liberty Profile's server.xml, I have (keeping on the relevant parts):

<server description="new server">

    <featureManager>
        <feature>jsp-2.2</feature>
        <feature>jdbc-4.0</feature>
    </featureManager>

    <library id="oracle-lib">
        <fileset dir="lib" includes="ojdbc5_g.jar"/>
    </library>

    <dataSource jndiName="jdbc/db" jdbcDriverRef="oracle-driver" type="javax.sql.DataSource">
        <jdbcDriver libraryRef="oracle-lib" id="oracle-driver"/>
        <connectionManager numConnectionsPerThreadLocal="10" id="ConnectionManager" minPoolSize="1"/>
        <properties user="user" password="password"
                    url="jdbc:oracle:thin:@//db-server:1521/db"/>
    </dataSource>

</server>

When the app attempts to get the datasource from JNDI, it fails with the following error:

CWNEN0030E: The @Resource factory encountered a problem getting
the object instance jdbc/oracle binding object.  The exception message was: 
failed to resolve jdbc/oracle to javax.sql.DataSource: 
javax.naming.NameNotFoundException: 
Intermediate context does not exist: jdbc/oracle

What I am missing here?

Tasimeter answered 23/7, 2013 at 22:29 Comment(0)
T
12

WebSphere server.xml

<server>    
    <featureManager>
        <feature>jndi-1.0</feature>
        <feature>jdbc-4.1</feature>
    </featureManager>

    <httpEndpoint id="defaultHttpEndpoint"
                  host="localhost"
                  httpPort="9080"
                  httpsPort="9443" />

    <library id="oracle-lib">
        <fileset dir="lib" includes="ojdbc6_g.jar"/>
    </library>

    <dataSource jndiName="jdbc/oracle">
        <jdbcDriver libraryRef="oracle-lib"/>
        <properties.oracle user="orbeon" password="password"
                           url="jdbc:oracle:thin:@//localhost:1521/orbeon"/>
    </dataSource>

    <application name="orbeon" location="war/orbeon" type="war">
        <classloader commonLibraryRef="oracle-lib"/>
    </application>    
</server>

WEB-INF/ibm-web-bnd.xml

<web-bnd version="1.0"
        xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd">
    <virtual-host name="default_host"/>
    <resource-ref name="jdbc/oracle" binding-name="jdbc/oracle"/>
</web-bnd>

WEB-INF/web.xml

<resource-ref>
    <res-ref-name>jdbc/oracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Resource injection (instead of web.xml + ibm-web-bnd.xml):

@Resource(lookup = "jdbc/oracle")
DataSource ds;
Tasimeter answered 27/7, 2013 at 6:0 Comment(1)
I have Websphere Liberty Profile 8.5.5.9, and WEB-INF/ibm-web-bnd.xml isn't required by that. Also, for my app just including the ojdbc jar wasn't enough, it needed the orai18n jar as well. <library id="oracle-lib"><fileset dir="C:\jars\oracle11g" /></library> worked.Immunize
O
3

we use DB2 on Liberty 8.5.5 and we have in server.xml

<dataSource id="db2" isolationLevel="TRANSACTION_READ_COMMITTED" jndiName="jdbc/db2" type="javax.sql.DataSource">
    <jdbcDriver>
        <library>
            <fileset dir="/usr/lib/java/ibm-db2-universal-driver" includes="db2jcc4.jar, db2jcc_license_cisuz.jar, db2jcc_license_cu.jar"/>
        </library>
    </jdbcDriver>

    <properties.db2.jcc databaseName="DB2T" portNumber="21020" serverName="db2t.lvm.de"/>
    <containerAuthData password="{xor}KzspMC04" user="tdvorg"/>
</dataSource>

Maybe helps that.

Robert

Odele answered 25/7, 2013 at 6:59 Comment(3)
Yes, placing the <library> inside the <dataSource> as you did worked for me. However, I ended up with a ClassCastException` as the classes in the driver where loaded by 2 different classloader: the one in the application and the one for the driver. (I am sure that in lots of cases this is fine, as long as you don't directly use the driver classes.) So I reverted to a top-level library declaration and got it to work. I answered my own question with the setup I used, but this definitely helped!Tasimeter
You might wanna hide your user/pass for the db instance if it is not a public databaseMaymaya
Do we have GUI to do these changes as we have in WAS.Tap
C
0

In my case, the solution below works fine for me.

I created a "Generic project" in Eclipse called "Resources". Inside this project I created a file dataSource.xml with following content:

<server>
    <dataSource id="ccm" jndiName="jdbc/ccm" type="javax.sql.DataSource">
        <jdbcDriver id="oracle-driver" libraryRef="oracle-lib"/>
        <connectionManager id="ConnectionManager" minPoolSize="1" numConnectionsPerThreadLocal="10"/>
        <properties.oracle password="password" url="jdbc:oracle:thin:@128.1.30.150:1521:ccmdes" user="ccm"/>
    </dataSource>

   <library id="oracle-lib">
        <fileset dir="C:\Oracle\product\10.1.0\Client_1\jdbc\lib" includes="ojdbc14_g.jar"/>
    </library>

    <jdbcDriver id="oracle" libraryRef="oracle-lib"/>

</server>

And I drag and drop this file for server configuration in Eclipse or create the line in server.xml:

<include location="${shared.config.dir}/dataSource.xml"/>

The file server.xml was like this:

<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>jsp-2.3</feature>
        <feature>adminCenter-1.0</feature>
        <feature>jdbc-4.1</feature>
        <feature>jndi-1.0</feature>
        <feature>servlet-3.1</feature>
    </featureManager>

    <httpEndpoint host="localhost" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>

    <applicationMonitor updateTrigger="mbean"/>                 

<!-- Define your admin user name and password -->
    <quickStartSecurity userName="admin" userPassword="password"/>

<!-- Define a keystore for the HTTPS port -->
    <keyStore id="defaultKeyStore" password="Liberty"/> 

<!-- Allows remote file access for config changes -->
<remoteFileAccess>
  <writeDir>${server.config.dir}</writeDir>
</remoteFileAccess> 

    <!-- <applicationMonitor updateTrigger="mbean"/> -->
    <webApplication id="TestPage" location="TestPage.war" name="TestPage"/>
    <webApplication id="MonitoriaAtendimento" location="MonitoriaAtendimento.war" name="MonitoriaAtendimento"/>

    <include location="${shared.config.dir}/dataSource.xml"/>
</server>
Chromatogram answered 26/10, 2015 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.