• 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

Multiple transaction in same session possible in hibernate??

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic