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
- Public A bussinessmethod (){
- Daoa A = new daoa ();
- A. udpate ();
- Daob B = new daob ();
- B. update;
- }
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
- Public connection getconnection (string username, string password)
- Throws sqlexception {
- Connection con = drivermanager
- . Getconnection (constr, username, password );
- Return con;
- }
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.
- Public A bussinessmethod (){
- Daoa A = new daoa ();
- A. begintx ();
- A. udpate ();
- A. endtx ();
- A. Close ();
- Daob B = new daob ();
- B. begintx ();
- B. update;
- B. endtx ();
- B. Close ();
- }
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
- Private Static threadlocal <connection> localcon = new threadlocal <connection> ();
- Public final static connection getconnection () throws sqlexception {
- Connection con = localcon. Get ();
- If (con = NULL | con. isclosed ()){
- Con = new oraclejdbcdatasource (). getconnection ();
- Localcon. Set (CON );
- }
- Return con;
- }
Copy code
In this way, the above problems are implemented.
[5] New Transaction Management
- Public A bussinessmethod (){
- Daoa A = new daoa ();
- Daob B = new daob ();
- A. begintx ();
- A. udpate ();
- B. begintx ();
- B. update;
- A. endtx ();
- A. Close ();
- // B. endtx ();
- // B. Close ();
- Achieve the same effect
- }
Copy code
You can manage transactions in a unified manner.