SSH [SSH] the Hibernate framework: Transaction Management and thread-bound Session

Affairs

1, the transaction : a database operation unit executing a program of various data items.

2, the transaction characteristics :

  • Atomicity: the database transaction is the smallest unit of execution of a series of operations, can not be divided;
  • Consistency: the operations in one transaction at the same time either succeed or fail at the same time;
  • Isolation: operating a transaction without interference from other transactions;
  • Persistence: Once you have submitted the transaction, can not be rolled back.

3, concurrent operation of its services possible problems :

  • Dirty read: A transaction to read another transaction has been updated but did not submit data;
  • Non-repeatable read: a transaction reads the intermediate data update operation twice another transaction;
  • Magic Reading: a transaction data reading operation of the insertion in the middle of two other transaction.

4, transaction isolation level :

  • Read uncommitted (read uncommitted): dirty reads, non-repeatable reads and phantom reads can occur.
  • Read committed (read submission): avoid dirty reads, but non-repeatable read and phantom read may occur.
  • Repeatable read (repeatable read): avoid dirty reads and non-repeatable reads, but phantom reads can occur.
  • The Serializable (serialization): avoid dirty reads, non-repeatable read and phantom read, but low concurrency, seldom used.

Hibernate transaction management

1, open transaction : the Transaction session.beginTransaction TS = ();
2, roll back the transaction : ts.rollBack ();
. 3, uncommitted transactions : ts.commit ();
. 4, configured transaction isolation level : in hibernate.cfg. xml configuration file

  • 1:Read uncommitted isolation
  • 2:Read committed isolation
  • 4:Repeatable read isolation
  • 8:Serializable isolation
<!--例:设置隔离级别为:Repeatable read isolation-->
<property name="hibernate.connection.isolation">4</property>

Thread-bound Session

1 reason : the transaction is usually provided in the service layer, because one is a specific operation of the database layer processing dao, if each operation plus a transaction manager makes no sense, because the transaction is to ensure the success or failure of multiple operations simultaneously. However, each operating a transaction must be in the same session, which requires each method dao transaction layer is obtained with a session object.
2, to solve : making a transaction dao layer of each method to obtain the same session object, there are two practices:

  • 1, the layer created service session object is passed to each transaction method dao layer;
  • 2, the service session object bound to a ThreadLocal layer are acquired from the session object in each method ThreadLocal dao layer. Since each operation in the transaction are in the same thread, so get anywhere in this thread session object will be the same.

3, to solve the Hibernate : Hibernate solve the above problem using the second method, but Hibernate is a very sound framework that the bottom has been good for us to bind the session object, simply call the SessionFactory getCurrentSession () method on binding can get a good session object. However, doing so also requires a premise that is configured in hibernate.cfg.xml.
In the Hibernate configuration file, with hibernate.current_session_context_class attribute to specify the session management methods, management in the following ways:

  • thread: Session object life cycle of the local thread-bound;
  • jta: Session with a JTA transaction life cycle bindings for cross-database transaction management;
  • managed: Hibernate entrust program to manage the life cycle of the Session object.
<!--例:Session对象的生命周期与本地线程绑定-->
<property name="hibernate.current_session_context_class">thread</property>
发布了128 篇原创文章 · 获赞 17 · 访问量 2725

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104234070