Reputation: 2053
i am a newbie to JTA, and i wanted to do a two phase commit in my application which would connect to two different database schemas. I am using Spring, Hibernate and JPA in my application and i want to add the JTA transaction manager to start with, but i havent been able to get through this.
Can someone please help me resolve the problem that i am facing or point me at what is wrong in my configuration or understanding.
Here is my setup in the config file
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="LineManagement" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="${hibernate.dialect}" />
</bean>
</property>
<property name="beanName" value="entityManager"></property>
</bean>
<bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp"
init-method="init" depends-on="setAtomikosSystemProps">
<constructor-arg>
<props>
<prop key="com.atomikos.icatch.service">com.atomikos.icatch.standalone.UserTransactionServiceFactory</prop>
</props>
</constructor-arg>
</bean>
<bean id="setAtomikosSystemProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="getProperties" />
</bean>
</property>
<property name="targetMethod" value="putAll" />
<property name="arguments">
<util:properties>
<prop key="com.atomikos.icatch.file">/jta.properties</prop>
<prop key="com.atomikos.icatch.hide_init_file_path">true</prop>
<prop key="com.atomikos.icatch.console_log_level">DEBUG</prop>
</util:properties>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="userTransaction" ref="AtomikosUserTransaction" />
<property name="transactionManager" ref="AtomikosTransactionManager" />
</bean>
<bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" depends-on="userTransactionService" >
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="AtomikosTransactionManager" class = "com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
The persistence.xml has the following configuration
<persistence-unit name="LineManagement" transaction-type="JTA">
<properties>
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.current_session_context_class" value="thread" />
<property name="hibernate.default_batch_fetch_size" value="200" />
<property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
<property key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<!-- General Debugging -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="true" />
</properties>
</persistence-unit>
When i run the app, it gives me an exception org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
i tried to muck around with the propogation flag on the @Transactional annotation, since i believe this exception states that there isnt a transaction present, but it didnt make the error go away...
Upvotes: 1
Views: 6209
Reputation: 634
Try putting @Transactional
with "Required"
before the service (or persistence layer) call. Make sure you are using the same transaction manager that is looked by the declared hibernate transaction manager lookup class.
Basically your spring transaction interceptor should be starting a transaction before you do any db operation that needs a transaction.
Upvotes: 1