Dashboard > iBATIS DataMapper > Home > Frequently Asked Questions > How do I use a JNDI DataSource with iBATIS in Tomcat?
How do I use a JNDI DataSource with iBATIS in Tomcat?
Added by Chris Lamey, last edited by Chris Lamey on Sep 27, 2007  (view change)

This breaks down into two logical parts. First you have to declare the
DataSource in JNDI in Tomcat. Then you have to tell iBATIS to use that
object via its JNDI name.

Putting a DataSource into JNDI in Tomcat depends on the version of Tomcat
you have. We'll use Tomcat 5.5 as an example here. Take a look at the
Tomcat docs for specifics on your version or more details in general.

You declare JNDI DataSource objects in Tomcat using the Resource xml
element. Here's an example:

<Resource name="jdbc/AS400B"
    validationQuery="select * from PODATA"
    url="jdbc:as400:AS400B/RPTSTEXTDB;naming=system;date format=iso;time format=hms;prompt=false"

The Resource element either goes in your global JNDI context or in a webapp
specific context. To put it in your global JNDI context, put your
DataSource Resource element as a child of the GlobalNamingResources element
in conf/server.xml. If you put it in the global JNDI context, you need to
use a ResourceLink in either your webapp's context or in the
conf/context.xml (which is shared by all webapps).

Here's a ResourceLink

<ResourceLink name="jdbc/AS400B" global="jdbc/AS400B" type="javax.sql.DataSource"/>

To put it in a webapp specific context depends on how your webapps are set
up. If you have a context file for your webapp, you can put the Resource as
a child of the Context element. If you don't have a context file for you
webapp, you can put the Resource element as a child of the webapp's Context
element in conf/server.xml.

Another place for the Resource element could be in the conf/context.xml
file, which is shared by all webapps.

Once the DataSource is bound in JNDI, you need to tell iBATIS to use it.
NOTE: that Tomcat helpfully prepends the string "java:/comp/env/" to your
names, which is needed to do the lookups.

If you're using the iBATIS sqlmap xml config files, you need this:

<transactionManager type="JDBC">
    <dataSource type="JNDI">
    <property name="DataSource" value="java:/comp/env/jdbc/AS400B"/>

If you're using Spring, you need to reference the JNDI DataSource object as
a JndiObjectFactoryBean and then set that as the DataSource on your
SqlMapClientFactoryBean. Here's an example:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:sqlmap-config.xml"/>
    <property name="dataSource" ref="dataSource"/>

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators