在 Spring JDBC 中通过 JNDI 获取 JDBC 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3672794/
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
Getting a JDBC connection by JNDI in Spring JDBC
提问by Synesso
This page on Spring JDBCsays
The DataSourceUtils class … provides static methods to obtain connections from JNDI
DataSourceUtils 类……提供了从 JNDI 获取连接的静态方法
However the API doc for DataSourceUtilsdoes not include the said static methods, as far as I can see.
但是,据我所知,DataSourceUtils 的 API 文档不包括上述静态方法。
What am I missing?
我错过了什么?
采纳答案by Pascal Thivent
Hmm... Somehow, the Javadoc of DataSourceUtilsis more "accurate" (I mean, the doc is not wrong but technically, you obtain a connection from a DataSource - that can be obtained via JNDI):
嗯......不知何故,JavadocDataSourceUtils更“准确”(我的意思是,该文档没有错,但从技术上讲,您从数据源获得连接 - 可以通过 JNDI 获得):
Helper class that provides static methods for obtaining JDBC Connections from a
DataSource.
Helper 类,提供用于从
DataSource.
And the following methods should be what you're looking for:
以下方法应该是您正在寻找的:
Basic example (from the MySQL documentation):
基本示例(来自MySQL 文档):
// Create a new application context. this processes the Spring config
ApplicationContext ctx = new ClassPathXmlApplicationContext("ex1appContext.xml");
// Retrieve the data source from the application context
DataSource ds = (DataSource) ctx.getBean("dataSource");
// Open a database connection using Spring's DataSourceUtils
Connection c = DataSourceUtils.getConnection(ds);
try {
// retrieve a list of three random cities
PreparedStatement ps = c.prepareStatement(
"select City.Name as 'City', Country.Name as 'Country' " +
"from City inner join Country on City.CountryCode = Country.Code " +
"order by rand() limit 3");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
String city = rs.getString("City");
String country = rs.getString("Country");
System.out.printf("The city %s is in %s%n", city, country);
}
} catch (SQLException ex) {
// something has failed and we print a stack trace to analyse the error
ex.printStackTrace();
// ignore failure closing connection
try { c.close(); } catch (SQLException e) { }
} finally {
// properly release our connection
DataSourceUtils.releaseConnection(c, ds);
}
回答by gpeche
As I understand, what would be really useful to you is JndiObjectFactoryBean. This Spring factory bean returns object published in JNDI.
据我了解,对您真正有用的是JndiObjectFactoryBean。这个 Spring 工厂 bean 返回在 JNDI 中发布的对象。
You would configure it like this, and then you would get the Connectionusing DataSourceUtilson the injected DataSource:
你会像这样配置它,然后你会得到Connection使用DataSourceUtils注入的DataSource:
<bean name="myDataSourceInJndi" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/MyDataSource</value>
</property>
</bean>
<bean name="myBean" class="MyClass">
...
<property name="dataSource" ref="myDataSourceInJndi">
...
</bean>
回答by molino.bruno
For further future references: the jndi is managed on the application server i.e. : standalone.xml (Wildfly) with Postgresql driver:
进一步参考:jndi 在应用程序服务器上管理,即:standalone.xml (Wildfly) 和 Postgresql 驱动程序:
<datasource jta="true" jndi-name="java:comp/env/jdbc/MyDataSource" pool-name="myDS" enabled="true">
<connection-url>jdbc:postgresql:[<//host>[:<5432>/]]<database></connection-url>
<driver>postgresql</driver>
<security>
<user-name>$USER</user-name>
<password>$PWD</password>
</security>
</datasource>

