• 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

@Transactional annotaion in Spring

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am working on a Spring-hibernate based web app.I am using a DAO with methods like saveObject(),updateObject() etc.Now I am using the annotation @Transactional at the top of the class.In the DAO,as I mentioned above,I have a method called saveObject().
a Line of code like
sessionFactory.getCurrentSession().save(object);works fine.The record is inserted into the database.

But if a Transaction is started and then use save in that transaction,the insert works too.So I am not clear about the difference between using the annotation @Transactional and starting a separate transaction

public void saveObject(Object persistObject) {
System.out.println("call from the DAO ");
plSessionFactory.getCurrentSession().beginTransaction();
plSessionFactory.getCurrentSession().save(persistObject);
plSessionFactory.getCurrentSession().getTransaction().commit();

}

Please help.

Thanks
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use a transactional annotation so you won't have to write code to start the transaction. AOP will weave the code around your DAO that will start and stop the transaction for you
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply.So when I remove the annotation @Transactional and have the transaction done manually,I was expecting it to work.But what I get is an exception.The application which I am working on is a Spring-hibernate based webapp where I have a spring xml file which has all the information regarding datasource,session factory and transaction.

Also,if I have the @Transactional on and have the code on for manual transactions,will two separate transactions be created?

Thanks for your insight.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Donald wrote:Thank you for the reply.So when I remove the annotation @Transactional and have the transaction done manually,I was expecting it to work.But what I get is an exception.
.



What's the exception?

Chris Donald wrote:
The application which I am working on is a Spring-hibernate based webapp where I have a spring xml file which has all the information regarding datasource,session factory and transaction.

Also,if I have the @Transactional on and have the code on for manual transactions,will two separate transactions be created?

Thanks for your insight.



Never done it before. but I would expect that you Spring will call sessionCOntext.getCurrentSession to start/commit transaction, and if you use getCurrentSession to save then you should be in the same transaction.

BTW, I have a hunch that your core problem is TheXYProblem. Why are you doing your own transaction management mixed with Transactional annotations?
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have a look at this http://www.webtuts.in/transaction-management-in-spring/
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic