Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does hibernate uses jdbc internally or JTA internally or it is configurable?"

Tags:

hibernate

I am new to hibernate and started studying hibernate.The chapter I am going thru uses hibernate with JDBC().My first question is Does hibernate use jdbc internally or JTA internally to perform persist and get operations? But I can see some mention of JTA also here, like getCurrentSeessionSession() method obtains the session associated with current JTA transaction. Question:- Basically I want to understand the role of JTA and jdbc here in hibernate.

Question2 :- I can see below code snippet in any operation in hibernate

try{ 
session=factory.openSession();
tx=session.beginTransaction();
session.save(myClass);
tx.commit();
}
finally{
session.close();
}

Here, I want to understand the role of line // tx=session.beginTransaction(); As per understanding, each session will be using one connection . So even if we start multiple transactions from the same session, we will be using same connection. once we commit the specific transaction all transactions created from the same session will be committed once. So what are we trying to achieve with // tx=session.beginTransaction(); ?

like image 822
M Sach Avatar asked Sep 17 '11 15:09

M Sach


1 Answers

Does hibernate uses jdbc internally or JTA internally to perform persist and get operations?

JDBC and JTA aren't interchangeable. JDBC is the standard API that Java applications use to interact with a database. JTA is the standard API for managing transactions across one or more resources. The closest answer to your question would be that "internally", Hibernate uses JDBC to interact with the database.

Like getCurrentSeessionSession() method obtains the session associated with current JTA transaction.

Not exactly. SessionFactory.getCurrentSession() gets a session according to the current session context. One implementation of that strategy is the JTA session context, which essentially associates sessions with a JTA transaction. A JTA transaction doesn't "have" a Hibernate session, since JTA knows nothing about Hibernate, and it wouldn't be quite right to say that Hibernate uses JTA internally. It simply has the ability to integrate with JTA and let it manage transactions.

here i want to understand the role of line // tx=session.beginTransaction();

It begins a transaction in whatever transaction mechanism you're using, which is governed by the TransactionFactory in use. For instance, with the JDBCTransactionFactory, it just ensures auto-commit is turned off so that changes won't be committed until the transaction is complete.

once we commit the specific transaction all transactions created from a same session will be commited once.

Under normal circumstances, a Session is associated to only one transaction. Multiple calls to Session.beginTransaction() will simply return the same underlying Transaction.

So what we are trying to achieve with // tx=session.beginTransaction()

Just that: tell whatever is managing your transactions that you're beginning a new transaction. That implies that everything that happens until you commit() or rollback() should have the generally accepted semantics of a database transaction.

like image 70
Ryan Stewart Avatar answered Sep 28 '22 10:09

Ryan Stewart