1

I'm trying to understand Tomcat configuration at the moment, but I have problems using Eclipse to do that.

I have a Dynamic Web project in Eclipse running on a Tomcat 9 server. I'm not using Maven as this adds one more layer I don't fully understand.

I basically wanted to add a connexion pool to the project to access my DB with a DataSource factory. To do that, I added a context.xml in WebContent/META-INF/. It looks like this:

<Context path="/my-app" docBase="my-app" debug="99">
<Ressource
    name="jdbc/postgres"
    auth="Container"
    type="javax.sql.DataSource"
    description="Pool"
    driverClassName="org.postgresql.Driver"
    url="jdbc:postgresql://localhost:5432/my-app"
    username="blabla"
    password="blabla"
    maxTotal="4"
    maxIdle="2"
    maxWaitMillis="10000"
/>
</Context>

Somewhere in the code I use this resource with:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup( "java:comp/env" );
DataSource ds = (DataSource) envCtx.lookup( "jdbc/postgres" );
Connection con = ds.getConnection();

Unfortunately, the 3rd line raises a NameNotFoundException saying that the name jdbc/postgresis not linked to the Context.

So I read plenty of docs about this but can't find the reason. Did I put this context.xml file at the wrong place in Eclipse?

I tried to add

  <resource-ref>
    <res-ref-name>jdbc/postgres</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

in my WEB-INF/web.xml and I made some progress but got another exception:

Cannot create JDBC driver of class '' for connect URL 'null'

Something else I tried is to call the JNDI resource with java: in front of it but it didn't help either:

DataSource ds = (DataSource) envCtx.lookup( "java:jdbc/postgres" );

Any idea about what I'm doing wrong is welcome.

Thanks a lot.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
macadam
  • 31
  • 5
  • 1
    To exclude the one and other, did you copypaste the context.xml unmodified from the real file, including the typo, or did you type it down in the editor here without actually testing it? – BalusC Feb 01 '18 at 19:27
  • I copied/paste from the real file but changed the name of the app, username, password. Where do you see a typo? – macadam Feb 01 '18 at 19:35
  • You typed "resource" in French instead of English. – BalusC Feb 01 '18 at 20:11
  • Ooh shoot... Can't believe it, I've been spending hours on this. You saved my life, thank you!! (And yes I'm french :D) – macadam Feb 01 '18 at 20:59

0 Answers0