Mybatis中的一級緩存和二級緩存(本博文只是針對一級緩存說明)
概述
ORM框架一般都會有緩存機制,做為其中一員的Mybatis也存在緩存。功能是用以提升查詢的效率和服務給數據庫帶來壓力。同樣的Mybatis也存在有一級緩存和二級緩存,並且預留了集成第三方緩存的接口類:【cache】。
緩存角色
【一級緩存】
- MyBatis默認支持一級緩存。在沒有任何配置情況下,默認開啟一級緩存,MyBatis 的一級緩存是在會話(SqlSession)層面進行緩存的;
- 如果同樣SqlSession對象查詢相同數據,則只會查詢一次。因在第一次查詢時向數據庫發送SQL語句,並將查詢結果放入到SQLSESSION中(作為緩存存在)。后續在此做相同對象查詢時,則直接從緩存中查詢該對象即可(省略了訪問數據庫操作,提高查詢效率);
- Mybatis開啟一個數據庫會話時,會創建一個新的SqlSession對象表示一次數據庫會話,SqlSession對象中會有一個新的Executor對象。Executor對象持有一個新的PerpetualCache對象;會話結束時,SqllSession對象及其內部的 Executor 對象還有PerpetualCache對象都會一同釋放掉;
- 如SqlSession調用close()方法,會釋放掉一級緩存PerpetualCache對象,一級緩存將不可用;
- 如SqlSession調用clearCache()方法,會清空PerpetualCache對象中數據,但該對象仍可使用;
- SqlSession中insert、update及delete,都會清空PerpetualCache對象的數據,但該對象仍可繼續使用。
一級緩存執行流程
MyBatis回話流程:當客戶端開啟一次數據回話SqlSession對象中創建一個本地緩存(local cache)。執行查詢前嘗試從本地緩存中查找是否存在,如果存在本地緩存中,就直接從緩存中取出,然后返回給客戶端。否則,從數據庫讀取數據,將查詢結果存入緩存並返回給用戶。
驗證(下面通過代碼進行實際驗證):
1、pom文件
2、數據庫配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis01"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/cache/mapper/personMapper.xml"/> </mappers> </configuration>
3、實體類
public class Person { /* 人員ID */ private int id; /* 人員名稱 */ private String name; /* 人員年齡 */ private int age; /* 人員性別 */ private Boolean sex; public Person() { } public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public Person(int id, String name, int age, Boolean sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
4、接口類
package com.cache.mapper; import com.cache.bean.Person; import java.util.List; // 操作mybatis接口 public interface PersonMapper { /* 根據ID查詢人員信息 */ Person queryPersonById(int id); /* 根據ID修改人員信息 */ void updateStudentById(Person person); }
5、接口對應mapper類
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cache.mapper.PersonMapper"> <select id="queryPersonById" parameterType="int" resultType="com.cache.bean.Person"> select id,name,age from t_person where id = #{id} </select> <update id="updateStudentById" parameterType="com.cache.bean.Person"> UPDATE t_person SET NAME =#{name},age = #{age} where id =#{id} </update> </mapper>
6、數據庫表結構和數據
7、測試類
public class test { /** * 演示一級緩存:根據ID查詢人的信息 */ @Test public void test01() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); PersonMapper personMapper = session.getMapper(PersonMapper.class); /* 默認使用一級緩存演示 */ Person person = personMapper.queryPersonById(1001); System.out.println("初次查詢結果為:"+person); Person person01 = personMapper.queryPersonById(1001); System.out.println("第二次查詢結果為:"+person); session.close(); }
執行結果:
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=28708:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit-rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit5-rt.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\junit\junit\4.12\junit-4.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;D:\ideaworkspace\ProjectStudy\mybatis-cache-03\target\classes;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;D:\download\lib\mavenTollTransfer\mic-repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;D:\download\lib\mavenTollTransfer\mic-repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\download\lib\mavenTollTransfer\mic-repository\cglib\cglib\3.3.0\cglib-3.3.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\ow2\asm\asm\7.1\asm-7.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\net\logstash\logback\logstash-logback-encoder\5.3\logstash-logback-encoder-5.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.cache.test.test,test01 [lsjSso]2021-10-20 12:55:30,892-org.apache.ibatis.logging.LogFactory-0 [main]DEBUGorg.apache.ibatis.logging.LogFactory-Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. [lsjSso]2021-10-20 12:55:31,036-org.apache.ibatis.datasource.pooled.PooledDataSource-144 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,037-org.apache.ibatis.datasource.pooled.PooledDataSource-145 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,037-org.apache.ibatis.datasource.pooled.PooledDataSource-145 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,037-org.apache.ibatis.datasource.pooled.PooledDataSource-145 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 12:55:31,137-org.apache.ibatis.transaction.jdbc.JdbcTransaction-245 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Opening JDBC Connection [lsjSso]2021-10-20 12:55:31,414-org.apache.ibatis.datasource.pooled.PooledDataSource-522 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Created connection 52908367. [lsjSso]2021-10-20 12:55:31,415-org.apache.ibatis.transaction.jdbc.JdbcTransaction-523 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 12:55:31,418-com.cache.mapper.PersonMapper.queryPersonById-526 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Preparing: select id,name,age from t_person where id = ? [lsjSso]2021-10-20 12:55:31,459-com.cache.mapper.PersonMapper.queryPersonById-567 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Parameters: 1001(Integer) [lsjSso]2021-10-20 12:55:31,500-com.cache.mapper.PersonMapper.queryPersonById-608 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-<== Total: 1 初次查詢結果為:Person{id=1001, name='zhangsan', age=27} 第二次查詢結果為:Person{id=1001, name='zhangsan', age=27} [lsjSso]2021-10-20 12:55:31,501-org.apache.ibatis.transaction.jdbc.JdbcTransaction-609 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 12:55:31,502-org.apache.ibatis.transaction.jdbc.JdbcTransaction-610 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 12:55:31,502-org.apache.ibatis.datasource.pooled.PooledDataSource-610 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Returned connection 52908367 to pool. Process finished with exit code 0
分析:
分析一:查詢兩次結果只進行一次查詢
分析二:查詢了兩次執行結果
驗證執行,commit操作后緩存失效:
第一:測試代碼修改update操作后再查詢兩次,其他保持不變:
A、修改測試代碼為:
@Test public void test01() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); PersonMapper personMapper = session.getMapper(PersonMapper.class); /* 默認使用一級緩存演示 Person person = personMapper.queryPersonById(1001); System.out.println("初次查詢結果為:"+person); Person person01 = personMapper.queryPersonById(1001); System.out.println("第二次查詢結果為:"+person); session.close(); */ /* 清除緩存演示 */ Person personFirst = personMapper.queryPersonById(1001); System.out.println("update執行前,操作第一次查詢結果為:"+personFirst); Person personSecond = personMapper.queryPersonById(1001); System.out.println("update執行前,操作第二次查詢結果為:"+personSecond); Person personEntity = new Person(1001,"HuanCun",26); personMapper.updateStudentById(personEntity); System.out.println("執行update操作,清除一級緩存!"); session.commit(); System.out.println("update執行后commit提交成功!"); Person personEarCache = personMapper.queryPersonById(1001); System.out.println("update執行后,初次查詢結果為:"+personEarCache); Person person01 = personMapper.queryPersonById(1001); System.out.println("update執行后,第二次查詢結果為:"+person01); session.close(); }
B、驗證結果
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=13417:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit-rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit5-rt.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\junit\junit\4.12\junit-4.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;D:\ideaworkspace\ProjectStudy\mybatis-cache-03\target\classes;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;D:\download\lib\mavenTollTransfer\mic-repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;D:\download\lib\mavenTollTransfer\mic-repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\download\lib\mavenTollTransfer\mic-repository\cglib\cglib\3.3.0\cglib-3.3.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\ow2\asm\asm\7.1\asm-7.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\net\logstash\logback\logstash-logback-encoder\5.3\logstash-logback-encoder-5.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.cache.test.test,test01 [lsjSso]2021-10-20 13:14:00,298-org.apache.ibatis.logging.LogFactory-0 [main]DEBUGorg.apache.ibatis.logging.LogFactory-Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. [lsjSso]2021-10-20 13:14:00,459-org.apache.ibatis.datasource.pooled.PooledDataSource-161 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,459-org.apache.ibatis.datasource.pooled.PooledDataSource-161 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,459-org.apache.ibatis.datasource.pooled.PooledDataSource-161 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,460-org.apache.ibatis.datasource.pooled.PooledDataSource-162 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections. [lsjSso]2021-10-20 13:14:00,554-org.apache.ibatis.transaction.jdbc.JdbcTransaction-256 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Opening JDBC Connection [lsjSso]2021-10-20 13:14:00,832-org.apache.ibatis.datasource.pooled.PooledDataSource-534 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Created connection 52908367. [lsjSso]2021-10-20 13:14:00,832-org.apache.ibatis.transaction.jdbc.JdbcTransaction-534 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 13:14:00,834-com.cache.mapper.PersonMapper.queryPersonById-536 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Preparing: select id,name,age from t_person where id = ? [lsjSso]2021-10-20 13:14:00,869-com.cache.mapper.PersonMapper.queryPersonById-571 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Parameters: 1001(Integer) [lsjSso]2021-10-20 13:14:00,891-com.cache.mapper.PersonMapper.queryPersonById-593 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-<== Total: 1 update執行前,操作第一次查詢結果為:Person{id=1001, name='HuanCun', age=26} update執行前,操作第二次查詢結果為:Person{id=1001, name='HuanCun', age=26} [lsjSso]2021-10-20 13:14:00,892-com.cache.mapper.PersonMapper.updateStudentById-594 [main]DEBUGcom.cache.mapper.PersonMapper.updateStudentById-==> Preparing: UPDATE t_person SET NAME =?,age = ? where id =? [lsjSso]2021-10-20 13:14:00,892-com.cache.mapper.PersonMapper.updateStudentById-594 [main]DEBUGcom.cache.mapper.PersonMapper.updateStudentById-==> Parameters: HuanCun(String), 26(Integer), 1001(Integer) [lsjSso]2021-10-20 13:14:00,950-com.cache.mapper.PersonMapper.updateStudentById-652 [main]DEBUGcom.cache.mapper.PersonMapper.updateStudentById-<== Updates: 1 執行update操作,清除一級緩存! [lsjSso]2021-10-20 13:14:00,951-org.apache.ibatis.transaction.jdbc.JdbcTransaction-653 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] update執行后commit提交成功! [lsjSso]2021-10-20 13:14:00,953-com.cache.mapper.PersonMapper.queryPersonById-655 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Preparing: select id,name,age from t_person where id = ? [lsjSso]2021-10-20 13:14:00,953-com.cache.mapper.PersonMapper.queryPersonById-655 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-==> Parameters: 1001(Integer) [lsjSso]2021-10-20 13:14:00,954-com.cache.mapper.PersonMapper.queryPersonById-656 [main]DEBUGcom.cache.mapper.PersonMapper.queryPersonById-<== Total: 1 update執行后,初次查詢結果為:Person{id=1001, name='HuanCun', age=26} update執行后,第二次查詢結果為:Person{id=1001, name='HuanCun', age=26} [lsjSso]2021-10-20 13:14:00,954-org.apache.ibatis.transaction.jdbc.JdbcTransaction-656 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 13:14:00,955-org.apache.ibatis.transaction.jdbc.JdbcTransaction-657 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@327514f] [lsjSso]2021-10-20 13:14:00,955-org.apache.ibatis.datasource.pooled.PooledDataSource-657 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Returned connection 52908367 to pool. Process finished with exit code 0