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>