0 Replies Latest reply: Jul 2, 2014 9:46 AM by 0e9b2a9d-b40a-4417-ac9b-a0116f3bab17 RSS

    JDBC Connection Pooling NullPointerException and ClassNotFoundException

    0e9b2a9d-b40a-4417-ac9b-a0116f3bab17

      Hi,

          I am trying to get a connection from a JDBC pool in a JSP web project.  We are using Apache, Tomcat and Eclipse.  My web.xml file (which is in the WEB-INF) folder is below:

       

       

       

      <?xml version="1.0" encoding="UTF-8"?>

      web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">


      <display-name>DataBaseConn</display-name>

       

      <welcome-file-list>


      <welcome-file>index.html</welcome-file>


      <welcome-file>index.htm</welcome-file>


      <welcome-file>index.jsp</welcome-file>


      <welcome-file>main.jsp</welcome-file>


      <welcome-file>default.html</welcome-file>


      <welcome-file>default.htm</welcome-file>


      <welcome-file>default.jsp</welcome-file>


      </welcome-file-list>


      <resource-ref> 


      <res-ref-name>jdbc/UCPPool</res-ref-name> 


      <res-type>javax.sql.DataSource</res-type> 


      <res-auth>Container</res-auth>


      </resource-ref>


      </web-app>

       

       

       

      The context.xml file is in the META-INF folder of the application and looks as follows:

       

      <?xml version='1.0' encoding='utf-8'?>


      <Context docBase="*****" path="/*****" reloadable="true">


      <Resource name="jdbc/UCPPool" auth="Container" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" type="javax.sql.DataSource" description="JDBC Connection Pool test" username="*****" password="*****" url="jdbc:oracle:thin://IPAddress:1521/SID" initializeSize="34" maxActive="377" maxIdle="233" minIdle="89" timeBetweenEvictionRunsMillis="34000" driverClassName="oracle.jdbc.OracleDriver"/>

       

      </Context>

       

      I have also tried with this context (the factory and type attributes are different and the url has the full server name with domain and sub-domain).

       

       

      <Resource name="jdbc/UCPPool" auth="Container" factory="oracle.ucp.jdbc.PoolDataSourceImpl" type="oracle.ucp.jdbc.PoolDataSource" description="Pas testing UCP Pool in Tomcat" connectionFactoryClassName="oracle.jdbc.pool.OracleDataSource" minPoolSize="5" maxPoolSize="100" inactiveConnectionTimeout="20" username="*****" password="*****" url="jdbc:oracle:thin://servername:1521/SID" connectionPoolName="UCPPool" /> 

      </Context>

       

      I have a ConnectionPool class which follows an example from a book.  The example is not using EJB's.

       


      com.connpool;

      import java.sql.Connection;

      import java.sql.SQLException;

      import javax.naming.InitialContext;

      import javax.sql.DataSource;


      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/UCPPool") ;

       

            } catch (Exception e) {          

       

       

                e.printStackTrace() ;
           }

       

          }

       

       

      public static ConnectionPool getInstance()

          if (pool == null)

          {   

              pool = new ConnectionPool() ;

          }

       

       

          return pool;

          }

       


          public Connection getConnection() {

              try

              {
                  return dataSource.getConnection() ;

              }

              catch (SQLException sqle) {

                  sqle.printStackTrace() ;

                  return null ;

       

              }

          }


      public void freeConnection(Connection c)


      try  {

          c.close() ;

          }

          catch (SQLException sqle) {

              sqle.printStackTrace() ;

          }

       

       

       

      Finally, the jsp page is getting an instance of the ConnectionPool class and using the getConnection() method to get the connection object. 


      ConnectionPool pool = ConnectionPool.getInstance() ;

      Connection connection = pool.getConnection() ;

       

       

       

       

      I am getting the following errors:

       




      javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSourceFactory]



      javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSourceFactory]










      : Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSourceFactory]Jul 2, 2014 10:07:12 AM org.apache.catalina.core.StandardWrapperValve invoke




      ConnectionPool.java:46)


      main_jsp.java:73)


      HttpJspBase.java:70)


      HttpServlet.java:717)


      JspServletWrapper.java:377)


      JspServlet.java:313)


      JspServlet.java:260)


      HttpServlet.java:717)


      ApplicationFilterChain.java:290)


      ApplicationFilterChain.java:206)


      StandardWrapperValve.java:233)


      StandardContextValve.java:191)


      StandardHostValve.java:127)


      ErrorReportValve.java:102)


      StandardEngineValve.java:109)


      CoyoteAdapter.java:298)


      Http11Processor.java:852)


      Http11Protocol.java:588)


      JIoEndpoint.java:489)


      Thread.java:662)



      ResourceFactory.java:81)


      NamingManager.java:304)


      NamingContext.java:793)


      NamingContext.java:140)


      NamingContext.java:781)


      NamingContext.java:140)


      NamingContext.java:781)


      NamingContext.java:140)


      NamingContext.java:781)


      NamingContext.java:153)


      SelectorContext.java:152)


      InitialContext.java:392)


      ConnectionPool.java:26)


      ConnectionPool.java:37)


      main_jsp.java:72)


      HttpJspBase.java:70)


      HttpServlet.java:717)


      JspServletWrapper.java:377)


      JspServlet.java:313)


      JspServlet.java:260)


      HttpServlet.java:717)


      ApplicationFilterChain.java:290)


      ApplicationFilterChain.java:206)


      StandardWrapperValve.java:233)


      StandardContextValve.java:191)


      StandardHostValve.java:127)


      ErrorReportValve.java:102)


      StandardEngineValve.java:109)


      CoyoteAdapter.java:298)


      Http11Processor.java:852)


      Http11Protocol.java:588)


      JIoEndpoint.java:489)


      Thread.java:662)


      java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSourceFactory


      WebappClassLoader.java:1516)


      WebappClassLoader.java:1361)


      ResourceFactory.java:79)