简体   繁体   中英

Liberty Server: Data source configuration

I'm trying to configure Datasource on Liberty Server 20.0.0.9. I'm referring to the following instructions: https://www.ibm.com/docs/en/was-liberty/base?topic=liberty-configuring-default-data-source

This is my server.xml

Features added:

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

Library configuration

<library description="My shared library" id="MyLib" name="MyLib">
        <fileset dir="${usr.extension.dir}/lib" id="MyLibTAI" includes="MyLibTAI-1.0.4.jar db2jcc-db2jcc4.jar"/>
    </library>

Datasource

<dataSource id="defaultDS" jndiName="jdbc/defautlDS">
        <jdbcDriver libraryRef="MyLib"/>
        <properties.db2.jcc databaseName="DBXV01" serverName="192.33.112.21" portNumber="70080"/>
        <containerAuthData user="myuser" password="mypwd"></containerAuthData>
    </dataSource>

In my code I'm trying to refer to Datasource in different ways:

@Resource(name="defaultsDS") // I also tried name="jdbc/defaultDS"

or

ds = (DataSource) new InitialContext().lookup("java:comp/picoDS"); // Name not found exception

What I'm missing? Any help appreciated Thank you

If you are doing a lookup of the jndiName configured in server.xml (as suggested in the other answer), you need to be aware that it is not using container managed authentication and thus the containerAuthData that you specified in server.xml for the dataSource is not being used. You were on the right track the first time in using a resource reference, and here is how to do that correctly:

@Resource(lookup = "jdbc/defautlDS")
DataSource datasource;

Similar, you can use the Resource annotation to define a name for the resource reference that can be looked up in java:comp,

@Resource(lookup = "jdbc/defautlDS", name = "java:comp/env/picoDS")
DataSource datasource;

after which you can successfully do:

DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/picoDS");

I added the /env to the resource reference name in the above to better follow standards.

In both of the above, I used defautlDS to match the unusual spelling of the jndiName value from your example, which might itself be a typo. Make sure it is consistent in both places in order for it to work.

By using the resource reference, you are able to get container managed authentication (container managed is the default for resource reference lookups) and so you end up using the user and password from conatinerAuthData of your config. If not using the resource reference, then you get application managed authentication.

I solved, as suggested by Gas in comments, using

ds = (DataSource) new InitialContext().lookup("jdbc/defaultDS");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM