Spring 对hibernate事务处理的实现过程(辅助) ThreadLocal
来源:互联网 发布:js实现dos窗口效果 编辑:程序博客网 时间:2024/05/22 13:17
spring在事物处理的时候,会把ConnectionHolder和SessionHolder与线程绑定,使用的是ThreadLocal,下面看看ThreadLocal的实现是怎样做到线程间隔离的
先看看ThreadLocal 的几个主要方法
1 get方法
/** * Returns the value in the current thread's copy of this * thread-local variable. If the variable has no value for the * current thread, it is first initialized to the value returned * by an invocation of the {@link #initialValue} method. * * @return the current thread's value of this thread-local */ public T get() { Thread t = Thread.currentThread(); // 获取当前线程 ThreadLocalMap map = getMap(t); //获取当前线程的threadLocals 实际是ThreadLocal 的内部类ThreadLocal.ThreadLocalMap if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); // 从当前线程的threadLocals中取出对象,使用的key是当前的ThreadLocal实例 if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); // 如果当前线程的threadLocals还没有初始化,则初始化 }
2setInitialValue 方法
/** * Variant of set() to establish initialValue. Used instead * of set() in case user has overridden the set() method. * * @return the initial value */ private T setInitialValue() { T value = initialValue(); //初始值,默认返回null,可以根据需要重写实现 Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); // 根据初始值创建<span style="font-family: Arial, Helvetica, sans-serif;">ThreadLocal.ThreadLocalMap实例</span> if (map != null) map.set(this, value); else createMap(t, value); return value; }
/** * Sets the current thread's copy of this thread-local variable * to the specified value. Most subclasses will have no need to * override this method, relying solely on the {@link #initialValue} * method to set the values of thread-locals. * * @param value the value to be stored in the current thread's copy of * this thread-local. */ public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); }
/* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null;
0 0
- Spring 对hibernate事务处理的实现过程(辅助) ThreadLocal
- Spring 对hibernate事务处理的实现过程(1) hibernate 自己是怎么做的
- Spring 对hibernate事务处理的实现过程(3) spring怎么commit和rollback的
- Spring 对hibernate事务处理的实现过程(2) spring怎么获取hibernate使用的session并开启事务的
- Spring事务处理-ThreadLocal的使用
- (十一)Spring事务处理 - 事务处理的过程
- Spring事务处理的实现
- Spring事务处理的实现
- (十二)Spring事务处理 - 事务处理的实现
- 利用ThreadLocal & Filter 实现事务处理
- Spring+Hibernate事务处理,很简单的例子
- spring事务处理:调用一个方法前的事务处理过程
- spring集成hibernate事务处理
- spring+hibernate事务处理
- spring 配置hibernate事务处理
- Spring框架学习【Spring事务处理的实现】
- 事务处理中的ThreadLocal的使用
- Spring的事务处理机制 ---以及spring整合hibernate事务管理配置
- 螺旋方阵
- Caffe 深度学习框架上手教程
- 第七周项目2:求两点的距离(3)一般函数
- windows10连接蓝牙耳机
- 图片库
- Spring 对hibernate事务处理的实现过程(辅助) ThreadLocal
- filter 、map 、reduse、sorted 作用、特性及实例
- 从加载图片OOM说起
- iOS开发工具-网络封包分析工具Charles
- nginx安装
- 第七周上机实践项目-项目一-成员函数,友元函数和一般函数的区别
- form表单中用户名和密码对齐的简单实现
- 深度学习-LeCun、Bengio和Hinton的联合综述-Deep Learning一篇nature综述论文
- 欧拉函数(转)