2014-01-30 60 views
1

我在我的spring配置文件中有下面的配置。我正在使用Spring3,Hibernate4和Tomcat7。使用spring配置jta事務管理器?

<jee:jndi-lookup id="wcDataSource" jndi-name="java:comp/UserTransaction" resource-ref="false" environment-ref="remoteEnv" /> 

    <util:properties id="remoteEnv"> 
     <prop key="java.naming.provider.url">jnp://jndi.myURL.me:1099</prop> 
     <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop> 
     <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop> 
     <prop key="jnp.disableDiscovery">true</prop> 
    </util:properties> 

    <bean id="dataSourceKS" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
     <property name="driverClass" value="${driverClassName}" /> 
     <property name="jdbcUrl" value="${url}" /> 
     <property name="user" value="${username}" /> 
     <property name="password" value="${password}" /> 
     <!-- pool sizing --> 
     <property name="initialPoolSize" value="15" /> 
     <property name="minPoolSize" value="10" /> 
     <property name="maxPoolSize" value="20" /> 
     <property name="acquireIncrement" value="3" /> 
     <property name="maxStatements" value="6000" /> 
     <property name="maxStatementsPerConnection" value="300" /> 

    </bean> 

    <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSourceKS"/> 
     <property name="annotatedClasses"> 
      <list> 
       <value>com.sample.MyBean</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.use_sql_comments">true</prop> 
       <prop key="hibernate.jdbc.batch_size">50</prop> 
      </props> 
     </property> 
    </bean> 

現在我該如何配置JTATransactionManager這裏使用@Transactional?這裏我有wcDataSourcedataSourceKS。謝謝!

謝謝!

+3

JTA在最好的情況下工作起來很困難,特別是在使用遠程資源時。您的數據源也應該是遠程查找,可能來自同一臺服務器,它應該至少具有XA能力,並且應該/必須知道底層JTA tx實現。接下來,您還需要適當地配置hibernate(對於初始者,而不是'LocalSessionFactoryBean'上的'datasource' set'jtaDataSource')。 –

+0

你應該看看這個[link](http://www.byteslounge.com/tutorials/spring-jta-multiple-resource-transactions-in-tomcat-with-atomikos-example),這就是嘗試去做。 (Atomikos是他們自己實現的一個JTA管理器,但彈簧配置將是相同的) –

+0

Arnaud,請提供鏈接... – user755806

回答

1

請參閱http://lafernando.com/2011/01/05/xa-transactions-with-apache-dbcp/它在代碼中執行它,但您應該能夠將其轉換爲彈簧配置。

這會導致類似的情況。

<jee:jndi-lookup id="userTransaction" jndi-name="java:comp/UserTransaction" resource-ref="false" environment-ref="remoteEnv" /> 
<jee:jndi-lookup id="jtaTransactionManager" jndi-name="java:comp/TransactionManager" resource-ref="false" environment-ref="remoteEnv" /> 

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
    <constructor-arg ref="userTransaction"/> 
    <constructor-arg ref="jtaTransactionManager"/> 
</bean> 

<util:properties id="remoteEnv"> 
    <prop key="java.naming.provider.url">jnp://jndi.myURL.me:1099</prop> 
    <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop> 
    <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop> 
    <prop key="jnp.disableDiscovery">true</prop> 
</util:properties> 

<bean id="oracleXaDataSource" class="oracle.jdbc.xa.client.OracleXADataSource"> 
    <property name="user" value="${username}" /> 
    <property name="password" value="${password}" /> 
    <property name="url" value="${url}" /> 
</bean> 

<bean id="dataSourceKS" class="org.apache.commons.dbcp.managed.BasicManagedDatasource"> 
    <property name="transactionManager" ref="jtaTransactionManager" /> 
    <property name="xaDataSourceInstance" ref="oracleXaDataSource" /> 
    <property name="initialPoolSize" value="15" /> 
    <property name="minPoolSize" value="10" /> 
    <property name="maxPoolSize" value="20" /> 
    <property name="acquireIncrement" value="3" /> 
    <property name="maxStatements" value="6000" /> 
    <property name="maxStatementsPerConnection" value="300" /> 
</bean> 

<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="jtaDataSource" ref="dataSourceKS"/> 
    // .. other hibernate properties 
</bean> 

注意對commons-dbcp的更改,因爲c3p0沒有支持XA的實現。

1

JtaTransactionManager不需要了解DataSource或任何其他特定資源,因爲它使用容器的全局事務管理基礎結構。 因此,conf文件應該看起來像這樣

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/> 
+0

看起來像什麼? – DavidPostill