0

I'm deploying a working Tomcat webapp on websphere-liberty on docker. The webapp connects to a postgres data source on docker. In websphere when I try to get connection with

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/postgres");
Connection conn = ds.getConnection();

my web.xml is setted as:

<resource-ref>
        <description>postgreSQL Connection</description>
        <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.XADataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

I get the following error

javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/postgres JNDI name could not be instantiated.
If the reference name maps to a JNDI name in the deployment descriptor bindings for the application
performing the JNDI lookup, make sure that the JNDI name mapping in the deployment descriptor binding is correct.
If the JNDI name mapping is correct, make sure the target resource
can be resolved with the specified name relative to the default initial context.
[Root exception is com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The
server was unable to obtain an object instance for the java:comp/env/jdbc/postgres reference.
The exception message was: CWNEN1004E: The server was unable to find the jdbc/postgres default binding with the javax.sql.XADataSource type for
the java:comp/env/jdbc/postgres reference.]

I exlude that is a problem with docker network. What I have setted wrong? Is something in my websphere-liberty?

server.xml is

<server description="Default server">

  <!-- Enable features -->
 <featureManager>
  <feature>webProfile-7.0</feature>
  <feature>adminCenter-1.0</feature>
  <feature>jdbc-4.0</feature>
</featureManager>  
  <quickStartSecurity userName="admin" userPassword="password"/>
<!-- Define the host name for use by the collective.
    If the host name needs to be changed, the server should be
    removed from the collective and re-joined. -->
<!--  <variable name="defaultHostName" value="localhost" /> -->

<!-- Define an Administrator and non-Administrator -->
<basicRegistry id="basic">
  <user name="admin" password="admin" />
  <user name="nonadmin" password="nonadminpwd" />
</basicRegistry>

<!-- Assign 'admin' to Administrator -->
<administrator-role>
  <user>admin</user>
</administrator-role>

<!--  <keyStore id="defaultKeyStore" password="Liberty" /> -->

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

<remoteFileAccess>
  <writeDir>${server.config.dir}</writeDir>
</remoteFileAccess> 
<library id="postgres-lib">
  <fileset dir="/arturial/project/" includes="postgresql-9.4.1208.jre6.jar"/>
</library>
<dataSource id="jdbc-Prima_WA_db" jndiName="jdbc/postgres" type="javax.sql.DataSource">
  <jdbcDriver libraryRef="postgres-lib"/>
  <connectionManager numConnectionsPerThreadLocal="10" id="connectionManager" minPoolSize="1"/>
 <!--   <properties.oracle user="postgres" password="postgres" -
               url="jdbc:postgres://172.17.0.3:5432/Prima_WA_db"/> -->
 </dataSource>
<!--
<applicationManager updateTrigger="disabled"/>
<application id="primawebapp" name="primawebapp" location="war/primawebapp" type="war">
  <classLoader delegation="parentLast" commonLibraryRef="postgres-lib"/>
 </application>
-->
</server>
grandeale83
  • 141
  • 2
  • 14
  • Looks like your datasource might be incorrectly configured in server.xml. Please add relevant part or full server.xml to the question. – Gas Jul 20 '16 at 12:21
  • 1
    Try to add to dataSource `type="javax.sql.XADataSource"` and to `jdbcDriver` `javax.sql.XADataSource="org.postgresql.xa.PGXADataSource"` – Gas Jul 20 '16 at 15:51
  • ah yes, I believe that's the solution @Gas -- you should post that as an answer – Andy Guibert Jul 21 '16 at 15:12
  • @GAS -- I tried to add in server.xml to dataScource and to jdbcDriver what GAS said, but the error is the same. WebSphere: "javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/postgres JNDI name could not be instantiated..." I really think the server cannot obtain an object of the type I request, as it doesn't find the package or the class in the package. Could yoy I've not setted the classpath? How I can check this? – grandeale83 Jul 25 '16 at 08:52
  • Are you sure that this `fileset dir="/arturial/project/` path is correct in your docker container? Did you try to run Liberty without docker? – Gas Jul 25 '16 at 10:12
  • no, i'm working only on docker. The path is correct, it is the shared directory betwheen my machin and the docker container as a '-v' volume. For test I tried to put the jar file in the /lib directory, and passed in server.xml in to tags. The same situation I get tring with DB2. Exactly the saem exceptions. – grandeale83 Jul 25 '16 at 11:02
  • Ok, one more thing, in your resource-ref in web.xml, change res-type to plain Datasource `javax.sql.DataSource` – Gas Jul 25 '16 at 12:14
  • And either remove webProfile-7.0 (because it contains jdbc-4.1, and configure required features) or jdbc-4.0 feature, as there might be some clash between these two. – Gas Jul 25 '16 at 12:21
  • It doesn't solve the problem, also because jdbc-4.1 imported with with webProfile-7.0 goes to substitute the jdbc-4.0 as it's neweer. but if import webprofile-6.0 (that dosn't contian jdbs) and add the feature of jdbc-4.0 module in server.xml the error is exactly the same. – grandeale83 Jul 26 '16 at 13:18

3 Answers3

1

Try this;

DataSource ds = (DataSource)ctx.lookup("jdbc/postgres");

Also, a different way.

sozkul
  • 665
  • 4
  • 10
1

Your <datasource> must have properties configured under it, otherwise there is no way for Liberty to know how to connect to your database.

For postgresql, try the following configuration:

<dataSource id="jdbc-Prima_WA_db" jndiName="jdbc/postgres">
  <jdbcDriver libraryRef="postgres-lib"/>
  <properties serverName="172.17.0.3" portNumber="5432" databaseName="Prima_WA_db"  
              user="postgres" password="postgres"/>
</dataSource>

Of course, using the proper values that correspond to the postgresql instance that you are running on.

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • @arguibert your definition seems to be correctly, but I still get NamingException: object referenced by java:comp/env/jdbc/postgres JNDI name could not be instatiated – grandeale83 Jul 20 '16 at 14:24
  • lets eliminate the resource-ref from the equation. Are you able to do a direct lookup of the datasource? For example: `new InitialContext().lookup("jdbc/postgres");` – Andy Guibert Jul 20 '16 at 14:26
  • nevermind, I see in a comment to another question that you already tried the direct lookup. So the issue is with how you are defining your datasource in the server.xml. Check your server logs for any errors or warnings indicating a failure instantiating the datasource. – Andy Guibert Jul 20 '16 at 14:29
  • Where I can find server log? It is the message.log that I find in /logs ? – grandeale83 Jul 20 '16 at 14:31
  • logs would be in the `${server.config.dir}/logs` directory – Andy Guibert Jul 20 '16 at 14:43
0

Looks like your datasource definition might be incomplete, try updating it with the following:

<dataSource type="javax.sql.XADataSource" ...
    <jdbcDriver javax.sql.XADataSource="org.postgresql.xa.PGXADataSource" ...
Gas
  • 17,601
  • 4
  • 46
  • 93