Analyze Hibernate's transaction mechanism

xiaoxiao2021-03-06  19

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_classnet.sf.hibernate.transaction.JTATransactionFactory # hibernate.transaction.factory_classnet.sf.hibernate.transaction.JDBCTransactionFactory if you do nothing to configure, JDBCTransaction case of default, if you configured:

Hibernate.Transaction.Factory_ClassNet.sf.Hibernate.Trassaction.jtatransactionFactory will use JTATRANSACTION, whether you are ready to let hibernate use JDBCTRANSACTION, or JTATRRANSACTION, my advice is not worthy, will keep it default, as follows:

# Hibernate.Transaction.Factory_classnet.sf.hibernate.Trassaction.jtatransActionFactory # hibernate.sf.hibaAntational.TrassAtNet.sf.Hibernate.Trassaction.jdbctransActionFactory I will give the reason. I. JDBC Transaction Look at 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 situation, when you are in your code That is actually JDBCTRANSACTION when using Hibernate's Transaction. 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 (); setOutocommit (false); ...} This is the method of launching Transaction, see Connection (). SetAutoCommit (false)? Is it very familiar? Come back

Public void commit () throws hibernateException {... try {ife (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? 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 two sentences) conn.setautocommit (TRUE); conn.close () <--- session.close (); understand, Hibernate's JDBCTransaction is CONN.Commit, it is not mysterious, just in hibernate, the session will automatically connect, will automatically connect .SetautoCommit (FALSE), unlike average JDBC, the default is TRUE, so you don't do anything else, because Hibernate has turned off autocommit, so when you use Hibernate, you don't write transaction in the program, The database is not reacted at all. Second, JTATRRANSACTION If you use Hibernate in EJB, or prepare to manage cross session's long transactions, you need to use JTATRRANSACTION, first look at an example:

Javax.Transaction.usertransaction tx = newinitialContext (). 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, Transaction is across session, its life cycle ratio SESSION is long. 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 the source code of JTATRRANSACTION, net.sf.hibernate.transaction.jtatransaction:

Public void begin (InitialContext, ... UT = (usertransaction) Context.lookup (UTNAME); ... see clearly? Code written on me 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 It is seen that Hibernate is how to encapsulate JTA's Transaction code. But have you seen any questions now? Carefully think about it, Hibernate Transaction is obtained from the session, tx = session.begintransaction (), finally submit TX, then then Session.Close, which is completely compliant with JDBC's Transaction's operation order, but this order is completely contradictory with JTA's transactioin operation! When using JTA transaction, don't use Hibernate's Transaction, but should be used as used as the JTA's code snippered. Summary: 1. Use hibernate on JDBC must write hibernate transaction code, otherwise The database did not respond. At this time, Hibernate's Transaction is 2, which is 2, using Hibernate written on the JTA, do not write Hibernate's Transaction code, otherwise the program will report an error 3, using hibernate on EJB, don't Write, in the deployment descriptor in EJB | --- CMT (Container Managed Transaction) | | --- BMT (Bean Managed Transaction) | | ---- JDBC Transaction Action | | ---- JTA Transaction

转载请注明原文地址:https://www.9cbs.com/read-46239.html

New Post(0)