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

时间:2014-01-30 13:43:52

标签: java spring transactions java-ee-7

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

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

谢谢!

2 个答案:

答案 0 :(得分: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 :(得分:1)

JtaTransactionManager 不需要了解DataSource或任何其他特定资源,因为它使用容器的全局事务管理基础结构。 所以conf文件看起来应该是这样的

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