• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Transactional annotaion in Spring

 
Richard Ellison
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Chris Donald
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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?
 
Anindya Roy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have a look at this http://www.webtuts.in/transaction-management-in-spring/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic