jndi链接mysql缺点,使用Jet的JNDI查找失败与MySQL建立JDBC连接池?

这篇博客讲述了在使用Jetty9.2内嵌式服务器时,配置和初始化MySQL数据源遇到的困扰。作者发现启动代码中JNDI查找发生在服务器启动之前,并且必须在WebApp的关联线程中进行。解决方案是将JNDI查找的代码放在首次请求数据库连接时的静态块中,确保在正确的时间和上下文中执行。最终,作者成功地设置了数据库连接池。
摘要由CSDN通过智能技术生成

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:

JDBC Data Source

jdbc/DataSource

javax.sql.DataSource

Container

...and this in my jetty-env.xml:

jdbc/DataSource

jdbc:mysql://localhost:3306/DBName

user

pass

...and this code to initialize:

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

DataSource datasource = (DataSource) envCtx.lookup("jdbc/DataSource");

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;

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.

解决方案

It was a combination of problems specific to embedded Jetty.

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.

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

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值