jetty mysql database connection pooling
In my case, I went to the jetty mailing list (https://dev.eclipse.org/mailman/listinfo/jetty-users) to ask my question.
A very smart person (Hi Jan!) wrote and answered like this:
There are generally many different ways to acheive the same outcome in jetty, so that's probably why you've seen different ways documented. Generally having options is a good thing
In case there's any confusion, this is the definitive page: http://wiki.eclipse.org/Jetty/Feature/JNDI If there's something missing, then please let us know so we can update the page.
Your case is a little out-of-the-ordinary in that you are using javaee-style features, but without a web.xml. This is not something I've encountered before, but it sounds like something I should add to the documentation.
I would recommend (as does the page above) that you put any jndi definitions into a WEB-INF/jetty-env.xml file and not the context xml file. If you do that, then you can effectively do what a web.xml element would do and bind your datasource into the java:comp/env namespace by defining your resource and binding it into java:comp/env in the one go (note this will NOT work in a context xml file, it must be jetty-env.xml):
So do the following in WEB-INF/jetty-env.xml:
<New id="jdbc/myds" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/myds</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://localhost:3306/chat</Set>
<Set name="User">root</Set>
<Set name="Password">sillyness</Set>
</New>
</Arg>
<Call name="bindToENC">
<Arg>jdbc/myds</Arg>
</Call>
</New>
In the call to bindToENC bind whatever name you want to look up as the suffix to java:comp/env. Eg if you want to lookup java:comp/env/my/foo then the bindToENC argument would be "my/foo".
-- Jan Bartel www.webtide.com – Developer advice, services and support from the Jetty & CometD experts. _______________________________________________ jetty-users mailing list [email protected] https://dev.eclipse.org/mailman/listinfo/jetty-users
Now, that wasn't quite enough (I'm a jetty newbie). The WEB-INF referred to was the WEB-INF inside the war file for my servlet...so I extracted the contents of the war file into a directory, and then I could easily put the jetty-env.xml file in the WEB-INF directory.
This was critical too:
OK, I assumed a little jetty knowledge there, and thought you'd know that what I posted was merely a snippet from what you'd need to have for a complete jetty-env.xml file.
The wiki page for the jetty-env.xml file is here: http://wiki.eclipse.org/Jetty/Reference/jetty-env.xml
So wrap the root element as shown on that page around the snippet I posted and you should be good.
Now /that/ got the database connection to be attempted. Then I was getting connection refused from 127.0.0.1
errors in /var/log/auth.log.
Turns out for that I had denyhosts running, and sure enough someone had put ALL: 127.0.0.1
in there... which meant connections to my local machine were denied.
Once that detail was fixed, the servlet can finally reach MySQL on my server.
Ken Corey
Updated on June 04, 2022Comments
-
Ken Corey almost 2 years
I'm trying to do something seemingly simple: set up a small pool of database connections so that a servlet (just the one) doesn't create my server at 1 connection per second.
On
Ubuntu 10.04
, with the latest upgrade/update, I'm usingEclipse Jetty 8.1.5.v20120716
. I'm trying to connect to aMySQL 5.1
server.My servlet is called
gcm
, so in${jetty}/contexts
, I have thisgcm.xml
file:<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext"> <New id="jdbc/myds" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg></Arg> <Arg>jdbc/myds</Arg> <Arg> <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"> <Set name="Url">jdbc:mysql://localhost:3306/chat</Set> <Set name="User">root</Set> <Set name="Password">sillyness</Set> </New> </Arg> </New> <Set name="contextPath">/</Set> <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/gcm</Set> <Set name="extractWAR">true</Set> </Configure>
When I start Jetty with
java -jar start.jar -port=8080
, everything starts fine until I try to hit the database from my servlet. The code handling part of the request looks like this:public static List<String> getDevices() throws javax.naming.NamingException, java.sql.SQLException { synchronized (regIds) { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myds"); Connection conn = null; Statement stmt = null; try { conn = ds.getConnection(); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from chatdevice"); //blah,blah,blah...
The error I get is:
javax.naming.NameNotFoundException; remaining name 'jdbc/myds' at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:500) at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:531) at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:531) at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:546) at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:112) at javax.naming.InitialContext.lookup(InitialContext.java:409) at com.google.android.gcm.demo.server.Datastore.getDevices(Datastore.java:98)
How to get Jetty to find the database code?