Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Object Relational Mapping
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Object Relational Mapping
Rollback not working in hibernate
Booma Devi
Ranch Hand
Posts: 61
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am using spring with hibernate transaction management for
testing
the rollback behaviour. I have implemented the code like below.. But its not rollbacked. Can you please help to solve this problem?
hibernate.cfg.xml
============
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- oracle DB details --> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"></property> </bean> <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>account.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="mysessionFactory" /> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="mysessionFactory"/> </bean> <bean id="accountDao" class="com.spring.dao.AccountDaoImpl"> <property name="sessionFactory" ref="mysessionFactory"/> <property name="transactionTemplate" ref="transactionTemplate"></property> </bean>
AccountDaoImpl.java
public void insertData(Account account) { hibernateTemplate.save(account); System.out.println("Insert done for account object!!!!!"); } public void getUpdatedBalanceAmount(String fromAccountName,float transferAmount,float fromAccountbalance) { SessionFactory sessionFactory= hibernateTemplate.getSessionFactory(); Session session=sessionFactory.openSession(); Query query=session.createQuery("update Account set balance=:balance where name=:name"); query.setString("name",fromAccountName); query.setDouble("balance", fromAccountbalance); int result=query.executeUpdate(); } public void transferAmount(String toAccountName,float transferAmount,float balance) { SessionFactory sessionFactory= hibernateTemplate.getSessionFactory(); Session session=sessionFactory.openSession(); Query query=session.createQuery("update Account set balance=:balance where name=:name"); query.setString("name",toAccountName); query.setDouble("balance", balance); // if the isSuccess=false then entire changes will rollback ( I have set the success as false. so that exception will raise here) if(isSuccess) { int result=query.executeUpdate(); System.out.println("Update Done"+result); } else { throw new RuntimeException("Problem Occured while updating the transferred amount"); } } public void testAccountProcess(final String fromAccountName,final String toAccountName,final float transferAmount) { hibernateTemplate.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException,SQLException { try { float fromAccountBalance=getFromAccountBalance(fromAccountName); System.out.println("From Account Balance(before transfer amount to other bank)=====>"+fromAccountBalance); float toAccountBalance=getToAccountBalance(toAccountName); if(fromAccountBalance>=transferAmount) { fromAccountBalance=fromAccountBalance-transferAmount; getUpdatedBalanceAmount(fromAccountName,transferAmount,fromAccountBalance); transferAmount(toAccountName,transferAmount,toAccountBalance); System.out.println("====Transaction Successful==="); } else { System.out.println("Not enough balance in account"); } } catch(Exception ex) { session.connection().rollback(); //session.getTransaction().rollback(); System.out.println("Transaction rolledback--->"+ex.getMessage()); } return null; } }); }
Booma Devi
Ranch Hand
Posts: 61
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Problem solved..
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
calling a method in hibernate DAO through JMX export MBean
Spring test + JUnit +Hibernate transaction rollback issue
how to get session from spring-config.xml
I need help with Hibernate
Transaction Mangement with Spring JTA
More...