Hibernate framework Study Notes 6: transactions, hibernate Study Notes

Source: Internet
Author: User

Hibernate framework Study Notes 6: transactions, hibernate Study Notes

MySQL transactions and JDBC transaction operations:

For details, see this article: More detailed

Http://www.cnblogs.com/xuyiqing/p/8430214.html

 

How to configure the isolation level in hibernate:

In the core configuration file:

<! -- Specifies the isolation level when hibernate operates the database # hibernate. connection. isolation 1 | 2 | 4 | 8 0001 1 read not submitted 0010 2 read committed 0100 4 Repeatable read 1000 8 serialized --> <property name = "hibernate. connection. isolation "> 4 </property>

 

Here it is binary, which is converted to decimal 1, 2, 4, 8

 

Manage transactions in a project:

Before learning the hibernate framework, in the project, start the transaction at the business layer (service), submit or roll back after execution

In the hibernate framework, this is also the case. The session object required for Database Operations must ensure that the session at the service layer and dao layer is the same

Similar to servlet projects, you need to ensure that the connection objects of the service layer and dao layer are consistent. At that time, the binding thread is used.

(This is a tool class in the previous sevlet project, which can be viewed and compared)

Package utils; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import javax. SQL. dataSource; import com. mchange. v2.c3p0. comboPooledDataSource; public class performanceutils {private static DataSource dataSource = new ComboPooledDataSource (); private static ThreadLocal <Connection> tl = new ThreadLocal <Connection> (); // you can directly obtain a connection pool public static DataSource getDataSource () {return dataSource;} // gets the Connection object public static Connection getConnection () throws SQLException {Connection con = tl. get (); if (con = null) {con = dataSource. getConnection (); tl. set (con) ;}return con ;}// enable the public static void startTransaction () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. setAutoCommit (false) ;}// transaction rollback public static void rollback () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. rollback () ;}/// submit and close resources and release public static void commitAndRelease () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. commit (); // transaction commit con. close (); // close the resource tl. remove (); // remove from thread binding} // close the resource method public static void closeConnection () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. close () ;}} public static void closeStatement (Statement st) throws SQLException {if (st! = Null) {st. close () ;}} public static void closeResultSet (ResultSet rs) throws SQLException {if (rs! = Null) {rs. close ();}}}
View Code

 

In the hibernate framework, the principle is the same, and the thread is bound.

However, you only need to configure in the core configuration file:

<! -- Bind the session to the current thread --> <property name = "hibernate. current_session_context_class"> thread </property>

 

The Code only needs one line:

                SessionFactory sf = new Configuration().configure().buildSessionFactory();                sf.getCurrentSession();

 

A simple test can be understood as follows:

Package demo; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. junit. test; import utils. hibernateUtils; // Test getCurrentSessionpublic class Demo {@ Test // return the same thread-bound session public void fun1 () {Session session1 = HibernateUtils. getCurrentSession (); Session session2 = HibernateUtils. getCurrentSession (); System. out. println (session1 = session2); // true} @ Test // different session public void fun2 () {Session session1 = HibernateUtils. openSession (); Session session2 = HibernateUtils. openSession (); System. out. println (session1 = session2); // false }}

Add tool class:

Package utils; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; public class HibernateUtils {private static SessionFactory sf; static {// 1 Creation, call the null parameter to construct Configuration conf = new Configuration (). configure (); // 2 create the SessionFactory object sf = conf Based on the configuration information. buildSessionFactory ();} // get session => get new session public static Session openSession () {// 3 get session Session session = sf. openSession (); return session;} // get session => get the bound session public static Session getCurrentSession () {// 3 get session Session session = sf. getCurrentSession (); return session ;}}
View Code

 

 

Note:

The session object obtained through the getCurrentSession method will be automatically closed when the transaction is committed. Do not close the session manually; otherwise, an exception will occur.

 

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.