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.