Hi,
I am using Hibernate + Spring + JTA in my project. I am using JTA because my project deals with multiple databases. When I use the following:
getHibernateTemplate().save(employee);
JTA works properly in above case and in case of any exception, Transaction is rolled back.
But when I use following transaction is not rolled back.
Session ses =getHibernateTemplate().getSessionFactory().getCur rentSession();
ses.createQuery("update Employee emp set emp.empName='sonu' where emp.empId=1").executeUpdate();
Can anybody please help me in understanding the difference and also help me in knowing the solution.
Following is my configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Data source bean -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/emp</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>*****</value>
</property>
<property name="defaultAutoCommit" value="true" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>config/hibernate/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">tru e</prop>
<prop key="hibernate.transaction.flush_before_completion ">true</prop>
<prop key="hibernate.transaction.auto_close_session">tru e</prop>
<prop key="hibernate.transaction.factory_class ">org.hibernate.transaction.JTATransactionFact ory</prop>
<prop key="hibernate.current_session_context_class">thre ad</prop>
<prop key="jta.UserTransaction ">java:comp/UserTransaction</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<!-- <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager">
<property name="userTransaction">
<bean class="org.springframework.transaction.jta.JotmFac toryBean"/>
</property>
</bean>
<!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->
<!-- Spring DAO Entries -->
<bean id="employeeServiceTarget" class="com.dupont.business.impl.EmployeeImpl">
<property name="employeeDao" ref="employeeDao" />
</bean>
<bean id="employeeService" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref local="employeeServiceTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="emp">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="employeeDao" class="com.dupont.hibernate.impl.EmpDaoImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
</beans>
Thanks in advance.


Reply With Quote
. Use the forum search as this question has been answered numerous times before.