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 ) } }

Technology
©2019-2020 Toolsou All rights reserved,
c Language implementation 《 Student management system 》 No hole is the future of mobile phone ? There are still many problems to be solved Junior , A little sense , Just for mutual encouragement How to use Vue Used in Echarts Visual Library The 11th Blue Bridge Cup Java The second provincial competition B Group part Python- be based on OpenCV Contour fill for flooding algorithm hole filling 【C#】 The realization of student achievement information management system List Common interview questions in the collection and simple ideas China Mobile Science Popularization : Why do mobile networks call “ Cellular mobile network ”【Golang Basic series 10 】Go language On conditional sentences if