Connection Pooling using Jetty, in Oracle
Asked Answered
S

4

7

I am trying to implement the concept of ConnectionPooling in Oracle using Jetty server. I have tried the following that I saw on a tutorial. It is working if I deploy using Tomcat server, but Jetty seems to be giving me an unusual error. Details are below -

I have a class called TestServlet.java defined as -

import java.io.IOException;
import java.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.sql.DataSource;


@SuppressWarnings("serial")
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
public TestServlet() throws ServletException{
    System.out.println("Constructor");
    init();
}
public DataSource dataSource;
private Connection con;
private Statement statement;

public void init() throws ServletException {
    System.out.println("inside init method");
    try {
        // Get DataSource
        Context initContext  = new InitialContext();
        System.out.println("Before envcontext");
        Context envContext  = (Context)initContext.lookup("java:comp/env");
        System.out.println("After envcontext");
        dataSource = (DataSource)envContext.lookup("jdbc/DSTest");
        System.out.println(dataSource.toString());
    } catch (NamingException e) {
        System.out.println("Exception in try");
        e.printStackTrace();
    }
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    System.out.println("Request: "+req+"\tResponse: "+resp);
     int i=0;
    ResultSet resultSet = null;
    try {
        // Get Connection and Statement
        con = dataSource.getConnection();
        statement = con.createStatement();
        String query = "SELECT * FROM USER";
        resultSet = statement.executeQuery(query);
        while (resultSet.next()) {
            ++i;
            System.out.println(i+":\nID:"+resultSet.getString("ID") +"\nEmail:"+ resultSet.getString("UEMAIL") +"\nPassword:" + resultSet.getString("PASSWORD")+"\nFlag:"+resultSet.getShort("FLAG")
                    +"\n");

        }
    } catch (SQLException e) {
        System.out.println("EXCEPTIOn");
        e.printStackTrace();
    }finally {
        try { if(null!=resultSet)resultSet.close();} catch (SQLException e) 
        {e.printStackTrace();System.out.println("1");}
        try { if(null!=statement)statement.close();} catch (SQLException e) 
        {e.printStackTrace();System.out.println("2");}
        try { if(null!=con)con.close();} catch (SQLException e) 
        {e.printStackTrace();System.out.println("3");}
    }
}

}

And my Jetty-web.xml is as -

<?xml version="1.0" encoding="UTF-8"?>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
 <Arg>java:comp/env</Arg>
 <Arg>jdbc/DSTest</Arg>
 <Arg>
     <New class="org.apache.commons.dbcp.BasicDataSource">
        <Set name="driverClassName">oracle.jdbc.OracleDriver</Set>
        <Set  name="url">ConnectionUrl</Set>
        <Set name="username">app_user</Set>
        <Set name="password">abcd</Set>
     </New>
 </Arg>
</New>
</Configure>

When I run the servlet class I get an error that says -

java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructors(Class.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:748)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1078)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:993)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:741)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:383)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:317)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:276)
at org.eclipse.jetty.webapp.JettyWebXmlConfiguration.configure(JettyWebXmlConfiguration.java:100)
at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:427)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1207)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:262)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at runjettyrun.Bootstrap.main(Bootstrap.java:80)
Caused by: 
java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:415)
at runjettyrun.ProjectClassLoader.loadClass(ProjectClassLoader.java:92)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructors(Class.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:748)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1078)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:993)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:741)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:383)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:317)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:276)
at org.eclipse.jetty.webapp.JettyWebXmlConfiguration.configure(JettyWebXmlConfiguration.java:100)
at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:427)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1207)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:262)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at runjettyrun.Bootstrap.main(Bootstrap.java:80)
2015-10-30 14:22:51.172:INFO:oejs.AbstractConnector:Started     [email protected]:8081 STARTING
2015-10-30 14:22:51.172:WARN:oejuc.AbstractLifeCycle:FAILED   org.eclipse.jetty.server.Server@7857fe2: java.lang.NoClassDefFoundError:   org/apache/commons/pool/impl/GenericObjectPool
java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructors(Class.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:748)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1078)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:993)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:741)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:383)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:317)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:276)
at org.eclipse.jetty.webapp.JettyWebXmlConfiguration.configure(JettyWebXmlConfiguration.java:100)
at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:427)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1207)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:262)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at runjettyrun.Bootstrap.main(Bootstrap.java:80)
Caused by: 
java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:415)
at runjettyrun.ProjectClassLoader.loadClass(ProjectClassLoader.java:92)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructors(Class.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:748)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1078)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:993)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:741)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:383)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:317)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:276)
at org.eclipse.jetty.webapp.JettyWebXmlConfiguration.configure(JettyWebXmlConfiguration.java:100)
at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:427)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1207)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:262)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at runjettyrun.Bootstrap.main(Bootstrap.java:80)
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructors(Class.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:748)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1078)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:993)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:741)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:383)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:317)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:276)
at org.eclipse.jetty.webapp.JettyWebXmlConfiguration.configure(JettyWebXmlConfiguration.java:100)
at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:427)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1207)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:262)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at runjettyrun.Bootstrap.main(Bootstrap.java:80)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:415)
at runjettyrun.ProjectClassLoader.loadClass(ProjectClassLoader.java:92)
... 20 more

