![](/img/trans.png)
[英]JNDI lookup failing with Jetty for JDBC connection pooling with MySQL?
[英]jetty mysql database connection pooling
我正在尝试做一个看似简单的事情:建立一个小的数据库连接池,以便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>
当我使用java -jar start.jar -port=8080
启动Jetty时,一切正常,直到我尝试从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...
我得到的错误是:
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)
如何让Jetty查找数据库代码?
就我而言,我去了码头邮件列表( https://dev.eclipse.org/mailman/listinfo/jetty-users )问我一个问题。
一个非常聪明的人(嗨,扬!)这样写道和回答:
通常,有许多不同的方法可以实现码头的相同结果,所以这可能就是为什么您看到了不同的文献记载的原因。 通常拥有选择是一件好事
如果有任何混淆,请访问以下官方页面: 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>
在对bindToENC的调用中,将要查找的任何名称绑定为java:comp / env的后缀。 例如,如果您要查找java:comp / env / my / foo,则bindToENC参数为“ my / foo”。
-Jan Bartel www.webtide.com –来自Jetty&CometD专家的开发人员建议,服务和支持。 _________________________________________________码头使用者邮件列表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
因此,将该页面上显示的根元素包裹在我发布的代码段中,您应该会很好。
现在,/ that /获得了要尝试的数据库连接。 然后我connection refused from 127.0.0.1
/var/log/auth.log中的connection refused from 127.0.0.1
错误中被connection refused from 127.0.0.1
。
事实证明,我运行了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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.