使用Jet的JNDI查找失败与MySQL建立JDBC连接池? [英] JNDI lookup failing with Jetty for JDBC connection pooling with MySQL?

查看:216
本文介绍了使用Jet的JNDI查找失败与MySQL建立JDBC连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有标准MySQL连接器API的Jetty 9.2(嵌入式),我对如何设置它感到困惑。目前,我在 web.xml 文件中有这个:

I'm using Jetty 9.2 (embedded) with the standard MySQL connector API and I'm very confused by how this is supposed to be setup. Currently, I have this in my web.xml file:

<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>

...这在我的jetty-env.xml中:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

   <New id="DatabaseConnector" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/DataSource</Arg>
        <Arg>
            <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                <Set name="Url">jdbc:mysql://localhost:3306/DBName</Set>
                <Set name="User">user</Set>
                <Set name="Password">pass</Set>
            </New>
        </Arg>
    </New>

 </Configure>

...此代码初始化:

Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
DataSource datasource = (DataSource) envCtx.lookup("jdbc/DataSource");

当我尝试启动服务器时,我收到错误 javax。 naming.NameNotFoundException;剩余名称'jdbc / DataSource'。我在代码初始化中尝试了很多不同的字符串变体,比如在 InitialContext上删除 lookup 调用对象,但我只是使用不同的名称值继续得到相同错误的变体;

When I try to fire up the server, I get the error javax.naming.NameNotFoundException; remaining name 'jdbc/DataSource'. I've tried lots of different variations of the strings in the code initialization, like removing the lookup call on the InitialContext object, but I just keep getting variations of the same error with a different name value;

两个xml文件位于我的 / WAR / WEB-INF 目录中。我看过很多以前的问题和教程,博客等,但我无处可去。

Both of the xml files are located in my /WAR/WEB-INF directory. I've looked at loads of previous questions and tutorials, blogs etc. but I'm getting nowhere.

推荐答案

这是一个嵌入式Jetty特有的问题组合。

It was a combination of problems specific to embedded Jetty.

首先,配置和启动Web服务器的启动器代码在我实际启动Web服务器之前进行JNDI查找,即在调用服务器之前.start(),因此JNDI配置在该阶段未初始化。

First, my launcher code that was configuring and launching the web server was doing the JNDI lookup before I actually started the web server i.e. before calling server.start(), so the JNDI configuration was not initialized at that stage.

但即使进行此更改也不起作用,因为需要从与WebApp关联的线程调用 envCtx.lookup(jdbc / DataSource)。所以我将该代码移动到静态块,在第一次Web服务器请求请求数据库连接时调用该块。

But even making this change didn't work, because the envCtx.lookup("jdbc/DataSource") needs to be called from a thread that's associated with the WebApp. So I moved that code to a static block that gets called the first time a database connection is requested by a web server request.

最后,我最终得到了一些东西对于我的启动器代码是这样的:

In the end, I ended up with something like this for my launcher code:

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查找,所以把它放在像<$ c $这样的地方c> init servlet请求的方法,或者像我所做的那样,servlet使用的静态数据库访问器类的同步方法,例如

The JNDI lookup cannot be made by this main thread, so put it somewhere like the init method of a servlet request, or like I did, a synchronized method of a static database accessor class that gets used by servlets e.g.

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);
        }
    }

这篇关于使用Jet的JNDI查找失败与MySQL建立JDBC连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