Developing hibernate applications with eclipse

Time:2021-12-22

Hibernate is a lightweight object encapsulation of JDBC. Hibernate itself does not have the transaction processing function. Hibernate’s transaction is actually the encapsulation of the underlying JDBC transaction or JTA transaction. We will analyze it in detail below:

Hibernate can be configured as JDBC transaction or jtatransaction, depending on what you do in hibernate Configuration in properties:

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

If you do not configure anything, JDBC transaction is used by default. If you configure it as:

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

Jtatransaction will be used. No matter whether you want hibernate to use JDBC transaction or jtatransaction, my advice is to 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 following analysis, I will give the reasons.

  1、 JDBC transaction

Take a 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. When you use hibernate transaction in your code, it is actually JDBC transaction. So what exactly is JDBC transaction? Just look at the source code:

  Hibernate2. 0.3 classes in source code

  net.sf.hibernate.transaction.JDBCTransaction:

  public void begin() throws HibernateException {
  …
  if (toggleAutoCommit) session.connection().setAutoCommit(false);
  …
  }

This is the method to start the transaction. See connection() Setautocommit (false)? Are you familiar with it?

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 commit method. See connection() Commit()? Needless to say, this class code is very simple and easy to understand. Through reading, we can understand what hibernate transactions are doing? Now I translate the example written in Hibernate into JDBC, and you can see it at a glance:

  Connection conn = …; <— session = sf.openSession();
  conn.setAutoCommit(false); <— tx = session.beginTransactioin();
  … <— …
  conn.commit(); <— tx.commit(); (corresponding to the two sentences on the left)
  conn.setAutoCommit(true);
  conn.close(); <— session.close();

See, hibernate’s JDBC transaction is simply conn.commit. There is no mystery at all. However, in Hibernate, when the session is opened, it will automatically conn.setautocommit (false). Unlike ordinary JDBC, the default is true, so it doesn’t matter if you don’t write commit at last. Because hibernate has turned off autocommit, So when using hibernate, if you don’t write transaction in the program, the database will not respond at all.

  2、 Jtatransaction

If you use hibernate in EJB or are going to use JTA to manage long transactions across sessions, you need to use jtatransaction. Take an example first:

  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();

This is a standard code fragment using JTA. Transaction is cross session, and its life cycle is longer than that of session. If you use hibernate in EJB, it is the simplest. You don’t need to write any transaction code. You can directly configure whether a certain method uses transactions on the deployment descriptor of EJB.

Now let’s analyze the source code of jtatransaction, net sf. hibernate. transaction. JTATransaction:

  public void begin(InitialContext context, …
  …
  ut = (UserTransaction) context.lookup(utName);
  …

Do you see clearly? And the code I wrote above TX = new initial context? (). lookup(“javax.transaction.UserTransaction”); Is it exactly the same?

  public void commit() …
  …
  if (newTransaction) ut.commit();
  …

The control of JTA transaction is a little complicated, but you can still clearly see how hibernate encapsulates the transaction code of JTA.

But do you see any problems now? Think about it carefully. Hibernate transaction is obtained from session, TX = session Begin transaction(), and finally submit TX before session Close, which is completely consistent with the transaction operation sequence of JDBC, but this sequence is completely contradictory to the transactioin operation sequence of JTA!!! JTA starts the transaction first, then starts the session, closes the session, and finally submits the transaction. Therefore, when you use the transaction of JTA, you should never use the transaction of hibernate, but should use it like the code fragment of JTA above.

Summary:

1. When using hibernate on JDBC, the hibernate transaction code must be written, otherwise the database will not respond. At this time, the transaction of Hibernate is connection Just commit

2. Use hibernate to write the transaction code of JTA on JTA. Do not write the transaction code of hibernate, otherwise the program will report an error

3. Do not write any transactioin code when using hibernate on EJB. Configure it in the deployment descriptor of EJB

  |—CMT(Container Managed Transaction)
  |
  |—BMT(Bean Managed Transaction)
  |
  |—-JDBC Transaction
  |
  |—-JTA Transaction

Recommended Today

IOS overall framework and class inheritance framework diagram

I. overall framework In the process of IOS development, understanding and learning the overall framework of IOS is an essential link. Today, let’s have a good understanding of the overall framework of IOS. First, post a framework introduction about IOS:Overview of IOS system framework。 We usually call the IOS framework cocoa touch framework. Cocoa touch […]