zoukankan      html  css  js  c++  java
  • ThreadLocal 解决多线程程序的并发问题+事务处理

    ThreadLocal 本地线程变量:

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
      tl.get():获取的就是当前线程中map{tl:conn}中的conn
      tl.set(conn):向当前线程中的map中保存一个元素{tl:conn}
      tl.remove():从当前线程中的map移除key为tl的元素
      initialValue():当调用get()获取当前线程的map中元素时,如果不存在,则调用该方法创建一个,并保存到map中

     

     1 import java.sql.Connection;
     2 import java.sql.SQLException;
     3 
     4 public class TranManager {
     5     private static ThreadLocal<Connection> tl=
     6                     new ThreadLocal<Connection>(){
     7         /**
     8          * 内部方法 
     9          * 返回回该线程局部变量的初始值
    10          * 初始化线程,每次get()或者set(object)的时候会被调用。
    11          */
    12         protected Connection initialValue() {
    13             try {
    14                 return DaoUtils.getConn();
    15             } catch (Exception e) {
    16                 e.printStackTrace();
    17                 return null;
    18             }
    19         }
    20     };
    21     private TranManager(){}
    22     public static  Connection getConn(){
    23         return tl.get();
    24     }
    25     /**
    26      * @return 开启手动提交事务,
    27      * @false: 将sql命令交给应用程序管理 
    28      */
    29     public static void startTran(){
    30         try {
    31             tl.get().setAutoCommit(false);
    32         } catch (SQLException e) {
    33             e.printStackTrace();
    34         }
    35     }
    36     /**
    37      * @return 回滚
    38      */
    39     public static void rollbackTran(){
    40         try {
    41             tl.get().rollback();
    42         } catch (SQLException e) {
    43             e.printStackTrace();
    44         }
    45     }
    46     /**
    47      * @return 执行
    48      */
    49     public static void commitTran(){
    50         try {
    51             tl.get().commit();
    52         } catch (SQLException e) {
    53             e.printStackTrace();
    54         }
    55     }
    56     /**
    57      * @return 关闭
    58      */
    59     public static void release(){
    60         try {
    61             tl.get().close();
    62             tl.remove();//map{tl:conn}
    63         } catch (SQLException e) {
    64             e.printStackTrace();
    65         }
    66     }
    67     /*public void rollbackTran(Savepoint sp){
    68         try {
    69             conn.rollback(sp);
    70             conn.commit();
    71         } catch (SQLException e) {
    72             e.printStackTrace();
    73         }
    74     }*/
    75 }
  • 相关阅读:
    1、编写一个简单的C++程序
    96. Unique Binary Search Trees
    python 操作redis
    json.loads的一个很有意思的现象
    No changes detected
    leetcode 127 wordladder
    django uwsgi websocket踩坑
    you need to build uWSGI with SSL support to use the websocket handshake api function !!!
    pyinstaller 出现str error
    数据库的读现象
  • 原文地址:https://www.cnblogs.com/pxffly/p/7622216.html
Copyright © 2011-2022 走看看