0

Trying to set up a couple of JDBC connections in the context.xml of my tomcat instance but struggling to understand why some names seem to clash.

With some combinations of name I get the folllowing:

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.BasicDataSource cannot be cast to javax.naming.Context

Can someone explain why it doesn't work if I have 2 resources with names like:

name="jdbc/postgres/1/2/3/4/5"
name="jdbc/postgres/1/2/3/4/5/6"

But changing the second name to this does work:

name="jdbc/postgres/1/2/3/4/4/6"

Thanks.

RichCF
  • 3
  • 2

1 Answers1

0

According to the Tomcat doc, name is the JNDI name relative to the java:comp/env context. The full name is treated as a path with the elements separated by /.

In your example,

name="jdbc/postgres/1/2/3/4/5"

creates the data source node 5 under the path

java:comp/env/jdbc/postgres/1/2/3/4

The following

name="jdbc/postgres/1/2/3/4/5/6"

then tries to create the data source node 6 at path

java:comp/env/jdbc/postgres/1/2/3/4/5

The exception occurs because Tomcat expects a path element (Context) at 5 but finds a data source instead.

In your working example, you create the data source 6 under the different path

java:comp/env/jdbc/postgres/1/2/3/4/6
Community
  • 1
  • 1
Michael Koch
  • 1,152
  • 11
  • 17
  • Thanks for the reply. I see, I assumed the whole value of _name_ would be used as a key, - clearly not! – RichCF Feb 24 '15 at 15:15
  • I think my lack of understanding stems from considering this from a DNS perspective, it makes more sense to think in a file system context i.e. a directory and a file with the same name cannot exist at the same path. – RichCF Feb 25 '15 at 10:53