Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Hibernate transaction

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you a sample analysis of Hibernate transactions, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Hibernate is a lightweight encapsulation of JDBC and does not have transaction management capabilities. In the transaction management layer, Hibernate delegates it to the underlying JDBC or JTA to implement transaction management and scheduling functions.

Hibernate transaction mechanism is based on JDBC Transaction. We can also use JTA as the transaction management implementation through the configuration file:

…… Net.sf.hibernate.transaction.JTATransactionFactory...

Transaction management based on JDBC is undoubtedly the simplest way to delegate transaction management to JDBC for processing, and the encapsulation of Hibernate transactions for JDBC transactions is also very simple.

Let's look at the following code:

Session = sessionFactory.openSession (); Transaction tx = session.beginTransaction (); … Tx.commit ()

At the JDBC level, the above code actually corresponds to:

Connection dbconn = getConnection (); dbconn.setAutoCommit (false); … Dbconn.commit ()

As simple as that, Hibernate doesn't do much (and can't actually do more), just encapsulating such JDBC code.

Note here that in sessionFactory.openSession (), hibernate initializes the database connection while setting its AutoCommit to false. Then, in the Session.beginTransaction method, Hibernate reconfirms that the AutoCommit property of Connection is set to off (to prevent user code from modifying the Connection.AutoCommit property of session).

That is to say, the autocommit property of the session we obtained from SessionFactory has been turned off (AutoCommit=false), and the following code will have no effect on the database:

Session = sessionFactory.openSession (); session.save (user); session.close ()

This is actually equivalent to setting the AutoCommit property of JDBC Connection to false, and after performing several JDBC operations, the commit operation is not called to shut down the Connection. If we want the code to actually work on the database, we must explicitly call the Transaction instruction:

Session = sessionFactory.openSession (); Transaction tx = session.beginTransaction (); session.save (user); tx.commit (); session.close (); this is all the content of the article "sample Analysis of Hibernate transactions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report