shjnlee
shjnlee

Reputation: 243

NullPointerException when initiating the connection pool

I have settings in the context.xml which contain the information for the connection of my database

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

        <Resource 
        name="jdbc/*(my username)*" 
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        auth="Container" 
        type="javax.sql.DataSource"
        removeAbandoned="true" 
        removeAbandonedTimeout="30" 
        maxACtive="100"
        maxIdle="30" 
        maxWait="10000" 
        username= *(my username)*
        password= *(my password)*
        driverClassName="com.ibm.db2.jcc.DB2Driver" 
        url="jdbc:db2://(my server host):50000/*(my username)*">
    </Resource>
</Context>

While for the Connection Pool I created a class to initiate and to connect it to the database. The error begins in the method of getConnection(). The way the error is NullPointerException in the return of the datasource.getConnection()

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


public class ConnectionPool {
    private static ConnectionPool pool = null;
    private static DataSource dataSource = null;

    private ConnectionPool() {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/COMPANY");
            System.out.print(dataSource);
        } catch (NamingException e) {
            System.out.println(e);
        }
    }

    public static synchronized ConnectionPool getInstance() {
        if (pool == null) {
            pool = new ConnectionPool();
        }
        return pool;
    }

    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            System.out.println(e);
            return null;
        }
    }

    public void freeConnection(Connection c) {
        try {
            c.close();
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}

I tested the database connection with Eclipse's Perspective Database Development and I was able to connect and done all the queries.

In addition, it gives me this error in output

SEVERE: Unable to create initial connections of pool.
java.sql.SQLException: No suitable driver found for jdbc:db2://db2.cecsresearch.org:50000/(my username)

Although I have the jar library files in the lib folder and in the classpath.

Upvotes: 3

Views: 1407

Answers (2)

Carlos Saltos
Carlos Saltos

Reputation: 1511

Please download the DB2 Java driver from https://artifacts.alfresco.com/nexus/content/repositories/public/com/ibm/db2/jcc/db2jcc4/10.1/db2jcc4-10.1.jar and add it to your tomcat/lib directory. Then restart Tomcat and you should be good to go !!

Upvotes: 1

Pieter
Pieter

Reputation: 2785

The nullpointer is simply caused by the following problem:

java.sql.SQLException: No suitable driver found for jdbc:db2://db2.cecsresearch.org:50000/(my username)

Putting your database driver in $CATALINA_HOME/lib should fix the problem.

For more info, check the docs.

driverClassName (String) The fully qualified Java class name of the JDBC driver to be used. The driver has to be accessible from the same classloader as tomcat-jdbc.jar

You can also check the official jndi-datasource examples.

Upvotes: 0

Related Questions