user755806
user755806

Reputation: 6815

configure jta transaction manager using spring?

I have below configuration in my spring config file. I am using Spring3, Hibernate4 and 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 JTATransactionManager here to use @Transactional? Here i have wcDataSource and dataSourceKS. Thanks!

Thanks!

Upvotes: 2

Views: 25046

Answers (2)

user3740684
user3740684

Reputation: 101

The JtaTransactionManager does 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

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

Upvotes: 4

M. Deinum
M. Deinum

Reputation: 124441

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.

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.

Upvotes: 1

Related Questions