Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Hibernate session.getTransaction().begin() vs session.beginTransaction()

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()

like image 495
RanPaul Avatar asked Jun 13 '14 01:06

RanPaul


People also ask

What is Session beginTransaction in Hibernate?

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().

What is the purpose of 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.

What is difference between session and transaction in Hibernate?

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.

What is Session SessionFactory and transaction in Hibernate?

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.


1 Answers

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.

like image 140
JamesENL Avatar answered Nov 09 '22 23:11

JamesENL