Hibernate is a lightweight object encapsulation of JDBC, hibernate itself does not have transaction processing capabilities, hibernate transaction is actually the underlying JDBC transaction encapsulation, or JTA Transaction's encapsulation, below we detailed analysis:
Hibernate can be configured as Jdbctransaction or Jtatransaction, depending on your configuration in hibernate.properties:
will use Jtatransaction, whether you are ready to let hibernate use Jdbctransaction, or jtatransaction, my advice is nothing, will keep it in the default state, as follows:
This is the default case, when you use hibernate transaction in your code is actually jdbctransaction. So what exactly is Jdbctransaction? To see the source code is clear:
Hibernate2.0.3 the class in source code
Net.sf.hibernate.transaction.JDBCTransaction:
public void Begin () throws Hibernateexception {
...
if (toggleautocommit) session.connection (). Setautocommit (false);
...
}
This is the way to start transaction, see Connection (). Setautocommit (False)? Is it familiar?
This is the submission method, see Connection (). Commit ()? I don't need to say more, this kind of code is very easy to understand, through reading to make us understand what hibernate transaction are doing? I now translate the example written by hibernate into JDBC, and everyone will be at a glance:
Connection conn = ...; <---session = sf.opensession ();
Conn.setautocommit (FALSE); <---tx = Session.begintransactioin ();
... <---...
Conn.commit (); <---tx.commit (); (corresponding to the left of the two sentences)
Conn.setautocommit (TRUE);
Conn.close (); <---session.close ();
See understand it, hibernate jdbctransaction is simply conn.commit, there is no mystery at all, but in Hibernate, session open, will automatically conn.setautocommit ( False), unlike the general JDBC, the default is true, so you do not write a commit does not matter, because Hibernate has autocommit to turn off, so with hibernate, you do not write in the program transaction words , the database is not responding at all.
Second, jtatransaction
If you use hibernate in EJBs, or if you are ready to use JTA to manage long transactions across sessions, then you need to use jtatransaction to see an example:
This is the standard snippet for using JTA, transaction is a cross session, and its lifecycle is longer than the session. If you use Hibernate in EJB, then it is the simplest, you do not write any transaction code, directly on the EJB deployment descriptor to configure a certain method to use transactions.
Now let's analyze the source code for jtatransaction, Net.sf.hibernate.transaction.JTATransaction:
public void begin (InitialContext context, ...)
...
UT = (usertransaction) context.lookup (utname);
...
Do you see it clearly? And I wrote the code tx = new Initial context? (). Lookup ("Javax.transaction.UserTransaction"); Is it exactly the same?
public void commit () ...
...
if (newtransaction) ut.commit ();
...
Jtatransaction's control is slightly more complex, but it's still clear how hibernate encapsulates JTA's transaction code.
But do you see any problems now? Think carefully, Hibernate transaction is obtained from the session, TX = Session.begintransaction (), the last to be submitted to TX, And then Session.close, which completely conforms to the transaction order of the JDBC, but this order is completely contradictory to the JTA transactioin sequence of operations!!! JTA is to start the transaction, then start the session, close the session, and finally submit the transaction, so when you use JTA transaction, Then do not use the Hibernate transaction, but should be like the JTA code snippet above me to use the line.
Summarize:
1, the use of hibernate on JDBC must write hibernate transaction code, otherwise the database does not respond. At this point, Hibernate's transaction is connection.commit.
2, on the JTA use hibernate write JTA transaction code, do not write hibernate code transaction, otherwise the program will report an error
3, on the EJB use hibernate what transactioin code do not write, in the EJB deployment descriptor inside the configuration
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.