Java JNDI查找在Jetty中失败,JDBC连接池使用MySQL?
我正在使用Jetty 9.2(嵌入式)和标准的MySQL连接器API,我对如何设置这个接口感到非常困惑。目前,我的web.xml文件中有以下内容:Java JNDI查找在Jetty中失败,JDBC连接池使用MySQL?,java,jdbc,jetty,jndi,embedded-jetty,Java,Jdbc,Jetty,Jndi,Embedded Jetty,我正在使用Jetty 9.2(嵌入式)和标准的MySQL连接器API,我对如何设置这个接口感到非常困惑。目前,我的web.xml文件中有以下内容: <webapp ... <resource-ref> <description>JDBC Data Source</description> <res-ref-name>jdbc/DataSource</res-ref-name>
<webapp ...
<resource-ref>
<description>JDBC Data Source</description>
<res-ref-name>jdbc/DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</webapp>
当我尝试启动服务器时,我得到了错误javax.naming.NameNotFoundException;剩余名称“jdbc/DataSource”
。我在代码初始化中尝试了许多不同的字符串变体,比如删除InitialContext
对象上的lookup
调用,但我只是不断地用不同的名称
值得到相同错误的变体
这两个xml文件都位于我的
/WAR/WEB-INF
目录中。我已经看了很多以前的问题、教程、博客等,但我什么也没有得到。这是嵌入式Jetty特有的问题组合
首先,我配置和启动web服务器的启动程序代码在我实际启动web服务器之前(即调用server.start()
)进行JNDI查找,因此JNDI配置在该阶段没有初始化
但是,即使做出这样的更改也不起作用,因为需要从与WebApp关联的线程调用envCtx.lookup(“jdbc/DataSource”)
。因此,我将代码移动到一个静态块中,该块在web服务器请求第一次请求数据库连接时被调用
最后,我的启动程序代码得到了类似的结果:
public static void main(String[] args) {
Server server = new Server();
//Enable parsing of jndi-related parts of web.xml and jetty-env.xml
ClassList classlist = ClassList.setServerDefault(server);
classlist.addAfter(
"org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.plus.webapp.PlusConfiguration");
...
...
server.start();
这个主线程无法进行JNDI查找,所以将它放在类似servlet请求的init
方法的地方,或者像我做的那样,放在servlet使用的静态数据库访问器类的同步方法的地方,例如
public class DatabaseUtils {
private static DataSource datasource;
private static synchronized Connection getDBConnection() throws SQLException {
if (datasource == null) {
initDataSource();
}
return datasource.getConnection();
}
public static void initDataSource() {
try {
datasource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/DataSource");
LOG.info("Database connection pool initalized successfully");
} catch (Exception e) {
LOG.error("Error while initialising the database connection pool", e);
}
}
您可以发布Jetty嵌入式实例的
WebAppContext.setConfiguration()
代码吗?我在其他地方找到了我想要的答案,我将发布一个总结解决方案的答案。
public static void main(String[] args) {
Server server = new Server();
//Enable parsing of jndi-related parts of web.xml and jetty-env.xml
ClassList classlist = ClassList.setServerDefault(server);
classlist.addAfter(
"org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.plus.webapp.PlusConfiguration");
...
...
server.start();
public class DatabaseUtils {
private static DataSource datasource;
private static synchronized Connection getDBConnection() throws SQLException {
if (datasource == null) {
initDataSource();
}
return datasource.getConnection();
}
public static void initDataSource() {
try {
datasource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/DataSource");
LOG.info("Database connection pool initalized successfully");
} catch (Exception e) {
LOG.error("Error while initialising the database connection pool", e);
}
}