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/postgres
is 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.