Spring JNDI Datasource Configuration in JBOSS

In this article let's discuss about configuring a JNDI Datasource from JBOSS in a Spring application.

Let's first create the following oracle-ds.xml (assume the db is Oracle) XML file.

oracle-ds.xml

<datasources>

<local-tx-datasource>

    <jndi-name>jdbc/OracleDS</jndi-name>

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

    <connection-url>jdbc:oracle:thin:@10.1.1.1:1521:orcl</connection-url>

    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>

    <min-pool-size>2</min-pool-size>

    <max-pool-size>10</max-pool-size>

    <user-name>scott</user-name>

    <password>tiger</password>

</local-tx-datasource>

</datasources>

The above file creates a Datasource referred by the JNDI name "OracleDS" during runtime. This file should be placed in the JBOSS-HOME/server/default/deploy directory.

Now in any Spring application, we can get the Datasource by configuring the beans in Spring context file.

spring-dao.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/OracleDS"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<constructor-arg ref="dataSource"/>

</bean>

</beans>


In the tag "jee:jndi-lookup", the jndi-name specified in the oracle-ds.xml file should be given. This Datasource can be used by the other DAO beans in the application.

Copy ojdbc14.jar in JBOSS-HOME\server\default\lib directory.

In this way, Datasources for other databases can be configured in JBOSS and used in Spring applications using JNDI.

Technology: 

Search