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&characterEncoding=UTF-8&characterSetResults=UTF-8&connectionCollation=UTF8_PERSIAN_CI&noAccessToProcedureBodies=true"
/>
</Context>
</Host>