Reputation: 10557
I am using jdk 11 with Eclipse 2021 and WAS Liberty 21.0.0.4. I also have OpenLiberty 21.0.0.4 and the same issue happens there.
I have ConnectionFactory code that looks like this
public class ConnectionFactory {
private static DataSource ds = null;
...
public static Connection getDb2Connection() {
Connection conn = null;
try {
Context initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("jdbc/db2DataSource"); //NamingException is thrown here
conn = ds.getConnection();
} catch (NamingException ex) {
System.out.println(ex);
}
return conn;
}
}
The exception I get is
javax.naming.NamingException: CWWKN0008E: An object could not be obtained for name jdbc/db2DataSource
I provided below my solution which is to not use Liberty variable server.config.dir
but rather hardcode the path as explained in my answer below. This is not perfect solution and I hope someone will be able to explain why resolving Liberty variable by use of ${server.config.dir}
does not work.
Upvotes: 0
Views: 1053
Reputation: 10557
I created this question in order to hopefully get an answer but I found solution which is not what I hoped but I have no better.
After spending quite some time, I found that liberty server provided varialbe server.config.dir
(see https://www.ibm.com/docs/en/was-liberty/core?topic=liberty-directory-locations-properties) is the cause of this error.
Following is how my Liberty server.xml is set.
I dont put hardcoded values in server.xml but I put them in server.env file like so: The above variables are set hardcoded inside server.env like:
port=xxx
server=xxx
database=xxx
path=xxx
schema=xxx
user=xxx
password=xxx
type=4
, and then in my server.xml, I reference these variables using ${var-name} notation like here to set my JNDI name for data source:
<dataSource jndiName="jdbc/db2DataSource">
<jdbcDriver libraryRef="DB2Library" />
<connectionManager maxPoolSize="1" minPoolSize="1" />
<properties.db2.jcc
serverName="${server}"
portNumber="${port}"
currentFunctionPath="${path}"
currentSchema="${schema}"
databaseName="${database}"
user="${user}"
password="${password}"
driverType="${type}"" />
</dataSource>
This is all fine, however in the above dataSource, I have libraryRef="DB2Library"
and my DB2Library is then set as:
<library id="DB2Library">
<fileset dir="C:\Users\ME\dev\wlp-javaee8-21.0.0.4\wlp\usr\servers\MyServer\resources\drivers\db2" includes="*.jar"/>
<!-- <fileset dir="${server.config.dir}/resources/drivers/db2" includes="*.jar"/> -->
<!-- <fileset dir="${server.config.dir}\resources\drivers\db2" includes="*.jar"/> -->
</library>
Notice the 2 commented out lines that use ${server.config.dir}
. They are the same except one uses forward slash and the other backward slash. Both of them will cause this NamingException.
The line with hardcoded path will work fine.
So, it seems like the Liberty resolution of variable ${server.config.dir}
is not working properly.
Upvotes: 0
Reputation: 3484
In your answer, you posted that the following works,
<fileset dir="C:\Users\ME\dev\wlp-javaee8-21.0.0.4\wlp\usr\servers\MyServer\drivers\db2" includes="*.jar"/>
But this does not work,
<fileset dir="${server.config.dir}/resources/drivers/db2" includes="*.jar"/>
This suggests that you do not have a resources
folder in the server.config.dir
, and should just be doing,
<fileset dir="${server.config.dir}/drivers/db2" includes="*.jar"/>
Upvotes: 2