Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
I just got a question about commit() and rollback() methods used by Transaction class. But in Session class there are save() and update() methods. Why commit() and rollback() method not declared with save() and update() method in Session class and declared in Transaction class.

we write:
Session ses = factory.openSession();
Transaction tx = session.beginTransaction();
then:
try{
. . .
session.save();
tx.commit();
} catch(Exception e){
. . .
tx.rollack();
}

Thanks.

What I have tried:

I tried to find answer using API docs and also tried to googled it.
Posted
Updated 11-Nov-17 19:08pm
Comments
Afzaal Ahmad Zeeshan 11-Nov-17 16:40pm    
Because committing and rollbacking are related to transactions, and saving and updating are related to session objects.

If you want to extend the functionality, you can always add more functions to the types of objects. General use case is different and this is why they do not support such a behavior.
VISH_a_CODE 14-Nov-17 7:04am    
Yes, your thought is right but still I am searching is there any other reason. Thanks for your comment though. If I found some other reason I will post here but if I didn't then your theory and below solution given by Mehdi Gholam should be only answers.

1 solution

You can "save" something with or without it being in a transaction, the session.beginTransaction() just indicates that the following (block of code) must all work completely or be rolled back if failed, so there are no side effects.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900