My configuration files
project/WEB-INF/web.xml:
ConnectionPool DataSource Reference
jdbc/mysql
javax.sql.DataSource
Container
project/WEB-INF/jetty-env.xml:
jdbc/mysql
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/db
user
pwd
50
code to invoke:
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
con=ds.getConnection();
scripts to start jetty:
java -DOPTIONS=plus -jar start.jar
java -jar start.jar
Either way to start jetty, I got following error:
javax.naming.NameNotFoundException; remaining name 'env/jdbc/mysql'
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:632)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:663)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:678)
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:110)
at javax.naming.InitialContext.lookup(Unknown Source)
The questions are:
what is the problem here?
Any other configurations needed?
where to put following jar files:
commons-dbcp-1.2.2.jar
mysql-connector-java-5.1.10-bin.jar
Thank you!
解决方案
Note: The following assumes you're using Jetty 7.2.2+ (probably works with any Jetty 7.0+).
The problem is you're missing ONE extra layer of indirection. Even though you've configured Jetty in your webapp, you still need to tell the Jetty container that it needs to look for jetty-env.xml in your webapp. To do that, add the following to your jetty.xml (under ${JETTY_INSTALL_DIR}/etc:
org.eclipse.jetty.webapp.configuration
org.eclipse.jetty.webapp.WebInfConfiguration
org.eclipse.jetty.webapp.WebXmlConfiguration
org.eclipse.jetty.webapp.MetaInfConfiguration
org.eclipse.jetty.webapp.FragmentConfiguration
org.eclipse.jetty.plus.webapp.EnvConfiguration
org.eclipse.jetty.plus.webapp.PlusConfiguration
org.eclipse.jetty.webapp.JettyWebXmlConfiguration
org.eclipse.jetty.webapp.TagLibConfiguration
NOW Jetty will know to parse the jetty-env.xml you've created.