• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple transaction in same session possible in hibernate??

 
Mothea Anil Kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible to work on more than one transaction with the same hibernate session object ?? I'm getting some problem by doing like this,
.
.
........
........

Session session = sessionFactory.getSession();
Transaction tx = null;

While(totalObjects) {
tx = session.beginTansaction();
try {
session.saveOrUpdate(Object);
tx.commit();
} catch(Exception e) {
tx.rollback();
throw e;
}
......
......
}

In the above code in while loop , in every condtion i'm creating new transaction and save or updating the object.If any error occurs i'm rollback the transaction. Problem is, if Object is saving means there is no problem. If it's not saving means , I rollback the transaction and for the next objects it's giving the error related to session.(error).

My question is ,
--> Is it necessery to close the session every time ??
--> Is it not possible to use more than one transation in same session ??

Any body help me please.It's urgent.
Thank's in advance.

--Anil
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I want to update 100 records, Why would I want to create 100 transactions. I would just want one transaction, and if any of the 100 updates/inserts fail, then rollback the transaction. If you have a "Use Case" that takes these 100 objects and want each individual Object to have its own transaction, then I think the Use Case is wrong. That there are 100 different units of work, so different calls.

I can't see a case where you would need multiple transactions like that.

Mark
 
Mothea Anil Kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

Thanks for your reply.

Your are saying is correct. But i'm facing the situation where i have to continue to saveOrUpdate next records even if previous record was failed.

Please give answer to my questions.

Thanks,
-Anil
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, so you are taking the Unit of Work definition of one all succeeds or it all fails, and trying to redefine it how you want it. But a Unit of Work is just that, it all succeeds or all fails, you cannot change what that means. So it is a matter of relooking at your use case. I suggest getting the JDBC connection directly out of the Session and work at that level to get what you want, or create one session per update.

Mark
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's not saving means , I rollback the transaction and for the next objects it's giving the error related to session.(error).


Could you post the error!
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you commit the transaction, the session will be flushed and closed, hence you will get an error. you might want to try to set the FlushMode.never so the session will never be flushed automatically.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hanna Habashy:
When you commit the transaction, the session will be flushed and closed, hence you will get an error. you might want to try to set the FlushMode.never so the session will never be flushed automatically.


Yes, but you have to flush, manually or automatically at some point to send the statements to the database, and any exception you get will cause the entire transaction to rollback.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic