首页 > 数据库 >MyBatis-06-Spring的SqlSession和原始区别

MyBatis-06-Spring的SqlSession和原始区别

时间:2024-04-16 22:57:16浏览次数:27  
标签:sqlSessionFactory 06 Spring executorType sqlSession SqlSession SqlSessionTemplat

DefaultSqlSession

这个就不说了,SQL 执行是调用执行器 Executor 执行

SqlSessionTemplate

构造函数,虽然没有立即创建 SqlSession 传入代理拦截器 SqlSessionInterceptor,但是拦截器是一个实例内部类,可以访问到 SqlSessionFactory
并且 SqlSessionTemplate 不支持 commit、rollback、close 操作(UnsupportedOperationException)

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
    PersistenceExceptionTranslator exceptionTranslator) {

  notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
  notNull(executorType, "Property 'executorType' is required");

  this.sqlSessionFactory = sqlSessionFactory;
  this.executorType = executorType;
  this.exceptionTranslator = exceptionTranslator;
  this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),
      new Class[] { SqlSession.class }, new SqlSessionInterceptor());
}

执行时,以 selectOne 举例,是代理到 SqlSessionProxy 处理的,而 SqlSessionProxy 会先从 Spring 的事务上下文获取 SqlSession,没有获取到就新建 SqlSession 并绑定到事务上下文
本质上还是使用 DefaultSqlSession 来处理,并且若是在 Spring 事务管理下,则执行过后不会提交

SqlSessionInterceptor

SqlSessionProxy 的拦截

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

标签:sqlSessionFactory,06,Spring,executorType,sqlSession,SqlSession,SqlSessionTemplat
From: https://www.cnblogs.com/chenxingyang/p/18139464

相关文章

  • MyBatis-08-Spring的MyBatis Interceptor
    addInterceptor3个地方XML解析的SqlSessionFactoryBean:生成SqlSession的FactoryBeanPageHelperAutoConfiguration:分页助手的自动配置SqlSessionFactoryBean发现现在都没有将他作为一个FactoryBean使用了getObject调用了afterPropertiesSet生成SqlSessionF......
  • 31.SpringBoot项目_员工管理系统总结 下
    本来想好好整理最后结束的奈何又接了个项目一个挺大的项目更能运用所以时间不够就简单整理算烂了个尾吧首先:1.人工AI取代程序员对我来说是不对的确实是骗外行的但其确实强大带来的便利但也只有程序员才能完美部署并运用它2.有多少人像我一样不喜欢wordexcelppt喜......
  • springboot简单正确的使用构造函数注入
    一个一个写构造函数太麻烦了,而且代码会显得非常多,这里我们可以采用lombok快捷注入但是我们并不是所有的成员变量都需要进行注入,所以使用@RequiredArgsConstrucotr需要构造函数的部分添加上final关键字"Alwaysuseconstructorbaseddependencyinjectioninyourbeans.Alwa......
  • SpringBoot 事务失效常见的原因
    low一点的原因:1.非public方法2.异常被处理了(trycatch)3.bean没被Spring管理,比如没加Component注解4.数据库不支持(比如MyISAM) 高大上一点的原因:5.内部方法调用,可以注入本实例的代理类,通过代理类来调用6.抛出的是非RuntimeException,可以这么解决(@Transactional(rollb......
  • 初次使用SpringBoot3遇到的一些问题
    打开网址https://start.spring.io/  选择版本依赖等信息 本机环境原先装了个jdk8,由于SpringBoot3最低支持jdk17,所以本机环境上又装了个jdk17   https://www.oracle.com/java/technologies/downloads/#java17出于怀旧,jdk8还保留着,并且环境变量还是jdk8,只是在idea里吧......
  • 30.swagger springboot中简单整理
    类似postman接口在线测试API接口哈哈我也不太懂接口还没前后端整合过呢也就是注释你的各种接口吧可有可无先说依赖问题这个是swagger2的依赖当然出现问题了已经更新到了swagger3了参考:https://cloud.tencent.com/developer/article/1936862https://mvnrepos......
  • SpringBoot+SpringSecurity6+Jwt最小化代码
    SpringBoot+SpringSecurity6+Jwt最小化代码[toc]零、参考资料https://blog.csdn.net/m0_71273766/article/details/132942056一、快速开始1.1、引入依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0&quo......
  • 06 Vue3插槽
    6.9.【slot】1.默认插槽父组件中:<Categorytitle="今日热门游戏"><ul><liv-for="gingames":key="g.id">{{g.name}}</li></ul></Category>子组件中:......
  • RocketMQ集成SpringBoot
    踩了3天的坑,发现使用rocketMQTemplte消费者一直注册不上,发现是springboot版本问题1.版本选择经过不严格测试发现rocketmq-spring-boot-starter2.3.0支持springboot3以上rocketmq-spring-boot-starter2.2.x只能使用3以下,高版本会导致消费者注册不上的问题2.application.y......
  • 微服务Spring Cloud17_服务调用方式2
    一、RPC和HTTP无论是微服务还是SOA,都面临着服务间的远程调用。那么服务间的远程调用方式有哪些呢?常见的远程调用方式有以下2种:RPC:RemoteProduceCall远程过程调用,RPC基于Socket,工作在会话层。自定义数据格式,速度快,效率高。早期的webservice,现在热门的dubbo,都是RPC的典型......