I couldnt find much information on this topic. Can someone explain me whats the Difference between Hibernate session.getTransaction().begin(
) vs session.beginTransaction()
vs session.beginTransaction().begin()
In 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. beginTransaction().
session. beginTransaction is used to start a transaction which may consists of one or more crude operations like INSERT,SELECT,DELETE etc. While transaction. commit() is used for committing all changes happened during a transaction so that database remains in consistent state after operations.
Session is a common interface and a way of communication between java application and Hibernate ORM that can have one or many transactions. basically a transaction is a single atomic operation that may be insert, update, delete. in case of interupt the transaction is roll back.
SessionFactory is a factory class for Session objects. It is available for the whole application while a Session is only available for particular transaction. Session is short-lived while SessionFactory objects are long-lived. SessionFactory provides a second level cache and Session provides a first level cache.
Calling session.getTransaction().begin()
doesn't make much sense as session.getTransaction()
will retrieve the transaction already in progress because it assumes that a transaction is in progress. You are basically saying, begin this transaction that should already be in progress.
session.beginTransaction()
will either begin a new Transaction if one isn't present, or it will use an existing transaction to begin the unit of work specified.
session.beginTransaction().begin()
== session.beginTransaction()
For more information I suggest you have a look at the Hibernate documentation for your version of Hibernate. You should only be dealing with the low levels of Hibernate like this if you are not using a TransactionManager
or you are using a JDBCTemplate
so have a think because messing with transactions in this way gets messy fast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With