Analyzing the transaction processing mechanism of Hibernate

Source: Internet
Author: User
Tags commit flush

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:

#hibernate.transaction.factory_class
net.sf.hibernate.transaction.JTATransactionFactory
#hibernate.transaction.factory_class
net.sf.hibernate.transaction.JDBCTransactionFactory

If you are not configuring anything, use Jdbctransaction by default, if you configure it to:

hibernate.transaction.factory_class
net.sf.hibernate.transaction.JTATransactionFactory

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:

#hibernate.transaction.factory_class
net.sf.hibernate.transaction.JTATransactionFactory
#hibernate.transaction.factory_class
net.sf.hibernate.transaction.JDBCTransactionFactory

In the analysis below I will give the reason.

First, JDBC Transaction

See our code example when using JDBC transaction:

Session session = sf.openSession();
Transaction tx = session.beginTransactioin();
...
session.flush();
tx.commit();
session.close();

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?

Look again.

public void commit() throws HibernateException {
...
try {
if ( session.getFlushMode()!=FlushMode.NEVER ) session.flush();
try {
session.connection().commit();
committed = true;
}
...
toggleAutoCommit();
}

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(); (对应左边的两句)
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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.