"java:comp/env"代表的就是数据库连接池的目录,我要得到一个连接就在这个目录下查找相应的数据源名字。也就是我配置的<Resource name="jdbc/bn" ..../>
tomcat下数据源的目录默认就是这个啊,数据源都在这个目录下:
Context ctcontext=new InitialContext();
Context envcontext=(Context)ctcontext.lookup("java:/comp/env");
ds=(DataSource)envcontext.lookup("jdbc/bn");
这相当于一层一层的查找,知道找到和jdbc/nc相对应的datasource!
在Tomcat 5.5/conf/Catalina/localhost下放的*.xml文件是启动即加载的文件,所以这个配置文件一般用于配置数据源的,配置试例代码如下:
<Context path="/POS" docBase="D:/kmrxt/workspace/server2/WebRoot"
privileged="true" antiResourceLocking="false" antiJARLocking="false">
<Resource auth="Container" description="Oracle Connection" driverClass="oracle.jdbc.driver.OracleDriver" maxPoolSize="4"
minPoolSize="2" acquireIncrement="1" name="jdbc/DefaultDS" user="sjcb" password="sjcb" factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:oracle:thin:@192.168.0.228:1521:dlyx" />
<Resource auth="Container" description="Oracle Connection" driverClass="oracle.jdbc.driver.OracleDriver" maxPoolSize="4"
minPoolSize="2" acquireIncrement="1" name="jdbc/DefaultDSSybase" user="zsdl" password="zsdl" factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:oracle:thin:@192.168.0.228:1521:dlyx" />
</Context>
在spring的配置文件中的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>report</value>
</list>
</property>
</bean>
-->
<bean id="oracleDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/DefaultDS</value>
</property>
</bean>
<bean id="sybaseDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/DefaultDSSybase</value>
</property>
</bean>
<bean id="swappableDataSource" class="org.springframework.aop.target.HotSwappableTargetSource">
<constructor-arg>
<ref local="oracleDataSource"/>
</constructor-arg>
</bean>
<bean id="swappable" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- not config it ,will error 'Cannot subclass final class class' -->
<property name="proxyInterfaces" value="javax.sql.DataSource"/>
<property name="targetSource">
<ref local="swappableDataSource"/>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>/WEB-INF/classes/com/kmr/applicationConfig/sql-map-config.xml</value>
</property>
<property name="dataSource">
<ref bean="swappable" />
</property>
</bean>
<bean id="persistence" class="com.kmr.common.persistence.PersistenceFactory" factory-method="createPersistence">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="KMRPM" class="com.kmr.common.persistence.PersistenceManager">
<property name="persistence">
<ref bean="persistence" />
</property>
<property name="oraDS">
<ref bean="oracleDataSource" />
</property>
<property name="sybDS">
<ref bean="sybaseDataSource" />
</property>
<property name="swappableDataSource">
<ref bean="swappableDataSource" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="swappable" />
</property>
</bean>
<!--
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction">
<ref local="jotm" />
</property>
</bean>
-->
<bean name="buffaloConfigBean" class="net.buffalo.service.BuffaloServiceConfigurer">
<property name="services">
<map>
<entry key="sysBmgxService">
<ref bean="sysBmgxService" />
</entry>
<entry key="eecDzwjService">
<ref bean="eecDzwjService" />
</entry>
</map>
</property>
</bean>
</beans>