Mein Web-Anwendung ein Servlet-Listener wie folgt Zugriff hat:Tomcat 8.5 - Kann nicht MySQL JDBC-Ressource in contextInitialized Methode von ServletContextListener
@WebListener
public class MyContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent e)
{
...
con = DatasourceProvider.getDatasource("java:comp/env/jdbc/mysql/mydb").getConnection();
...
}
}
Meine Ressourcendefinition ist OK & es hat geklappt hat es tomcat 7, aber wenn ich auf Tomcatbediener 8.5 aktualisiert hat, wirft es eine Ausnahme beim Start:
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)
...
Wie auch immer, ich mysql-Verbindungen nach Server starup (in den anderen Servlets) mit gleichem Code ohne Probleme bekommen. Die Frage ist also, was diesen Fehler beim Tomcat Start verursacht?
UPDATE
Hier ist meine virtuelle Host-Konfiguration:
<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>
Können Sie Ihre 'context.xml' teilen? –
@ N00bPr0grammer es ist Standard 'context.xml' für Tomcat 8.5 & ich habe es nicht geändert. Aber wenn Sie "Context" -Element meiner Webanwendung meinen, fügte ich es zum Update hinzu –