1
votes

I am using WebSphere Liberty 17.0.0.2.

The end product is an ear that contains a jar which is using JPA to access database.

The EntityManager is annotated with @PersistenceContext with persistence unit name defined. In the persistence.xml under appropriate persistence-unit the jta-data-source tag contains datasource name (direct lookup) specified in server.xml. With this setup everything is working fine.

Now need arose that I need to switch from direct lookup to indirect lookup regarding datasource JNDI lookup method. As I understand indirect lookup is something like OS environment variables. I use a name to get the configured value, so I can switch datasources without touching my code to rename JDNI names there.

Switching from direct to indirect I'd need to append 'java:comp/env' in my persistence.xml for the jta-data-source.

How can I connect datasource name with indirect lookup name? I tried to specify it in server.xml using resource-ref tag but with no luck.

The main goal here is to using indirect lookup in the code but be able to change datasources in the applicaton server configuration, so I don't have to change my application when this is happning.

Configuration snippets:

server.xml
<library id="oraclelib">
<jdbcDriver id="oracledriver" libraryRef="oraclelib">
<dataSource jndiName="jdbc/oradb" jdbcDriverRef="oracledriver" id="oradbds">
<resource-ref name="jdbc/oradb" binding-name="jdbc/mydb">

persistence.xml
<jta-data-source>java:comp/env/jdbc/mydb</jta-data-source>

When running this setup a javax.naming.NameNotFoundException is thrown.

Update #1

server.xml after swapping name, binding-name

<?xml version="1.0" encoding="UTF-8"?>
<server description="app server">
  <library id="OracleLib">
    <fileset dir="/oracle" includes="ojdbc6.jar" />
  </library>
  <jdbcDriver id="OracleJDBCDriver" libraryRef="OracleLib" />
  <dataSource jndiName="jdbc/oradb" jdbcDriverRef="OracleJDBCDriver" id="dbDataSource">
    <properties.oracle URL="jdbc:oracle:thin:@//dbhost:port/SID" user="dbuser" password="dbpassword" />
  </dataSource>
  <application id="Myapp_ear" location="/path/myapp.ear" name="Myapp_ear" type="ear">
    <application-bnd>
      <resource-ref name="jdbc/mydb" binding-name="jdbc/oradb" />
    </application-bnd>
  </application>
</server>

jta-data-source is java:comp/env/jdbc/mydb

Solution

It turned out that the bean that was used to get EntityManager was a CDI bean. As it is modified to be an EJB bean, the ejb-jar.xml, ibm-ejb-jar-bnd.xml did the trick.

1
did you define the <resource-ref/> as a top level element in server.xml? It is only effective if it is nested under <application><application-bnd><resource-ref> for your application, or you can put it directly into the web.xml of your applicationAndy Guibert
Yes, resource-ref was a top level element. I modified as you suggested, but unfortunately it didn't help. Now it is: /server/application/application-bnd/resource-ref. I tried both java:comp/env/[...] and java:app/env/[...].bdz

1 Answers

1
votes

It should be the other way around:

not

 <resource-ref name="jdbc/oradb" binding-name="jdbc/mydb">

but

<resource-ref name="jdbc/mydb" binding-name="jdbc/oradb">

name - is resource ref name, and binding-name is jndi name in the server configuration.