I have added the commons-dbcp-1.4.jar to my WEB-INF/lib folder in eclipse after looking at other stackoverflow threads. Even after doing that, I am getting the same error when I deploy on Jetty.

I am using jetty server version 8 integrated with eclipse luna 4.4.1.

Any help much appreciated.

Substratum answered 30/10, 2015 at 7:22 Comment(5)
Is commons-pool in a separate jar file from commons-dbcp? It used to be that way, and you needed both.Khosrow
I added both versions. But it doesn't worked for me. Got any other smart ideas.@KhosrowSubstratum
If it is Jetty itself using the pool (as opposed to your webapp), you need to put the jar files in Jetty's own lib folder (not into WEB-INF/lib).Khosrow
You can't just put arbitrary jars in the Jetty distribution's ${jetty.home}/lib and expect them to be used. If you are using Jetty 9, add the ext module to your ${jetty.base} and then add the extra server side jars you want to ${jetty.base}/lib/ext/Faeroese
I understand you have added comon-pool, but can you check as recommended in https://mcmap.net/q/753446/-java-lang-noclassdeffounderror-org-apache-commons-pool-impl-genericobjectpool? (Deployment assembly)John
W
5

You have 3 ways to do it:

  1. If you are using maven or gradle in your project simply add commons-pool dependency to your pom.xml or build.gradle file (recommended way)
  2. If you are not using one of the mentioned tools place correct jar in the WEB-INF/lib directory
  3. The last one is to place jar in ${jettyHome}/lib directory

Make sure that you are using correct version of the commons-pool for your current commons-dbcp implementation

Wrong answered 5/11, 2015 at 22:46 Comment(2)
Yes I did, but I am not getting where does it find the context for the remaining name 'env'.Substratum
@Tejas Saitwal. [here] (#26217292) similar jndi problem. If you configured everything properly you should be able to lookup datasource by using datasource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/DSTest");.Wrong
O
0

you don't you just use tomcat instead. find it here http://tomcat.apache.org/ :) Note that jetty is only well to be used when you need to make local app development easier. https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html I suggest you start reading up the basics for tomcat and as you begin to gain a basic understanding of how things work, you should start implementing Connection Pooling :) Hope this helps.

Overbold answered 10/11, 2015 at 8:59 Comment(1)
This doesn't address the question. Jetty is widely adopted in production environments and has connection pooling. Often this is a preferable server to utilize when deploying to embedded / resource limited environments. Saying Jetty is only used to make local app development easier is absolutely incorrect, this is simply one use case.Anecdote
T
0

For Jetty 10.x you could resolve it in the following way,

  1. Enable ext module -> java -jar start.jar --add-modules=ext
  2. In $JETTY_BASE/start.d/ext.ini will get created after execution of 1)
  3. Place your oralce jdbc jar file in location $JETTY_HOME/lib/ext/
  4. Again start your application -> java -jar start.jar
Treillage answered 26/4, 2021 at 15:26 Comment(0)
D
-1

When i need an Oracle connection pool, i always use ojdbcx.jar with ucp.jar. You can download them from here.

You can find tons of how-tos from Google but there is an easy one(for Jetty 6) here.

Dollfuss answered 10/11, 2015 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.