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

5 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes