Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Hibernate transaction

Shulou Source: shulou.com Published: 2022-06-02 08:02:30 11月02日 Update

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!

Tags: Transactions code management transaction management attributes actual actually data databases more articles encapsulation examples analysis things content status faceted processing delegation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Apple Shulou Information Shulou Tech Info