views:

44

answers:

0

Hello there,

first things first: I've read various related questions on here, before actually deciding to enter my question anyway. The reason is that my problem is somewhat different from the others I've read about.

My aim: I have an application written in GWT (I'll explain later why it's relevant) that access some servlet via RPC. The servlet is supposed to access the database and return data (via a DTO) back. In order to not open a connection each time a servlet does its work, I decided to use connection pooling.

Problem(s):

I use GWT's built-in server Jetty for development. Later on I do want to deploy onto a tomcat server. At the moment using tomcat for devmode is not an option.

I use c3p0 for connection pooling. My database is MySQL (there won't be any other). I initialize the connection pool via ServletListenerContext.contextInitialized() and suspend it via ServletListenerContext.contextDestroyed().

This part works flawlessly within Jetty and tomcat 6, however: in devmode (GWT, Jetty) the connection pooling works as expected, while once deployed onto tomcat 6 the getConnection-method of my helper-class throws a NullPointerException.

My helper-class code:

package odrop.server;

import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;

/**
 * This static class handles ConnectionPooling
 * @author divStar
 *
 */
public class ConnectionPool implements ServletContextListener {
    private static Properties serverConstants = new Properties();
    private static ComboPooledDataSource dataSource;


/**
 * setupDriver()    sets up the PoolingDriver that will be used in place of the real driver.
 *         Since this method is called upon initialization, there's no need to call it from outside.
 * @throws ClassNotFoundException    if the specified class is not found, this is thrown
 * @throws SQLException                if the specified SQL-statement couldn't be executed, this is thrown
 */
private static void setupDriver() {
    // we need to read the properties file first
    try {
        String prefix = "/odrop/";
        serverConstants.load(new FileInputStream(prefix + "resources/server.properties"));

        Class.forName(serverConstants.getProperty("driverClass"));
        dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(serverConstants.getProperty("driverClass")); //loads the jdbc driver
        dataSource.setJdbcUrl(serverConstants.getProperty("dsn"));
        dataSource.setUser(serverConstants.getProperty("username"));
        dataSource.setPassword(serverConstants.getProperty("password"));

        dataSource.setMinPoolSize(5);
        dataSource.setAcquireIncrement(5);
        dataSource.setMaxPoolSize(20);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (PropertyVetoException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 * getConnection()            creates a connection that can be used by a servlet.
 * @return                    a valid connection (java.sql.Connection)
 * @throws SQLException        if the requested SQL-procedure couldn't execute properly, this exception is thrown
 */
public static Connection getConnection() throws SQLException {
    return dataSource.getConnection();  // the NullPointerException occurs (according to the output) right here
}

/**
 * shutdownDriver()            shuts down the PoolingDriver.
 * @throws SQLException        occurs, if the procedures couldn't execute properly
 */
public static void shutdownDriver() {
    try {
        DataSources.destroy(dataSource);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

/**
 * contextDestroyed() is called, when the server shutsdown (properly!)
 */
@Override
public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("Shutting down database connection pooling...");
    shutdownDriver();
}

/**
 * contextInitialized() is called, when the server starts up
 */
@Override
public void contextInitialized(ServletContextEvent arg0) {
    System.out.println("Initializing database connection pooling...");
    setupDriver();
}

}

The exception I get - only when running on tomcat 6!

26.09.2010 15:37:58 org.apache.catalina.core.ApplicationContext log
SCHWERWIEGEND: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.util.ArrayList odrop.client.rpc.RPCCharactersService.getCharacters(java.util.ArrayList)' threw an unexpected exception: java.lang.NullPointerException
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:378)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:581)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:188)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:224)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at odrop.server.ConnectionPool.getConnection(ConnectionPool.java:85)
    at odrop.server.RPCServiceImpl.prepareSet(RPCServiceImpl.java:47)
    at odrop.server.rpc.RPCCharactersServiceImpl.getCharacters(RPCCharactersServiceImpl.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:562)
    ... 17 more

Please help me - I don't have any idea what causes this problem, because it runs flawlessly in Jetty while causing trouble when deploying. I put the connection pooling libs as well as the original mysql-connector into /webapps/odrop/WEB-INF/lib .

related questions