user1727557
user1727557

Reputation:

Need to commit open transaction?

Is it need to commit opened hibernate transaction.What will happen if someone didn't? Is it caused to some issues?

thanks

Upvotes: 1

Views: 429

Answers (4)

Alok
Alok

Reputation: 301

A transaction must be closed. So by committing the transaction would automatically be closed as long as current context property mentioned in hibernate.cfg.xml is is thread and not managed.
This is to maintain the ACID properties of transaction. Also a when a transaction is begin it is allocated a lot of memory and resources.
What the best practices suggest is you should roll back the entire transaction and close the session if there's exception in catch block and you should commit the transaction in the last part of try block rather than finally block.

Upvotes: 0

Balaji Reddy
Balaji Reddy

Reputation: 5710

Well it is not only with hibernate transaction but with all database transactions. Commit/ Rollback are Atomicity of ACID (Atomicity, Consistency, Isolation, Durability) properties which actually represent/specifies TRANSACTION. Atomicity is more like do or die.

Answer to your question:

//creates something like cache/temporary space for you to perform all your operations. Note   this changes will not be reflected in your database at this point.
 Session.beginTransaction();

//perform some db operations 

//this line flushes/post your changes from temporary stuff to database. If your changes contains error then this will not be affected/made changes to the database else the changes will be affected.
Session.commit();

Hope this is helpful!

Upvotes: 1

ben75
ben75

Reputation: 28746

A transaction MUST ends, either by a commit or by a rollback.

Why ?

A transaction is consuming resources:

  • some bytes in memory
  • usually a JBDC connection. (or any kind of connection to a transnational external resource)

So, if a tx never ends : it will use a JDBC connection forever and there a good chances that you run out of database connections.

Conclusion : you don't need to commit every tx, but you need to terminate them : either by a commit or a rollback (there is no other end state for a tx)

Upvotes: 1

Jordi Reina
Jordi Reina

Reputation: 111

Commit will make the database commit. The changes to persistent object will be written to database. If you don't commit you will loose the changes you made in the database.

Upvotes: 1

Related Questions