3

I have a web app running in Tomcat correctly that I want to run on the new OpenLiberty server, the app is starting correctly inside OpenLiberty but at the moment of the database connection initiation is throwing the following exception:

[Default Executor-thread-15] 2018-03-15 15:02:30 ERROR TomcatConnectionManager:41 - Loading jdbc/mysql/myaap failure
javax.naming.NameNotFoundException: java:/comp/env
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLName.<init>(JavaURLName.java:83)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLNameParser.parse(JavaURLNameParser.java:39)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLNameParser.parse(JavaURLNameParser.java:60)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext$NameUtil.<init>(JavaURLContext.java:474)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:321)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:370)
    at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)

The above exception is thrown during the lookup phase:

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");

Is there any way to make it work on OpenLiberty doing less changes possible?

aleroot
  • 71,077
  • 30
  • 176
  • 213

2 Answers2

4

On OpenLiberty the equivalent lookup would look like this:

    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:comp/env");

The key is that you need to use java:comp/... instead of java:/comp/...


The reason why Tomcat is different than Liberty is because Tomcat is just a servlet container and Liberty conforms to the full Java EE specification.

According to section EE.5.2.2 of the Java EE 7 spec:

The application component’s naming environment is composed of four logical namespaces, representing naming environments with different scopes. The four namespaces are:

  • java:comp – Names in this namespace are per-component (for example, per enterprise bean). Except for components in a web module, each component gets its own java:comp namespace, not shared with any other component. Components in a web module do not have their own private component namespace. See note below.
  • java:module – Names in this namespace are shared by all components in a module (for example, all enterprise beans in a single EJB module, or all components in a web module).
  • java:app – Names in this namespace are shared by all components in all modules in a single application, where “single application” means a single deployment unit, such as a single ear file, a single module deployed standalone, etc. For example, a war file and an EJB jar file in the same ear file would both have access to resources in the java:app namespace.
  • java:global – Names in this namespace are shared by all applications deployed in an application server instance. Note that an application server instance may represent a single server, a cluster of servers, an administrative domain containing many servers, or even more. The scope of an application server instance is product-dependent, but it must be possible to deploy multiple applications to a single application server instance.
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • I've changed to: `java:comp/` but still getting javax.naming.NameNotFoundException: java:comp/ – aleroot Mar 15 '18 at 16:11
  • I just tried `initContext.lookup("java:comp/env");` and that worked for me. Also, do you have the `jndi-1.0` feature enabled in your server.xml? (or some other feature that pulls in `jndi-1.0`) – Andy Guibert Mar 15 '18 at 16:12
  • also, looking up `java:comp/` will not work but `java:comp` will – Andy Guibert Mar 15 '18 at 18:53
0

Had a similar problem going between WebSphere and Tomcat. I'm developing and testing on a Tomcat server and using utilities I can't change that handle the DB connection to our DB2. On WebSphere it uses a constant set to "jdbc/COMPDB2" to retrieve the DataSource when I configure Tomcat and my Web.xml file it resolves to "java:comp/env/jdbc/SFCCDB2"

My work around for on local work space it to add a listener to copy the resource to the level in the InitialContext. I'm not very experienced with the server side of things but this is working so far using TomEE 7.0.81.

        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SFCCDB2");
        javax.naming.Context envCtx = (javax.naming.Context) ctx.lookup("java:comp/env");
        try{
            /*
            Added this because after redeploying code to the server it would error 
            connecting to the DB with an SQLException Datasource is closed
             */
            DataSource dataSource = (DataSource) ctx.lookup("jdbc/COMPDB2");
            ctx.destroySubcontext("jdbc");
        } catch (NamingException e){
            //Doesn't exist; safe to just add
        }
        ctx.createSubcontext("jdbc");
        ctx.bind("jdbc/COMPDB2", ds);
        ctx.close();
Tri Dave
  • 11
  • 1
  • 1