Java 使用spring配置jta事务管理器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21458196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:49:10  来源:igfitidea点击:

configure jta transaction manager using spring?

javaspringtransactionsjava-ee-7

提问by user755806

I have below configuration in my spring config file. I am using Spring3, Hibernate4 and Tomcat7.

我的 spring 配置文件中有以下配置。我正在使用 Spring3、Hibernate4 和 Tomcat7。

    <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>

Now how can i configure JTATransactionManagerhere to use @Transactional? Here i have wcDataSourceand dataSourceKS. Thanks!

现在我如何JTATransactionManager在这里配置以使用@Transactional?在这里,我有wcDataSourcedataSourceKS。谢谢!

Thanks!

谢谢!

回答by M. Deinum

See http://lafernando.com/2011/01/05/xa-transactions-with-apache-dbcp/which does it in code but which you should be able to translate to a spring configuration.

请参阅http://lafernando.com/2011/01/05/xa-transactions-with-apache-dbcp/它在代码中执行此操作,但您应该能够将其转换为 spring 配置。

Which would result in something like this.

这会导致这样的事情。

<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>

Note the change to commons-dbcp as c3p0 doesn't have XA capable implementations.

请注意对 commons-dbcp 的更改,因为 c3p0 没有支持 XA 的实现。

回答by user3740684

The JtaTransactionManagerdoes not need to know about the DataSource, or any other specific resources, because it uses the container's global transaction management infrastructure. So conf file should look like this

的JtaTransactionManager不需要知道数据源,或任何其他特定资源,因为它使用容器的全局事务管理。所以conf文件应该是这样的

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