java Spring,通过具有远程 JBoss 服务器的 JNDI 配置数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11471679/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:15:31  来源:igfitidea点击:

Spring, configure the DataSource through JNDI having remote JBoss Server

javaspringjakarta-eedatasourcejndi

提问by Talha Ahmed Khan

I want to make DataSourcein Springthrough JNDI. All the configuration are given.

我想要DataSourceSpring通过JNDI。所有的配置都给出了。

Can someone tell me what is wrong with the configuration.

有人能告诉我配置有什么问题吗?

One thing I would like to mention here is that JNDI DS is hosted on JBoss server which does not host the Springapplication.

我想在这里提到的一件事是 JNDI DS 托管在不托管Spring应用程序的JBoss 服务器上。

Configuration

配置

datasource-ds.xml

数据源-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>jdbc/wc-mysql</jndi-name>
    <connection-url>jdbc:mysql://xx.xx.xx.xx:3306/club</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>club</user-name>
    <password>club</password>
    <exception-sorter-class-name>
      org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
    </exception-sorter-class-name>
    <min-pool-size>5</min-pool-size>
    <max-pool-size>20</max-pool-size>
    <use-java-context>false</use-java-context>
    <metadata><type-mapping>mySQL</type-mapping></metadata>
  </local-tx-datasource>
</datasources>

configContext.xml

配置上下文.xml

<bean id="wcDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="jdbc/wc-mysql" />
  <property name="jndiEnvironment">
    <props>
      <prop key="java.naming.provider.url">jnp://yy.yy.yy.yy:1099</prop>
      <!-- 
      <prop key="java.naming.factory.initial">
        org.springframework.mock.jndi.SimpleNamingContextBuilder
      </prop>
      <prop key="java.naming.factory.url.pkgs">yourPackagePrefixesGoHere</prop> -->
      <!-- other key=values here -->
    </props>
  </property>
  <!-- other properties here-->
</bean>

Exception

例外

Caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
        at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
        at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at org.springframework.jndi.JndiTemplate.doInContext(JndiTemplate.java:154)

采纳答案by Talha Ahmed Khan

I have made few changes and Its now working fine.

我做了一些更改,现在工作正常。

A JNDI Template must be initialized with the JNP properties. And URL to JBoss server has to be in that properties.

JNDI 模板必须使用 JNP 属性进行初始化。JBoss 服务器的 URL 必须在该属性中。

configContext.xml

配置上下文.xml

<bean id="wcJndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
            <prop key="java.naming.provider.url">jnp://jndi.myURL.me:1099</prop>
            <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
            <prop key="jnp.disableDiscovery">true</prop>
        </props>
    </property>
</bean>

<bean id="wcDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/wc-mysql"/>
    <property name="resourceRef" value="false"/>
    <property name="jndiTemplate" ref="wcJndiTemplate" />
</bean>

But after doing that changes I was facing an exception

但是在做了这些改变之后,我遇到了一个例外

java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory

So I found a link mentioning to include a dependency of jbossall-client.jarin the POM to resolve the issue. So the pom changes are

所以我发现一个链接提到jbossall-client.jar在 POM 中包含一个依赖项来解决这个问题。所以 pom 的变化是

<dependency>
    <groupId>jboss</groupId>
    <artifactId>jbossall-client</artifactId>
    <version>4.2.2.GA</version>
</dependency>

Every thing seems to be working fine.

一切似乎都运转良好。

Thanks.

谢谢。

回答by Suave Nti

There is no problem with your remote JNDIas this line

你的遥控器没有问题,JNDI因为这条线

 <use-java-context>false</use-java-context>

will take care of it.

会照顾它。

You have problem with your JNDI name value:

您的 JNDI 名称值有问题:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

Change this in your applicationConfig.xml

在你的 applicationConfig.xml

<property name="jndiName" value="jdbc/wc-mysql" />

to

<property name="jndiName" value="java:/jdbc/wc-mysql"></property>

It should work

它应该工作