Hibernate transaction mechanism

xiaoxiao2021-03-06  50

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 as JDBCTransaction or JTATransaction, depending on your configuration hibernate.properties in: # hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory # hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory if you Nothing, by default, use JDBCTRANSACTION if you are configured to: hibernate.sf.hibernate.trassaction.jtatransactionFactory will use JTATRRANSACTION Whether you are ready to make Hibernate use JDBCTRANSACTION, or JTATRRANSACTION, my advice is unworthy, it will remain default, as follows: # hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory # hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory I will be given in the following analysis the reason. I. JDBC Transaction See 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 use Hibernate in your code, it is actually JDBCTRANSACTION.

So what is JDBCTRANSACTION? Take a look at the source code: hibernate 2.0.3 Source code NET.SF.HIBERNATE.TRANSAASASACTION.JDBCTRANSASASASASASAC VOID BEGIN () THROWS HIBERNATEXCEPTION {... if (ToggleAutocommit) session.connection (). SetAutoCommit ( False); ...} This is the method of launching Transaction and see Connection (). SetAutoCommit (false)? Is it very familiar? Look at the 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? 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 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 the standard use JTA code snippet, which transaction is across session, and its lifecycle 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 the source code of JTATRRRRANSACTION, net.sf.hibernate.transaction.jtatransaction: public void becom (InitialContext context, ... UT = (useertransaction) context.lookup (utName); ... see clear Have you? The 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 to see how Hibernate is encapsulated by JTA's Transaction code. But have you seen something? Carefully think about it, hibernate Transaction is from Session Said, tx = session.begintransaction (), finally submit TX first, 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 !!! 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 Hibernate Transaction code, otherwise the database does not respond.

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

New Post(0)