日期:2014-05-16  浏览次数:20387 次

Spring---多个数据库的事务操作配置(JNDI)

一,当有记录需要操作两个以上数据库时,这时就会引发事务问题,,,jndi能解决此类问题

context.xml

<Context path="/spring-all" docBase="spring-all" debug="5" reloadable="true"
	crossContext="true">

	<Resource name="jdbc/mysql" auth="Container"
		type="javax.sql.DataSource" maxActive="100" maxIdle="50"
		maxWait="10000" 
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://127.0.0.1:3306/orange1"
		username="root"
		password="kgddxsksk"
		testOnBorrow="true" testWhileIdle="true"/>	
		
		<Resource name="jdbc/sqlserver" auth="Container"
		type="javax.sql.DataSource" maxActive="100" maxIdle="50"
		maxWait="10000" 
		driverClassName="net.sourceforge.jtds.jdbc.Driver"
		url="jdbc:jtds:sqlserver://192.168.7.83:1433/test"
		username="sa"
		password="kgddxsksk"
		testOnBorrow="true" testWhileIdle="true"/>	
</Context>

?

二,spring中的事务配置

?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

	<!-- 
		<bean id="dataSource1"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
		<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
		<value>jdbc:mysql://127.0.0.1:3306/orange1</value>
		</property>
		<property name="username">
		<value>root</value>
		</property>
		<property name="password">
		<value>kgddxsksk</value>
		</property>
		</bean>
		
	-->
	<jee:jndi-lookup id="dataSource1"
		jndi-name="java:/comp/env/jdbc/mysql">
	</jee:jndi-lookup>
	
	<jee:jndi-lookup id="dataSource2"
		jndi-name="java:/comp/env/jdbc/sqlserver">
	</jee:jndi-lookup>
	
	<bean id="jotm"
		class="com.spring.web.controller.JotmFactoryBean" />
	<!-- 
		当使用jta时,说明使用隔离级别
	-->
	<bean id="txManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="allowCustomIsolationLevels" value="true" />
		<property name="userTransaction">
			<ref local="jotm" />
		</property>
	</bean>

	<!-- "person" table in mysql and "user" table in sqlserver -->
	<bean id="person" class="com.spring.web.model.Person"></bean>
	<bean id="personDao" class="com.spring.web.dao.PersonDao">
		<property name="dataSource" ref="dataSource1"></property>
	</bean>
	<bean id="user" class="com.spring.web.model.User"></bean>
	<bean id="userDao" class="com.spring.web.dao.UserDao">
		<property name="dataSource" ref="dataSource2"></property>
	</bean>

	<bean id="userService"
		class="com.spring.web.services.UserService">
		<property name="userDao" ref="userDao"></property>
		<property name="personDao" ref="personDao"></property>
	</bean>

	<!-- 事务配置 -->
	<!-- 非检查型(RuntimeException)默认才会回滚 -->
	<!-- 
		对txManager进行了事务控制,,,所以在代码中使用持久化api时,也要是定制的,如
		JdbcTemplate、HibernateTemplate和JdoTemplate,,,也可用相应的
		DataSourceUtils(针对JDBC),SessionFactoryUtils(针对Hibernate),PersistenceManagerFactoryUtils(针对JDO)
		因为这些类都会引用mana