V7.2 uberJar JNDI connection access

Hi:

I’ve written my own code to try to get access to the JNDI database connection within an uberJar. I have declared my jetty-env.xml with the default cuba datasource JNDI name jdbc/CubaDS. My app works, except when I try to access the JNDI connection myself. Here’s the jetty-env.xml:

<?xml version="1.0"?>
<!--
  ~ (C) 2019 Professional Advertising Systems Inc.
  -->

<!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg/>
        <Arg>jdbc/CubaDS</Arg>
        <Arg>
            <New class="org.apache.commons.dbcp2.BasicDataSource">
                <Set name="driverClassName">org.postgresql.Driver</Set>
                <Set name="url">jdbc:postgresql://postgres.paslists.com/pas</Set>
                <Set name="username"><SystemProperty name="app.rade.user"/></Set>
                <Set name="password"><SystemProperty name="app.rade.pwd"/></Set>
                <Set name="maxIdle">2</Set>
                <Set name="maxTotal">20</Set>
                <Set name="maxWaitMillis">5000</Set>
            </New>
        </Arg>
    </New>
</Configure>

Here’s the code that tries to get the connection info:

            DataSource ds;
            Connection conn=null;
            String connStr;
            if (parameterMap.containsKey("conn")) {
                connStr = (String) parameterMap.get("conn");
            } else {
                connStr = "java:comp/env/jdbc/CubaDS";
            }

            try {
                Context ctx = new InitialContext();
                ds = (DataSource) ctx.lookup(connStr);
                conn = ds.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }

I am able to get the ctx:

image

Then, when I try to look up the datasource with ctx.lookup(connStr), I get the following exception (jdbc name is not bound):

image

Any idea why I can’t look up the connection? What am I missing here?

Thanks in advance for any help you can provide.

I think I can shed some more light on the subject. This code is running in the report generator as a background task using CustomReport. I believe this is a thread-related issue. Still researching the options to fix it.

If anybody knows a good way to send data between the report execution thread and the main thread, that would be very helpful! :slight_smile:

I have resolved the problem by using the CUBA datasource rather than looking it up myself in JNDI. The code was replaced with:

try {
      Persistence persistence = AppBeans.get(Persistence.class);
      ds = (DataSource) persistence.getDataSource();
      conn = ds.getConnection();
} catch (Exception e) {
      e.printStackTrace();
}

This is not quite as flexible as my original code, as that allowed for passing in a JDBC connection string, but I never really needed it anyway. :wink: