【问题标题】:JNDI lookup failing with Jetty for JDBC connection pooling with MySQL?Jetty 使用 MySQL 进行 JDBC 连接池的 JNDI 查找失败?
【发布时间】:2014-10-06 13:01:24
【问题描述】:

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

<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; remaining name 'jdbc/DataSource'。我在代码初始化中尝试了许多不同的字符串变体,例如删除InitialContext 对象上的lookup 调用,但我只是不断收到具有不同name 值的相同错误的变体;

这两个 xml 文件都位于我的 /WAR/WEB-INF 目录中。我查看了大量以前的问题和教程、博客等,但我无处可去。

【问题讨论】:

  • 您能否发布您的 Jetty Embedded 实例的 WebAppContext.setConfiguration() 代码?
  • 我得到了我在其他地方寻找的答案,我会发布一个总结解决方案的答案。

标签: java jdbc jetty jndi embedded-jetty


【解决方案1】:

这是嵌入式 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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多