mercredi 15 juin 2016

jetty mysql database connection pooling

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 using Eclipse Jetty 8.1.5.v20120716. I'm trying to connect to a MySQL 5.1 server.

My servlet is called gcm, so in ${jetty}/contexts, I have this gcm.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?

Aucun commentaire:

Enregistrer un commentaire