2013-01-21 24 views
1

我尝试在没有Java EE(类似JBoss的应用服务器)的情况下在Spring中进行全局(分发)事务,只能在tomcat中执行。涉及到两个数据库,第一个是PostgreSQL数据库,第二个是MS SQLServer数据库。 有没有办法做到这一点,而不使用休眠?与Spring分发事务,但没有休眠

我试着用atomikos API,但是我不知道如何在没有休眠会话的情况下做到这一点。我认为这很适合做基于JDBC的或Spring附带的其他工具。但我不知道该怎么做。

我的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:context="http://www.springframework.org/schema/context" 
xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:util="http://www.springframework.org/schema/util" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <!-- Get database driver XA properties from file --> 
    <util:properties id="jdbcConfiguration1" location="classpath:jdbcconfiguration1.properties"/> 
    <util:properties id="jdbcConfiguration2" location="classpath:jdbcconfiguration2.properties"/> 

    <bean id="dataSourceA" 
     class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> 
     <property name="uniqueResourceName"><value>XADBMS01</value></property> 
     <property name="xaDataSourceClassName"><value>org.postgresql.xa.PGXADataSource</value></property> 
     <property name="xaProperties" ref="jdbcConfiguration1"/>   
     <property name="poolSize"><value>1</value></property> 
    </bean> 

    <bean id="dataSourceB" 
     class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">   
     ... 
     <property name="poolSize"><value>1</value></property> 
    </bean>  

    <!-- Construct Atomikos UserTransactionManager, needed to configure Spring --> 
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" 
      init-method="init" destroy-method="close"> 
     <!-- when close is called, should we force transactions to terminate or not? --> 
     <property name="forceShutdown"> 
      <value>true</value> 
     </property> 
    </bean> 

    <!-- Also use Atomikos UserTransactionImp, needed to configure Spring --> 
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> 
     <property name="transactionTimeout"><value>300</value></property> 
    </bean> 

    <!-- Configure the Spring framework to use JTA transactions from Atomikos --> 
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
    <property name="transactionManager"><ref bean="atomikosTransactionManager" /></property> 
    <property name="userTransaction"><ref bean="atomikosUserTransaction" /></property> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    ...... 

</beans> 

是使用Hibernate需要它?我不想使用hibernate,因为我认为这对我的需求来说很复杂。是否有可能做到“基于Spring”?

回答

1

因此,您已经配置了两个DataSource,并且想要知道如何使用与您声明的JtaTransactionManager一致的方式。

Spring提供了两个选项:

  • (推荐)使用JdbcTemplateJdbcTemplate上的操作自动参与当前交易。

  • 使用DataSourceUitls.getConnection()可以获得一个Connection,它知道当前事务,并对其执行任意JDBC操作。

在你需要在你的代码来定义事务边界使用@TransactionalTransactionTemplate,如11. Transaction Management描述这两种情况。