Monday, November 25, 2013

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(); ?
share|improve this question
add comment
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 theTransactionFactory in use. For instance, with the JDBCTransactionFactory, it just ensures auto-commitis 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.
share|improve this answer
 
Thanks Ryan For such a elanorate answer. Just two more points. As you said "JDBC is the standard API that Java applications use to interact with a database" can i say every java application will use jdbc to connect to database either explicitly or internally(in case of any orm tools like hibernate)or EJB. You said " JTA is the standard API for managing transactions across one or more resources." I have this scenario in a java application i have two different schemas as database. Here i still will be using jdbc for both the database/schema .Also i will be using JTA here to manage continued..... –  M Sach Sep 18 '11 at 14:15 
 
continued... the transactions across both schemas.Right? –  M Sach Sep 18 '11 at 14:16
 
@Mohit: It would be an extremely rare Java app that connects to a database with other than JDBC. I've never heard of one myself, but it could happen. As for JTA, it's just the JEE transaction API. There are other ways of managing transactions, such as Spring's transaction management (which can, however, also integrate with JTA). If you want distributed transactions, I don't know much about them, but I think you'll probably want to go with a JTA implementation. –  Ryan Stewart Sep 19 '11 at 3:45
add comment