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

jotm跨数据库事务 spring + hibernate
jotm跨数据库 spring + hibernate


软件包
ow2-jotm-dist-2.1.9-bin.tar.gz
spring-framework-3.0.5.RELEASEhibernate-3.6.0.Final
hibernate-3.6.0.Final

spring和hibernate不再详述,讲讲jotm如何与spring集成
web开发中,使用hibernate的SessionFactory,而在SessionFactory中注入DataSource
DataSource需要支持事务,因此采用软件包ow2-jotm-dist-2.1.9-bin.tar.gz中的xapool.jar包来配置DataSource,放弃了org.springframework.jdbc.datasource.DriverManagerDataSource

配置jta事务管理器JtaTransactionManager
重点<property name="userTransaction" ref="jotm" />
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
		<property name="allowCustomIsolationLevels" value="true"></property>
	</bean>



数据源配置(DataSource)
重点<property name="transactionManager" ref="jotm" />
//1号数据库
<bean id="dataSource1" 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="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf-8" />
            </bean>
        </property>
        <property name="user" value="root" />
        <property name="password" value="" />
    </bean>

//2号数据库
<bean id="dataSource2" 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="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/db2?characterEncoding=utf-8" />
            </bean>
        </property>
        <property name="user" value="root" />
        <property name="password" value="" />
    </bean>


SessionFactory配置
重点<property name="dataSource" ref="dataSource1" />
重点<property name="dataSource" ref="dataSource2" />
//1号SessionFactory
<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	    <property name="hibernateProperties">
	    	<props>
	    		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
	    	</props>
	    </property>
	   	<property name="dataSource" ref="dataSource1" />
		<property name="packagesToScan" value="com.yuce.test.entity"/>
	</bean>

//2号SessionFactory
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	    <property name="hibernateProperties">
	    	<props>
	    		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
	    	</props>
	    </property>
	   	<property name="dataSource" ref="dataSource2" />
		<property name="packagesToScan" value="com.yuce.test.entity"/>
	</bean>


附录
jotm必须的jar包
xapool.jar
jotm-core.ja