1

My web application has a servlet listener like this:

@WebListener
public class MyContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent e)
    {
        ...
        con = DatasourceProvider.getDatasource("java:comp/env/jdbc/mysql/mydb").getConnection();
        ...
    }
}

My resource definition is OK & it has worked fine it tomcat 7, but when I upgraded to tomcat server 8.5, it throws an exception at startup:

javax.naming.NameNotFoundException: Name [jdbc/mysql/mydb] is not bound in this Context. Unable to find [jdbc].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    ...

Anyway, I can get mysql connections after server starup (in the other servlets) using same code without any problem. So the question is that what causes this error while tomcat startup?

UPDATE
Here is my virtual host configuration:

<Host name="my-domain.ir" appBase="/var/www/webapp" >
    <Context path="" docBase="MyWebApp" 
     xmlValidation="false" xmlNamespaceAware="false" crossContext="false" reloadable="false" >
    <Resource name="jdbc/mysql/mydb" auth="Container" type="javax.sql.DataSource"

    initialSize="1" maxTotal="100" maxIdle="2"
    maxWaitMillis="20000" removeAbandonedOnMaintenance="true" removeAbandonedTimeout="300"
    validationQuery="select now();"

    username="myUser" password="myPass" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8&amp;connectionCollation=UTF8_PERSIAN_CI&amp;noAccessToProcedureBodies=true"
    />
    </Context>
</Host>
Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87

1 Answers1

0

I had a similar issue as like you, for which I added the a line (similar to this) into my Tomcat's context.xml to resolve the issue.

<ResourceLink global="jdbc/mysql/mydb" name="jdbc/mysql" type="javax.sql.DataSource"/>

Following are the links to questions that point out to a similar solution

Question-1

Question-2

Question-3

Hope that helps!!!

Community
  • 1
  • 1
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
  • But I can't do this because I've too many virtual hosts & each one has it's own resources & I don't want & can't make them global. In addition, there should be a rational reason for this situation. – Ehsan Khodarahmi Dec 05 '16 at 06:21
  • Now that's interesting, let me see what I can do on this then! – N00b Pr0grammer Dec 06 '16 at 01:20