简体   繁体   中英

Spring, configure the DataSource through JNDI having remote JBoss Server

I want to make DataSource in Spring through JNDI. All the configuration are given.

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 Spring application.

Configuration

datasource-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

<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$1.doInContext(JndiTemplate.java:154)

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.

configContext.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.jar in the POM to resolve the issue. So the pom changes are

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

Every thing seems to be working fine.

Thanks.

There is no problem with your remote JNDI as this line

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

will take care of it.

You have problem with your JNDI name value:

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

Change this in your applicationConfig.xml

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

to

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

It should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM