spring+hibernate+atomikos 分布式事务管理

网上有很多的atomikos的分布式事务管理的配置,但是大多数都是同一类型的数据库,并没有跨数据库类型的配置。使用的数据库是Oracle和mysql。

配置文件代码如下:

 

 

<bean id="propertyConfigurer" class="*.*.*.DBConfigurer">
       <property name="order" value="1" />
       <property name="ignoreUnresolvablePlaceholders" value="true" />     
       <property name="location" value="WEB-INF/DBconfig.properties"/> 
    </bean>
    
    <bean id="mysqlDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" 
        init-method="init" destroy-method="close">
        <!-- Set unique name for this DataSource -->  
        <property name="uniqueResourceName">
        	<value>bpm</value>
        </property>
        <!-- Set XADatasource class name-->  
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">
            <props>
                <prop key="user">${connection.username}</prop>
                <prop key="password">${connection.password}</prop>
                <prop key="url">${connection.url}</prop>
            </props>
        </property>
        <!-- set properties for datasource connection pool -->  
        <property name="poolSize" value="3" />
        <!-- 管理 Connection 被占用的时间 -->
        <!-- 如果不设置这个值,Atomikos使用默认的300秒(即5分钟),那么在处理大批量数据读取的时候,一旦超过5分钟,就会抛出类似 Resultset is close 的错误 -->
        <property name="reapTimeout"><value>20000</value></property>  
    </bean>
    
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="mysqlDataSource" />
		<property name="packagesToScan">
			<list>
				<value>smtcl.mocs.pojos.device</value>
				<value>smtcl.mocs.pojos.authority</value>
				<value>smtcl.mocs.pojos.job</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
<!-- 			    <prop key="hibernate.connection.url">${connection.url}</prop> -->
                <prop key="hibernate.connection.driver_class">${connection.driver}</prop>
<!--                 <prop key="hibernate.connection.username">${connection.username}</prop> -->
<!--                 <prop key="hibernate.connection.password">${connection.password}</prop> -->
                <prop key="hibernate.dialect">${connection.dialect}</prop>
                <prop key="hibernate.show_sql">${connection.show.sql}</prop>
                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
			</props>
		</property>
	</bean>

	<!-- atomikos事务管理器 -->
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" 
        init-method="init" destroy-method="close">
        <property name="forceShutdown">
            <value>true</value>
        </property>
    </bean>

    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>

    <!-- spring 事务管理器 -->
    <bean id="springTransactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="atomikosTransactionManager"/>
        <property name="userTransaction" ref="atomikosUserTransaction" />
        <property name="allowCustomIsolationLevels" value="true"/> 
    </bean>

    <!-- 使用annotation定义事务,对于要加入事物的类,只需对该类加 @Transactional  -->
    <tx:annotation-driven transaction-manager="springTransactionManager" />

<!--     hibernate Dao层模板 -->
<!--     <bean id="transactionManager" -->
<!--         class="org.springframework.orm.hibernate3.HibernateTemplate"> -->
<!--         <property name="sessionFactory" ref="sessionFactory"></property> -->
<!--     </bean> -->
    
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

 

<bean id="erpPropertyConfigurer" class="*。*。*.ERPDBConfigurer">
     	<property name="order" value="2" />
     	<property name="ignoreUnresolvablePlaceholders" value="true" />     
    	<property name="location" value="WEB-INF/ERPDBconfig.properties"/> 
    </bean>
    
    <bean id="oracleDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean">
		<property name="uniqueResourceName">
			<value>chh</value>
		</property>
		<property name="xaDataSourceClassName" value="oracle.jdbc.xa.client.OracleXADataSource"/>
		<property name="xaProperties">
            <props>
                <prop key="user">${connection.username}</prop>
                <prop key="password">${connection.password}</prop>
                <prop key="URL">${connection.url}</prop>
            </props>
        </property>
		<property name="poolSize">
			<value>1</value>
		</property>
		<property name="borrowConnectionTimeout">
			<value>60</value>
		</property>
	</bean>

	<bean id="erpSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="oracleDataSource" />
		<property name="packagesToScan">
			<list>
				<value>smtcl.mocs.pojos.erp</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
<!-- 			    <prop key="hibernate.connection.url">${erp.connection.url}</prop> -->
                <prop key="hibernate.connection.driver_class">${erp.connection.driver}</prop>
<!--                 <prop key="hibernate.connection.username">${erp.connection.username}</prop> -->
<!--                 <prop key="hibernate.connection.password">${erp.connection.password}</prop> -->
                <prop key="hibernate.dialect">${erp.connection.dialect}</prop>
                <prop key="hibernate.show_sql">${erp.connection.show.sql}</prop>
                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
			</props>
		</property>
	</bean>

<!-- 	 <bean id="erpTransactionManager" -->
<!--         class="org.springframework.orm.hibernate3.HibernateTemplate"> -->
<!--         <property name="sessionFactory" ref="erpSessionFactory"></property> -->
<!--     </bean> -->

	<bean id="erpTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="erpSessionFactory" />
	</bean>

 分布式事务在连接Oracle数据时,可能会报错,错误信息如下:

javax.transaction.xa.XAException 
at oracle.jdbc.xa.OracleXAResource.recover(OracleXAResource.java:526) 
at com.ibm.ws.rsadapter.spi.WSRdbXaResourceImpl.recover(WSRdbXaResourceImpl.java:1038) 
at com.ibm.ws.Transaction.JTA.XARminst.recover(XARminst.java:138) 
at com.ibm.ws.Transaction.JTA.XARecoveryData.recover(XARecoveryData.java:687) 
at com.ibm.ws.Transaction.JTA.PartnerLogTable.recover(PartnerLogTable.java:524) 
at com.ibm.ws.Transaction.JTA.RecoveryManager.resync(RecoveryManager.java:1859) 
at com.ibm.ws.Transaction.JTA.RecoveryManager.run(RecoveryManager.java:2580) 
at java.lang.Thread.run(Thread.java:810)

 可以在Oracle数据库中执行sql语句:

1.grant select on sys.dba_pending_transactions to  username;   
2.grant select on sys.pending_trans$ to username;   
3.grant select on sys.dba_2pc_pending to username;   
4.grant execute on sys.dbms_system to username;  

 

你可能感兴趣的:(Hibernate)