The moose likes Object Relational Mapping and the fly likes Multiple transaction in same session possible  in hibernate?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Multiple transaction in same session possible  in hibernate??" Watch "Multiple transaction in same session possible  in hibernate??" New topic
Author

Multiple transaction in same session possible in hibernate??

Mothea Anil Kumar
Greenhorn

Joined: Aug 01, 2005
Posts: 22
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

Joined: Feb 05, 2001
Posts: 16622

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mothea Anil Kumar
Greenhorn

Joined: Aug 01, 2005
Posts: 22
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

Joined: Feb 05, 2001
Posts: 16622

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

Joined: Jun 03, 2005
Posts: 228
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

Joined: Aug 20, 2003
Posts: 532
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.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16622

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
 
IntelliJ Java IDE
 
subject: Multiple transaction in same session possible in hibernate??
 
Threads others viewed
struts 2 with hibernate insertion exception
Hibernate Exception: Could not insert and JDBC Rollback failed..
Passed SCBCD 5.0 :)
Need help with Hibernate Query language - timestamp field
Question for CMT Session Bean
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture