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