1. mybatis (SqlSessionTemplate)的动态代理

  a) sqlSession的结构

  

  b)SqlSession 结构

public class SqlSessionTemplate implements SqlSession {
//代理对象 ------1
private final SqlSession sqlSessionProxy;
/**
*//构造方法 --------2
*/
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
PersistenceExceptionTranslator exceptionTranslator) {
//利用jdk的动态代理,生成sqlSessionProxy的代理对象--------3
this.sqlSessionProxy = (SqlSession) newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
new Class[] { SqlSession.class },
new SqlSessionInterceptor());
} /**
*/
@Override
public <T> T selectOne(String statement) {
//代用代理对象的目标方法,触发代理对象的产生---------4
return this.sqlSessionProxy.<T> selectOne(statement);
} /**
*/
private class SqlSessionInterceptor implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
SqlSession sqlSession = getSqlSession(
SqlSessionTemplate.this.sqlSessionFactory,
SqlSessionTemplate.this.executorType,
SqlSessionTemplate.this.exceptionTranslator);
try {
//代理对象执行,实际上执行的是sqlSession的目标方法,proxy没有执行--------5
Object result = method.invoke(sqlSession, args);
if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
// force commit even on non-dirty sessions because some databases require
// a commit/rollback before calling close()
sqlSession.commit(true);
}
return result;
} catch (Throwable t) {
Throwable unwrapped = unwrapThrowable(t);
if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
// release the connection to avoid a deadlock if the translator is no loaded. See issue #22
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
sqlSession = null;
Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
if (translated != null) {
unwrapped = translated;
}
}
throw unwrapped;
} finally {
if (sqlSession != null) {
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
}
}
}
} } 

2.没有事务:切换数据源(成功切换数据源)

  a)代理对象执行目标时,切换数据源时 (栈结构)

  b) 在DataSourceUtils 中,判断conHolder是否为空,

3.存在事务:切换数据源(不会切换数据源)

  a)  在执行目标方式时,被spring拦截,检查是否存在事务,(存在事务,调用doBegin方法,为事务准备链接conncetion)

  b) 代理对象sqlSessionTemple 执行目标方法中,在DataSourceUtils 中判断connection 已经存在,则直接拿取

myBatis 切换数据源(spring事务)理解的更多相关文章

  1. Spring动态切换数据源及事务

    前段时间花了几天来解决公司框架ssm上事务问题.如果不动态切换数据源话,直接使用spring的事务配置,是完全没有问题的.由于框架用于各个项目的快速搭建,少去配置各个数据源配置xml文件等.采用了动态 ...

  2. 动态添加数据源,根据用户登录切换数据库.编程式Spring事务.

    根据用户注册,系统自动创建私有数据库,用户登录,动态添加数据源到Spring数据路由,Session超时删除数据源 好处:当数据量大的时候,类似水平切割效果,效率会高一些 坏处:数据源切换,Sprin ...

  3. 第6章 事务管理 6.1 spring事务

    事务管理——原子性.一致性.隔离性.持久性 理解spring对事务管理的支持 Spring提供对编码式和声明式事务管理的支持.编码式事务允许用户在代码中精确定义事务的边界,而声明式事务(基于AOP,面 ...

  4. spring+atomikos+mybatis 多数据源事务(动态切换)

    注:自动切换,是为不同的数据源,却要对应相同的dao层: 1.与无事务版的一样,创建DynamicDataSource类,继承AbstractRoutingDataSource package com ...

  5. spring+springMVC+Mybatis架构下采用AbstractRoutingDataSource、atomikos、JTA实现多数据源灵活切换以及分布式事务管理

    背景: 1.系统采用SSM架构.需要在10多个MYSQL数据库之间进行切换并对数据进行操作,上篇博文<springMVC+Mybatis(使用AbstractRoutingDataSource实 ...

  6. Spring AOP实现注解式的Mybatis多数据源切换

    一.为什么要使用多数据源切换? 多数据源切换是为了满足什么业务场景?正常情况下,一个微服务或者说一个WEB项目,在使用Mybatis作为数据库链接和操作框架的情况下通常只需要构建一个系统库,在该系统库 ...

  7. Spring+Mybatis多数据源的一种实现方式,支持事务

    最近一个项目用到了多个数据库,所以需要实现动态切换数据源来查询数据,http://www.cnblogs.com/lzrabbit/p/3750803.html这篇文章让我受益匪浅,提供了一种自动切换 ...

  8. spring mvc 多数据源切换,不支持事务控制[一]

    一个项目中需要使用两个数据库,Oracle 和Mysql ,于是参考各个blog,实现此功能.写好后才发现,原来的事务失效了,我去... spring-mybatis.xml 配置 <bean ...

  9. Spring+Mybatis动态切换数据源

    功能需求是公司要做一个大的运营平台: 1.运营平台有自身的数据库,维护用户.角色.菜单.部分以及权限等基本功能. 2.运营平台还需要提供其他不同服务(服务A,服务B)的后台运营,服务A.服务B的数据库 ...

随机推荐

  1. python并发编程之Queue线程、进程、协程通信(五)

    单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...

  2. Tutorial 1: Serialization

    转载自:http://www.django-rest-framework.org/tutorial/1-serialization/#tutorial-1-serialization Tutorial ...

  3. PTP简介

    PTP简介 在通信网络中,许多业务的正常运行都要求网络时钟同步,即整个网络各设备之间的时间或频率差保持在合理的误差水平内.网络时钟同步包括以下两个概念: l              时间同步:也叫相 ...

  4. 连接数据库:ERROR:The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration prop

    本打算在maven项目中配置mybatis试试看,想到mybatis如果不是在容器中运行,那么他的事务控制实际上可以使用的是jdbc的提交和回滚,这就要在pom.xml文件中配置mysql-conne ...

  5. 20165301陈潭飞2017-2018-2 20165301 实验三《Java面向对象程序设计》实验报告

    2017-2018-2 20165301 实验三<Java面向对象程序设计>实验报告 一.敏捷开发与XP实践-1 实验要求: 在IDEA中使用工具(Code->Reformate C ...

  6. 20165301 2017-2018-2 《Java程序设计》第八周学习总结

    20165301 2017-2018-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十二章:Java多线程机制 进程与线程 操作系统与进程:进程是程序的一次动态执行过程. 进 ...

  7. Python+Selenium 自动化实现实例-模块化调用

    public 目录存一些公共模块,供用例调用.login.py 内容如下: # coding=utf-8 import time # login def login(driver): driver.f ...

  8. 渗透常用SQL注入语句合集

    1.判断有无注入点; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等..and 0<>(select ...

  9. 包含Winsock2.h出错问题

    工程中添加 Winsock2.h 报错 1>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(100): warni ...

  10. ceph笔记(一)

    一.ceph概述本质上是rados:可靠的.自动的.分布式对象存储特性:高效性(大型的网络raid,性能无限接近raid).统一性(支持文件存储.块存储.对象存储).可扩展性数据库的一个弱点:查表ce ...