栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用MySQL进行JDBC连接池的Jetty时,JNDI查找失败?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用MySQL进行JDBC连接池的Jetty时,JNDI查找失败?

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

首先,在我实际启动Web服务器之前(即在调用之前),正在配置和启动Web服务器的启动器代码正在执行JNDI查找

server.start()
,因此在该阶段未初始化JNDI配置。

但是,即使进行此更改也不起作用,因为

envCtx.lookup("jdbc/DataSource")
需要从与WebApp关联的线程中进行调用。因此,我将该代码移到了静态块,该静态块在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查找,因此将其放置在类似于

init
servlet请求的方法的地方,或者像我一样,将其放置在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);        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号