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.
分两个逻辑部分,首先你必须在Tomcat里定义JNDI的DataSource,然后你必须告诉 iBatis通过JNDI名称使用那个对象。
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.
设置JNDI数据源取决于tomcat的版本,我们用Tomcat5.5作为例子。通常需要参考Tomcat文档在不同版本的tomcat上设置数据源。
You declare JNDI DataSource objects in Tomcat using the Resource xml
element. Here's an example:
用Resource元素定义JNDI数据源。这是一个例子:
<Resource name="jdbc/AS400B"
type="javax.sql.DataSource"
password="password"
driverClassName="com.ibm.as400.access.AS400JDBCDriver"
maxIdle="2"
maxWait="5000"
validationQuery="select * from PODATA"
username="userid"
url="jdbc:as400:AS400B/RPTSTEXTDB;naming=system;date format=iso;time format=hms;prompt=false"
maxActive="4"/>
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).
如果想把JNDI数据源定义在全局,需要修改conf/context.xml,加入ResourceLink。
Here's a ResourceLink
example:
<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:
iBatis的sqlmap配置文件中,你需要这样设置数据源
<transactionManager type="JDBC">
<dataSource type="JNDI">
<property name="DataSource" value="java:/comp/env/jdbc/AS400B"/>
</dataSource>
</transactionManager>
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:
Spring的配置需要如下,先用JNDI定义数据源,然后注入到SqlMapClientFacotryBean的dataSource属性里。
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:/comp/env/jdbc/AS400B</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>