Threadlocal and transaction

Source: Internet
Author: User

Place the connection in threadlocal to ensure that the DAO class in the transaction obtains the same connection, so as to ensure the transaction.

Below is a specific example of http://www.pin5i.com/showtopic-26854.html found online

Bytes -----------------------------------------------------------------------------------

Generally, we place the transaction processing on the business layer.

[1] Introduction

For example:
Business logic method

  1. Public A bussinessmethod (){
  2. Daoa A = new daoa ();
  3. A. udpate ();
  4. Daob B = new daob ();
  5. B. update;
  6. }

Copy code

Assume that the preceding transaction is automatically committed.

So we can leave it alone. However, there is a problem. When creating a DaO object, the two objects use different connection objects.

Then let's assume that our connection getting code is

  1. Public connection getconnection (string username, string password)
  2. Throws sqlexception {
  3. Connection con = drivermanager
  4. . Getconnection (constr, username, password );
  5. Return con;
  6. }

Copy code

A call to this method.

If we need to control the start and end of the transaction.

How to control it.

Maybe a new method is available.

  1. Public A bussinessmethod (){
  2. Daoa A = new daoa ();
  3. A. begintx ();
  4. A. udpate ();
  5. A. endtx ();
  6. A. Close ();
  7. Daob B = new daob ();
  8. B. begintx ();
  9. B. update;
  10. B. endtx ();
  11. B. Close ();
  12. }

Copy code

In this way, we control transactions, and we can only achieve this, because different Dao gets different connection objects.

[2] Problems

So how can two or more Dao be controlled using the same transaction?

First, we need to solve a fundamental problem. In a transaction, different daos obtain the same connection. How can we achieve this. We thought of theadlocal.

[3] Solution

Because when a business logic needs to process multiple Dao statements, this operation belongs to the same thread. Therefore, we hope to put this connection in the thread or be associated with the thread.

The first scheme can be implemented by map.

Is Map <thread, connection>

However, this map is difficult to maintain. If it is put all the time, the map will become larger and larger as the system runs.

The second solution is to use threadlocal

Threadlocal can be used in any thread to save the copy of a variable. In this way, if this object exists, it can be directly used.

The same applies if the connection exists in threadlocal.

Therefore, the same connection can be obtained in the same thread.

[4] Implementation

  1. Private Static threadlocal <connection> localcon = new threadlocal <connection> ();
  2. Public final static connection getconnection () throws sqlexception {
  3. Connection con = localcon. Get ();
  4. If (con = NULL | con. isclosed ()){
  5. Con = new oraclejdbcdatasource (). getconnection ();
  6. Localcon. Set (CON );
  7. }
  8. Return con;
  9. }

Copy code

In this way, the above problems are implemented.

[5] New Transaction Management

  1. Public A bussinessmethod (){
  2. Daoa A = new daoa ();
  3. Daob B = new daob ();
  4. A. begintx ();
  5. A. udpate ();
  6. B. begintx ();
  7. B. update;
  8. A. endtx ();
  9. A. Close ();
  10. // B. endtx ();
  11. // B. Close ();
  12. Achieve the same effect
  13. }

Copy code

You can manage transactions in a unified manner.

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.