Hibernate is a lightweight object package for JDBC. Hibernate itself does not have Transaction processing. Hibernate's Transaction is actually encapsulation of the underlying JDBC Transaction, or the package of JTA Transaction, the following detailed analysis: Hibernate can be configured For 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 don't configure anything, use JDBCTRANSACTION by default if you are configured to: hibernate.jtatransactionFactory.tatransActionFactory
JTATRRANSACTION Whether you are ready to let hibernate use JDBCTRANSACTION, or JTATRRANSACTION, my advice is not worthy, will keep it the default state, as follows: # hibernate.transaction.Factory_class net.sf.hibernate.Transaction.jtatransAdomFactory
# hibernate.transaction.Factory_Class Net.sf.hibernate.transaction.jdbcTransActionFactory
I will give the reason among the following analysis. I. JDBC Transaction See the use of JDBC Transaction when our code example: session session = sf.opensession ();
Transaction tx = session.begintransactioin ();
...
Session.flush ();
TX.comMit ();
session.close ();
This is the default situation, when you use Hibernate in your code, it is actually JDBCTRANSACTION. So what is JDBCTRANSACTION? Take a look at the source code clear: hibernate2.0.3 source code NET.SF.HIBERNATE.TRANSAASACTION.JDBCTRANSACTION:
Public void begin () throws hibernateException {
...
IF (ToggleAutocommit) session.connection (). setAutocommit (false);
...
}
This is a method of launching Transaction, see Connection (). SETAUTOCOMMIT (FALSE)? Is it very familiar? Let's see 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 and see connect (). Commit ()? You don't have to say more, this class code is very easy to understand. What do you understand? What is it to do with Hibernate Transaction? I now translate the example written by hibernate into JDBC, everyone is clear: 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 ();
Look, Hibernate's JDBCTransaction is basically conn.commit, it is not mysterious, but in Hibernate, when session open, automatic conn.setautocommit (false), unlike average JDBC, default They are all True, so you don't do it in the end, it doesn't matter. Because Hibernate has turned off the autocommit, you don't respond to Transaction in the program. Second, JTATRRANSACTION If you use hibernate in EJB, or prepare to manage cross session long transactions, then you need to use JTATRRRANSACTION, first see an example: javax.transaction.usertractions TX = new initialContext (). Lookup ("Javax .transaction.usertransaction ");
SESSION S1 = sf.openSession ();
...
S1.Flush ();
S1.Close ();
...
Session S2 = sf.openSession ();
...
s2.flush ();
S2.Close ();
TX.comMit ();
This is a standard use JTA code snippet, which transaction is across sessions, and its life cycle is longer than Session. If you use hibernate in EJB, it is the easiest, you don't write any transaction code, you can use a certain method to use a transaction directly on the EJB deployment descriptor. Now let's analyze JTATRRANSACTION source code, net.sf.hibernate.transaction.jtatransaction: public void becom (InitialContext Context, ...
...
UT = (useertransaction) context.lookup (utName);
...
Is it clear? And the code I wrote TX = New (Initial Context) (). Lookup ("javax.transaction.usertransaction); Is it exactly the same? Public void commit () ...
...
IF (newTransaction) ut.commit ();
...
JTATRRANSACTION control is slightly complicated, but it can still be clear that Hibernate is how to encapsulate JTA's Transaction code. But have you seen any questions now? Think carefully, Hibernate Transaction is obtained from session, tx = session.begintransaction (), finally submit TX, then session.close, which is fully compliant with JDBC's transaction, but this order is and JTA TransactioIn operation is completely contradictory! ! ! JTA is started to start Transaction, then start session, close session, finally submit transaction, so do you want to use Hibernate's Transaction, but should use the JTA's code snippered as the JTA. Talent. Summary: 1. Use hibernate on JDBC to write the Hibernate Transaction code, otherwise the database does not respond. At this time, Hibernate is already Connection.commit. It is 2. Use Hibernate to write JTA transaction code on JTA, do not write hibernate's Transaction code, otherwise the program will report an error 3, use hibernate on EJB, don't write, in EJB Deployment Descriptor Inside Configuration | --- CMT (Container Managed Transaction)
|
| --- BMT (Bean Managed Transaction)
|
| ---- JDBC Transaction
|
| ---- JTA Transaction
Question: javax.transaction.usertransaction tx = new initialContext (). Lookup ("javax.transaction.usertransaction);
SESSION S1 = sf.openSession ();
...
S1.Flush ();
S1.Close ();
...
Session S2 = sf.openSession ();
...
s2.flush ();
S2.Close ();
TX.comMit ();
S1 is not closed, using S1 in code that uses S2, use S1, can not be able to save resources, do not need repeated connections, turn off) but sf.opengsession (), there is no setAutocommit (false), I want to ask Yes, if you don't write any transaction code, such as:
Session s = sf.opensession ();
......
s.close ();
The database will not react (this should be the default autonommit to true).
Will not react. When SF.OpenSession () creates a session instance, conn.setautocommit (FALSE) has been called. Also, I would like to ask: 1. sflush () is the required 2. S.Close () is not to turn off code>
Answer: S.flush is not necessary, S.Close () will call once s.flush () s.close () should be turned off under normal circumstances unless you use Threadlocal to manage Session. S1 is not closed, using S1 in code to operate using S2 can not (I think this more saves resources, no need to repeated connection, close) In this example, the JTA can be seen in this example. Assume Class A {
Find () {
SESSION S1 = sf.openSession ();
...
S1.Flush ();
S1.Close ();
}
}
Class b {
Find () {
Session S2 = sf.openSession ();
...
s2.flush ();
S2.Close ();
}
}
MAIN {
Tx = ...;
A.find ();
B.Find ();
TX.comMit ();
}
do you understand? JTA Transaction Management is a cross-class call.