Ken*_*rey 5 java mysql database servlets jetty
我正在尝试做一些看似简单的事情:设置一个小的数据库连接池,这样一个servlet(只有一个)不会以每秒1个连接创建我的服务器.
在Ubuntu 10.04,使用最新的升级/更新,我正在使用Eclipse Jetty 8.1.5.v20120716.我正在尝试连接到MySQL 5.1服务器.
我的servlet被调用gcm,所以${jetty}/contexts,我有这个gcm.xml文件:
<?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>
Run Code Online (Sandbox Code Playgroud)
当我启动Jetty时java -jar start.jar -port=8080,一切都会正常,直到我尝试从我的servlet中访问数据库.请求的代码处理部分如下所示:
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...
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
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)
Run Code Online (Sandbox Code Playgroud)
如何让Jetty找到数据库代码?
就我而言,我去了码头邮件列表(https://dev.eclipse.org/mailman/listinfo/jetty-users)来询问我的问题.
一个非常聪明的人(Hi Jan!)写道并回答如下:
通常有许多不同的方法来实现码头中相同的结果,所以这可能就是为什么你看到了不同的记录方式.一般来说,选择是一件好事
如果有任何混淆,这是最终页面:http: //wiki.eclipse.org/Jetty/Feature/JNDI如果缺少某些内容,请告知我们,以便我们更新页面.
您的案例有点不同寻常,因为您使用的是javaee样式的功能,但没有web.xml.这不是我以前遇到过的,但这听起来像我应该添加到文档中的东西.
我建议(如上页所述)将任何jndi定义放入WEB-INF/jetty-env.xml文件而不是上下文xml文件.如果您这样做,那么您可以有效地执行web.xml元素将执行的操作,并通过定义资源并将其绑定到java:comp/env一次性将数据源绑定到java:comp/env命名空间(请注意这一点将不是在上下文中的XML文件时,它必须是码头-env.xml):
所以在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>
Run Code Online (Sandbox Code Playgroud)
在对bindToENC的调用中,绑定要查找的任何名称作为java:comp/env的后缀.例如,如果你想查找java:comp/env/my/foo,那么bindToENC参数将是"my/foo".
- Jan Bartel www.webtide.com - 来自Jetty&CometD专家的开发人员建议,服务和支持._______________________________________________ jetty-users邮件列表jetty-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/jetty-users
现在,这还不够(我是一个码头新手).提到的WEB-INF是我的servlet的war文件中的WEB-INF ...所以我将war文件的内容解压缩到一个目录中,然后我可以轻松地将jetty-env.xml文件放在WEB中-INF目录.
这也很重要:
好吧,我在那里假设了一点知识,并且认为你知道我发布的内容只是你需要拥有的一个完整的jetty-env.xml文件的片段.
jetty-env.xml文件的wiki页面位于:http://wiki.eclipse.org/Jetty/Reference/jetty-env.xml
所以在我发布的代码片段周围包装该页面上显示的根元素,你应该很好.
现在/那/已经尝试了数据库连接.然后我connection refused from 127.0.0.1在/var/log/auth.log 中遇到错误.
事实证明我有denyhosts运行,当然有人放在ALL: 127.0.0.1那里......这意味着与我的本地机器的连接被拒绝.
一旦修复了这个细节,servlet最终可以在我的服务器上访问MySQL.
尝试将其添加到 web.xml
<resource-ref>
<res-ref-name>jdbc/myds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9732 次 |
| 最近记录: |