Eclipse Development Hibernate Application _jsp programming

Source: Internet
Author: User
Tags flush

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 (); (corresponding to the left of the two sentences)
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.

   Second, jtatransaction

If you use hibernate in EJBs, or if you are ready to use JTA to manage long transactions across sessions, then you need to use jtatransaction to see an example:

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 the standard snippet for using JTA, transaction is a cross session, and its lifecycle is longer than the session. If you use Hibernate in EJB, then it is the simplest, you do not write any transaction code, directly on the EJB deployment descriptor to configure a certain method to use transactions.

Now let's analyze the source code for jtatransaction, Net.sf.hibernate.transaction.JTATransaction:

public void begin (InitialContext context, ...)
...
UT = (usertransaction) context.lookup (utname);
...

Do you see it clearly? And I wrote the code tx = new Initial context? (). Lookup ("Javax.transaction.UserTransaction"); Is it exactly the same?

public void commit () ...
...
if (newtransaction) ut.commit ();
...

Jtatransaction's control is slightly more complex, but it's still clear how hibernate encapsulates JTA's transaction code.

But do you see any problems now? Think carefully, Hibernate transaction is obtained from the session, TX = Session.begintransaction (), the last to be submitted to TX, And then Session.close, which completely conforms to the transaction order of the JDBC, but this order is completely contradictory to the JTA transactioin sequence of operations!!! JTA is to start the transaction, then start the session, close the session, and finally submit the transaction, so when you use JTA transaction, Then do not use the Hibernate transaction, but should be like the JTA code snippet above me to use the line.

Summarize:

1, the use of hibernate on JDBC must write hibernate transaction code, otherwise the database does not respond. At this point, Hibernate's transaction is connection.commit.

2, on the JTA use hibernate write JTA transaction code, do not write hibernate code transaction, otherwise the program will report an error

3, on the EJB use hibernate what transactioin code do not write, in the EJB deployment descriptor inside the configuration

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.