如何使用 JNDI 将 Java 应用程序连接到数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24456377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect java application to database using JNDI?
提问by CuriousMind
I am implementing JNDI concept to get a connection to Database. I googled to get the starting point, however didn't get it.
我正在实施 JNDI 概念以连接到数据库。我用谷歌搜索以获得起点,但没有得到它。
The things that i want to do is have a simple java standalone application which used JNDI concept for getting connected to a database.
我想要做的事情是有一个简单的 java 独立应用程序,它使用 JNDI 概念来连接到数据库。
Sample source that i have is:
我拥有的示例源是:
DataSource dataSource = null;
Context context = null;
try {
context = new InitialContext();
dataSource = (DataSource) context.lookup("database_connection");
}
catch (NamingException e) {
System.out.println("Got naming exception, details are -> " + e.getMessage());
}
Now, where we define database_connection? Is that defined in an xml file, and if so where do we specify that and what format of that is?
现在,我们在哪里定义 database_connection?它是否在 xml 文件中定义,如果是,我们在哪里指定它以及它的格式是什么?
If any pointers can be provided, that would be great.
如果可以提供任何指针,那就太好了。
Thanks
谢谢
采纳答案by user1676075
The real difference between your question and the examples is that you're using a standalone Java application. Pretty-much every example assumes that you're running within a Java EE application container. In that case you define the database connection to the container, use JNDI to get the DataSource, and get the connection from that.
您的问题与示例之间的真正区别在于您使用的是独立的 Java 应用程序。几乎每个示例都假设您在 Java EE 应用程序容器中运行。在这种情况下,您定义与容器的数据库连接,使用 JNDI 获取数据源,并从中获取连接。
When you do a JNDI lookup to get your DataSource, the JNDI framework looks for an initial context factory (a class that implements InitialContextFactory). In your Java EE application, the container provides that. In your standalone Java application, the initial context is null, and no further lookups resolve correctly.
当您执行 JNDI 查找以获取数据源时,JNDI 框架会查找初始上下文工厂(实现 InitialContextFactory 的类)。在您的 Java EE 应用程序中,容器提供了这一点。在您的独立 Java 应用程序中,初始上下文为空,并且没有进一步的查找正确解析。
One of the ways you can resolve this is to create your own initial context factory:
解决此问题的方法之一是创建自己的初始上下文工厂:
public class MyContextFactory implements InitialContextFactory
and inject that into JNDI at startup:
并在启动时将其注入 JNDI:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "mypackag.MyContextFactory");
and then return a new ObjectFactory from the getInitialContext call, and then implement the ObjectFactory to return a DataSource (also probably custom) from the getConnection() call.
然后从 getInitialContext 调用返回一个新的 ObjectFactory,然后实现 ObjectFactory 以从 getConnection() 调用返回一个 DataSource(也可能是自定义的)。
That would all work, but it's overkill. It would be easier to just use the normal JDBC connection string approach to get a connection directly in your application rather than trying to use a DataSource. Or use a framework like Spring to inject it for you or to separate the database connect information from the code (in which case you're using Spring configuration files rather than JNDI directly).
这一切都会奏效,但太过分了。使用普通的 JDBC 连接字符串方法直接在您的应用程序中获取连接会更容易,而不是尝试使用数据源。或者使用 Spring 之类的框架为您注入它或将数据库连接信息与代码分开(在这种情况下,您将使用 Spring 配置文件而不是直接使用 JNDI)。
The one reason I'd advocate creating a custom context factory and data source approach is if you have common JPA code that you want to run both within a Java EE app and within a standalone app (it's otherwise non-trivial to configure the same code to do both). That doesn't sound like your case.
我提倡创建自定义上下文工厂和数据源方法的一个原因是,如果您有想要在 Java EE 应用程序和独立应用程序中运行的通用 JPA 代码(否则配置相同的代码很重要)两者都做)。这听起来不像你的情况。
So, since you're standalone and not in a Java EE container, I think your real answer is that your use case is not appropriate for a DataSource (unless you move to a framework like Spring that would provide one).
因此,由于您是独立的而不是在 Java EE 容器中,我认为您真正的答案是您的用例不适合 DataSource(除非您转向像 Spring 这样的框架会提供一个)。
回答by Mike
There's a nice tutorial for that hereand here
The database should be defined somewhere (LDAP)? There's a series of old articles using the directory service with JNDI (ultimately in your case to get the server information) here.
数据库应该在某处定义(LDAP)?有一系列使用JNDI与目录服务(最终在你的情况下,获取服务器信息)旧物品的位置。
A nice introduction to naming services here
这里有一个很好的命名服务介绍