vendredi 17 juin 2016

tomcat8 - Main java HttpServlet works, but ServletContextListener cannot access mysql database

I am using ubuntu 16.04, tomcat8, mysql 5.7.12, and activeMQ for some things. I have googled alot. I don't generally find examples about my exact situation, but commonly for older tomcat and mysql versions, etc. I basically get this error when the context listener is working (every 15 mins it checks for certian activity). I am using the IntelliJ community edition and maven.

javax.naming.NameNotFoundException: Name [comp/env/jdbc/acnn] is not bound in this Context. Unable to find [comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:819)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.davidliebman.desktop.audio.ACNNDataBase.getConnection(ACNNDataBase.java:87)
at org.davidliebman.desktop.audio.ACNNDataBase.doUpdate(ACNNDataBase.java:199)
at org.davidliebman.desktop.audio.ACNNDataBase.deleteUserPlaying(ACNNDataBase.java:386)
at org.davidliebman.server.audio.ACNNContextListener$DB.run(ACNNContextListener.java:39)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
java.lang.NullPointerException

'acnn' is the name of my database. below is my web.xml file in my war:

<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    metadata-complete="false"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

<servlet>
       <servlet-name>ACNNServer</servlet-name>
       <servlet-class>org..server.audio.ACNNServer</servlet-class>
</servlet>

<servlet-mapping>
       <servlet-name>ACNNServer</servlet-name>
       <url-pattern>/ACNNServer</url-pattern>
</servlet-mapping>


<listener>
    <listener-class>
        org..server.audio.ACNNContextListener
    </listener-class>
</listener>

<listener>
    <listener-class>
        org..server.audio.ACNNContextListenerConsumer
    </listener-class>
</listener>

<resource-ref>
    <description>ACNN DB</description>
    <res-ref-name>jdbc/acnn</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

</web-app>

here is the section of my conf/server.xml

<GlobalNamingResources>
<!-- Editable user database that can also be used by
     UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />

 <Resource name="jdbc/acnn" auth="Container" type="javax.sql.DataSource"
           maxTotal="100" maxIdle="30" maxWaitMillis="10000"
           username="xxx" password="xxx" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/acnn"
            removeAbandonedOnBorrow="true" 
            removeAbandonedOnMaintenance="true" 
            removeAbandonedTimeout="60" 
            logAbandoned="true" />

<ResourceLink name="jdbc/acnn"
            global="jdbc/acnn"
            type="javax.sql.DataSource" />
</GlobalNamingResources>

here is the matching part of the conf/context.xml file

<Context allowLinking="true">

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

<Resource name="jdbc/acnn" auth="Container" type="javax.sql.DataSource"
           maxTotal="100" maxIdle="30" maxWaitMillis="10000"
           username="xxx" password="xxx" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/acnn"
            removeAbandonedOnBorrow="true" 
            removeAbandonedOnMaintenance="true" 
            removeAbandonedTimeout="60" 
            logAbandoned="true" />

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

</Context>

finally in despiration I copied the material to the META-INF/context.xml file. I think there it is ignored. Not sure.

<Context allowLinking="true">


<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>


<Resource name="jdbc/acnn" auth="Container" type="javax.sql.DataSource"
           maxTotal="100" maxIdle="30" maxWaitMillis="10000"
           username="xxx" password="xxx" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/acnn"
            removeAbandonedOnBorrow="true" 
            removeAbandonedOnMaintenance="true" 
            removeAbandonedTimeout="60" 
            logAbandoned="true" />

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

</Context>

the actual code, which works when run from the HttpServlet and not from the ServletContextListener is here:

public class ACNNDataBase {
    ...
    private Connection getConnection() {
        Connection conn = null;
        try {
            if (!doUseServerJDBC) {
                Class.forName(JDBC_DRIVER).newInstance();
                conn = DriverManager.getConnection(url, user, password);
            }
            else {
                InitialContext ctx = new InitialContext();
                DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/acnn");
                conn = ds.getConnection();
            }
        }
        catch (Exception e) { e.printStackTrace();}
        return conn;
    }
    ...
}

then I go on and use conn somewhere. That's everything. Any help would be appreciated. Thanks.

Aucun commentaire:

Enregistrer un commentaire