Spring Mybatis随记

2023-12-15 18:34:05
  1. 连接池获取连接逻辑
com.zaxxer.hikari.pool.HikariPool#getConnection(long)
// 内部循环获取空闲连接,返回前检查数据库连接是否可以用

  1. Spring获取连接
    如果已经开启了事务,只要没提交事务,调用非事务的DAO执行sql也会被事务统一管理
org.springframework.jdbc.datasource.DataSourceTransactionManager#doBegin 
// 获取连接后将其设置为手动提交
  1. mybatis获取连接
org.mybatis.spring.transaction.SpringManagedTransaction#getConnection
// 一个线程里当前只有一个连接被org.springframework.transaction.support.TransactionSynchronizationManager管理
// 如果非事务的操作,或者事务已提交,该连接不会被Spring管理
org.mybatis.spring.transaction.SpringManagedTransaction#openConnection
  1. mybatis获取SqlSession
org.mybatis.spring.SqlSessionUtils#getSqlSession
// 使用Spring管理SqlSession
org.mybatis.spring.SqlSessionUtils#registerSessionHolder

mybatis通过SqlSession进行数据库操作;
spring中通过TransactionSynchronizationManager为每个线程对应的数据库连接维护了事务相关的信息

  1. 打印事务日志
<!-- hikari 日志-->
	<logger name="com.zaxxer" level="DEBUG" additivity="false">
		<appender-ref ref="console"/>
	</logger>

	<!-- 数据库连接 -->
	<logger name="org.springframework.jdbc" level="DEBUG" additivity="false">
		<appender-ref ref="console"/>
	</logger>

	<!--spring 事务相关-->
	<logger name="org.springframework.transaction" level="DEBUG" additivity="false">
		<appender-ref ref="console"/>
	</logger>

	<!-- mybatis 相关-->
	<logger name="org.mybatis.spring" level="DEBUG" additivity="false">
		<appender-ref ref="console"/>
	</logger>
  1. 打印SQL
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
	<!-- 开启业务mapper日志 -->
	<logger name="com.ibreeze" level="DEBUG" additivity="false">
		<appender-ref ref="console"/>
	</logger>
  1. 日志过程
// Spring JDBC获取连接
o.s.jdbc.support.JdbcTransactionManager  : Acquired Connection [HikariProxyConnection@748466085 wrapping com.mysql.cj.jdbc.ConnectionImpl@402d6012] for JDBC transaction
// 连接设置为手动提交
o.s.jdbc.support.JdbcTransactionManager  : Switching JDBC Connection [HikariProxyConnection@748466085 wrapping com.mysql.cj.jdbc.ConnectionImpl@402d6012] to manual commit
o.s.jdbc.support.JdbcTransactionManager  : Participating in existing transaction
org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils       : Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46c662f2]
c.j.b.c.m.c.MybatisPlusMetaObjectHandler : mybatis plus start insert fill ....
// myabtis.Spring
o.m.s.t.SpringManagedTransaction         : JDBC Connection [HikariProxyConnection@748466085 wrapping com.mysql.cj.jdbc.ConnectionImpl@402d6012] will be managed by Spring
// SqlSession CRUD
c.j.b.w.b.f.m.SysFormMapper2.insert      : ==>  Preparing: INSERT INTO bip_wf_form ( form_id, form_name, create_by, create_time, update_by, update_time ) VALUES ( ?, ?, ?, ?, ?, ? )
c.j.b.w.b.f.m.SysFormMapper2.insert      : ==> Parameters: 1702632127793(Long), in(String), null, 2023-12-15T17:22:07.793(LocalDateTime), null, 2023-12-15T17:22:07.844(LocalDateTime)
c.j.b.w.b.f.m.SysFormMapper2.insert      : <==    Updates: 1
// 释放SqlSession操作
org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46c662f2]
// 再次操作时重新获取SqlSession
org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46c662f2] from current transaction
com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Pool stats (total=1, active=1, idle=0, waiting=0)
c.j.b.w.b.f.m.S.selectCount              : ==>  Preparing: SELECT COUNT( * ) FROM bip_wf_form
c.j.b.w.b.f.m.S.selectCount              : ==> Parameters: 
com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@30cf72ed
com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - After adding stats (total=2, active=1, idle=1, waiting=0)
c.j.b.w.b.f.m.S.selectCount              : <==      Total: 1
org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46c662f2]

org.mybatis.spring.SqlSessionUtils       : Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46c662f2]
org.mybatis.spring.SqlSessionUtils       : Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46c662f2]
// 回滚数据库
o.s.jdbc.support.JdbcTransactionManager  : Initiating transaction rollback
o.s.jdbc.support.JdbcTransactionManager  : Rolling back JDBC transaction on Connection [HikariProxyConnection@748466085 wrapping com.mysql.cj.jdbc.ConnectionImpl@402d6012]
// 释放连接
o.s.jdbc.support.JdbcTransactionManager  : Releasing JDBC Connection [HikariProxyConnection@748466085 wrapping com.mysql.cj.jdbc.ConnectionImpl@402d6012] after transaction

文章来源:https://blog.csdn.net/adaivskean/article/details/135019035
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。