In transaction ThreadLocal Use of

Simple understanding ThreadLocal

so-called ThreadLocal, Think simply , It's a global one Map,Map Of key Is a thread object ,value It's the object you want to save
After entering a thread , You can start from map Gets the object associated with the corresponding thread stored previously .
NT:ThreadLocal Not one Map, But with Map There is no problem in understanding

Use in transactions ThreadLocal

Make sure that all sql Statements are executed on the same link that opened the transaction , It can be used at this time ThreadLocal To solve this problem .
Solutions :

code implementation :
// Binding enabled linked connection reach ThreadLocal Of JdbcUtils Tools public class JdbcUtils { private
static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); //map public
static void startTransaction(){ try{ // Get the binding connection on the current thread to open the transaction Connection conn = tl.get
();if(conn==null){ // There is no binding on the thread conn = ds.getConnection(); //ds Representative data source , Omit his code here
tl.set(conn); } conn.setAutoCommit(false); }catch (Exception e) { throw new
RuntimeException(e); } }public static void commitTransaction(){ try{ Connection
conn = tl.get(); if(conn!=null){ conn.commit(); } }catch (Exception e) { throw
new RuntimeException(e); } } public static Connection getConnection() throws
SQLException{try{ // Gets the connection bound on the current thread Connection conn = tl.get(); if(conn==null){
// Represents that there is no bound connection on the thread conn = ds.getConnection(); tl.set(conn); } return conn; }catch
(Exception e) {throw new RuntimeException(e); } } } // On the service layer ThreadLocal Transaction management of
public void transfer2(int sourceid,int targetid,double money) throws
SQLException{try{ JdbcUtils.startTransaction(); AccountDao dao = new
AccountDao(); Account a = dao.find(sourceid);//select Account b =
dao.find(targetid);//select a.setMoney(a.getMoney()-money);
b.setMoney(b.getMoney()+money); dao.update(a);//update dao.update(b);//update
JdbcUtils.commitTransaction(); }finally{ JdbcUtils.closeConnection(); } } }
// Persistent layer , Object bound to the current thread connection, Operate public Account find(int id){ try{ QueryRunner
runner =new QueryRunner(); String sql = "select * from account where id=?";
return (Account) runner.query(JdbcUtils.getConnection(),sql, id, new
BeanHandler(Account.class)); }catch (Exception e) { throw new
RuntimeException(e); } }
use ThreadLocal What should be paid attention to

In the code above, the ThreadLocal The object is made static
private static ThreadLocal tl = new ThreadLocal(); //map

therefore , Start one thread at a time ,ThreadLocal There will be one Connection Add in , therefore , We have to remember to put these Connection remove , Otherwise
this ThreadLocal Static objects get bigger and bigger , Finally, it causes the program to hang up !!!!!!

-> Namely , When closing the link is , Remove variables
public static void closeConnection(){ try{ Connection conn = tl.get(); if
(conn!=null){ conn.close(); } }catch (Exception e) { throw new
RuntimeException(e); }finally{ tl.remove();
// Be careful , Unlinks the binding on the current thread ( from threadlocal Remove the link corresponding to the current thread from the container ) } }

©2019-2020 Toolsou All rights reserved,
Java Swing JList: List box components Accurate mobile phone number capture , Big data of operators The difference between memory overflow and memory leak , Causes and Solutions Website mobile phone number capture method stay Vue Use in Web Worker3 species Python data structure ,13 Creation methods , This is the conclusion , Great ! What's the difference in server prices ? Basic definition and use of function ‘ To be continued SQL Server Database Glossary Chrome OS, For programmers and Windows What does it mean ? Internet Marketing