spring+hibernate+jotm demo

准备工作
hibernate3
spring2
jotm2.1.9

2个oracle数据源

目标:在同一个service方法内,分别操作2个数据源的数据,任何一个发生错误,全部回滚

1、spring配置
1.1、jta《jotm》配置,所需jar包【spring.jar】
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

	<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>
	<!-- 声明式事物,service方法必须声明@Transactional 才能使用jta的事物管理,否则就会独立开来,必须jar包【cglib.jar】-->
	<tx:annotation-driven transaction-manager="txManager"
		proxy-target-class="true" />

1.2、dataSource配置,所需jar包【xapool.jar,  jotm-core.jar,   carol.jar,   carol-interceptor.jar】
<bean id="dataSourceA"
		class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
		destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
				destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName"
					value="oracle.jdbc.driver.OracleDriver" />
				<property name="url"
					value="jdbc:oracle:thin:@192.168.3.122:1521:LXHORCL" />
			</bean>
		</property>
		<property name="user" value="ADS" />
		<property name="password" value="ADS" />
	</bean>

	<bean id="dataSourceB"
		class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
		destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
				destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName"
					value="oracle.jdbc.driver.OracleDriver" />
				<property name="url"
					value="jdbc:oracle:thin:@192.168.7.13:1521:LWORCL" />
			</bean>
		</property>
		<property name="user" value="ADS" />
		<property name="password" value="ADS" />
	</bean>

1.3、hibernate sessionFactory配置
<bean id="sessionFactoryA"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSourceA" />
		</property>
		<property name="mappingResources">
			<list>
				<value>META-INF/hbm-A.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.OracleDialect
				</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.cglib.use_reflection_optimizer">
					true
				</prop>
				<prop key="hibernate.cache.provider_class">
					org.hibernate.cache.HashtableCacheProvider
				</prop>
				<prop key="hibernate.jdbc.bach_size">10</prop>
				<prop key="hibernate.jdbc.use_query_cache">true</prop>
			</props>
		</property>
	</bean>


	<bean id="sessionFactoryB"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSourceB" />
		</property>
		<property name="mappingResources">
			<list>
				<value>META-INF/hbm-B.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.OracleDialect
				</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.cglib.use_reflection_optimizer">
					true
				</prop>
				<prop key="hibernate.cache.provider_class">
					org.hibernate.cache.HashtableCacheProvider
				</prop>
				<prop key="hibernate.jdbc.bach_size">10</prop>
				<prop key="hibernate.jdbc.use_query_cache">true</prop>
			</props>
		</property>
	</bean>


好了,到这里基本上完成了基本的配置

1.5、具体的功能

<!-- BASE -->
	<bean id="baseDaoA" class="com.hmf.alex.simple.dao.BaseDao">
		<property name="sessionFactory">
			<ref local="sessionFactoryA" />
		</property>
	</bean>

	<bean id="baseDaoB" class="com.hmf.alex.simple.dao.BaseDao">
		<property name="sessionFactory">
			<ref local="sessionFactoryB" />
		</property>
	</bean>

	<!-- FUNCTION -->
	<bean id="userDaoA" parent="baseDaoA" lazy-init="true">
		<property name="clazz">
			<value>com.hmf.alex.simple.entities.User</value>
		</property>
	</bean>

	<bean id="userDaoB" parent="baseDaoB" lazy-init="true">
		<property name="clazz">
			<value>com.hmf.alex.simple.entities.User</value>
		</property>
	</bean>

	<bean id="userService"
		class="com.hmf.alex.simple.service.UserService" lazy-init="true">
		<property name="daoA">
			<ref bean="userDaoA" />
		</property>
		<property name="daoB">
			<ref bean="userDaoB" />
		</property>
	</bean>

2、测试
public class UserService {
	private IDao daoA;

	private IDao daoB;

	.....
	@Transactional     
	public void createAB(Object a ,Object b){
		this.create(a, "A");
		this.create(b, "B");
	}
	
	public Serializable create(Object o ,String type){
		if("B".equals(type)){
			return daoB.create(o);
		}
		return daoA.create(o);
	}
}

你可能感兴趣的:(java,spring,oracle,Hibernate,配置管理)