Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is transaction.commit() in Hibernate?

What does transaction.commit() do?

Account account = new Account();
account.setId(100);
account = (Account) session.get(Account.class, account.getId());
System.out.println("Before Transaction: Balance = " + account.getBalance());
double preBal = account.getBalance();
account.setBalance(50000000);
Transaction transaction = session.beginTransaction();
session.update(account);
account = (Account) session.get(Account.class, account.getId());
System.out.println("After Transaction: Balance = " + account.getBalance());
// transaction.commit();    
account = (Account) session.get(Account.class, account.getId());
System.out.println("Pev-Bal=" + preBal + " Curr-Bal=" + account.getBalance());

This gives me result:

Hibernate: select account0_.id as id0_1_, account0_.balance as ..........
Before Transaction: Balance = 300.0
After Transaction: Balance = 5.0E7
Pev-Bal=300.0 Curr-Bal=5.0E7

But since I did not call transaction.commit() there was no change in Database.

Does this means everything was done only on some instance/objects without really changing the Database?

I am new to Hibernate, so please help me understand. I am using hibernate 4.

UPDATE:

IF I call transaction.commit() then the result have this line

Hibernate: update account set balance=? where id=?

And Database also updated.

Does this mean that without calling transaction.commit() everything was done only on instance level without really changing the Database?

like image 436
Mawia Avatar asked Jan 31 '13 09:01

Mawia


People also ask

What is the meaning of commit in transaction?

In the context of a database transaction, a commit refers to the saving of data permanently after a set of tentative changes. A commit ends a transaction within a relational database and allows all other users to see the changes.

Does flush commit transaction?

flush() will synchronize your database with the current state of object/objects held in the memory but it does not commit the transaction. So, if you get any exception after flush() is called, then the transaction will be rolled back.

How does transaction management work in hibernate?

In the hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA, JDBC). A transaction is associated with Session and instantiated by calling session.

What difference between save and commit?

Use the Save icon to temporarily save your changes. Note that this doesn't activate any recent changes you made to the workflow. Use the Commit icon to activate your recent changes.


2 Answers

Commit will make the database commit. The changes to persistent object will be written to database. Flushing is the process of synchronizing the underlying persistent store with persistant state held in memory. ie. it will update or insert into your tables in the running transaction, but it may not commit those changes (this depends on your flush mode).

When you have a persisted object and you change a value on it, it becomes dirty and hibernate needs to flush these changes to your persistence layer. It may do this automatically for you or you may need to do this manually, that depends on your flush mode(auto or manual) :)

So in short: transaction.commit() does flush the session, but it also ends the unit of work.

There is a similar reference to your problem here

like image 65
DarkHorse Avatar answered Sep 28 '22 05:09

DarkHorse


No matter what you'll do, write operations can't be done outside a transaction, Hibernate will complain if there is no ongoing transaction and throw an exception. So no choice here.

I am adding to that above quote by @pasacal : And that won't be effect DB untill you commit the transaction .

For more reference How expensive is committing a hibernate transaction?

like image 43
Suresh Atta Avatar answered Sep 28 '22 04:09

Suresh Atta