- 我设定二个线程使用同一个sqlsession去查同一条信息,会发现他只会在数据库中查找一次, 也就是说第二次查找是在一级缓存中查找的
@org.junit.Test
public void selectById2() throws InterruptedException {
SqlSession sqlSession = DBUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
System.out.println(mapper.selectUserById(99));
CountDownLatch countDownLatch = new CountDownLatch(1);
Thread thread = new Thread(new Service(sqlSession, countDownLatch));
thread.start();
countDownLatch.await();
System.out.println(“关闭sqlSession”);
sqlSession.close();
}
public class Service implements Runnable {
private SqlSession sqlSession;
private CountDownLatch countDownLatch;
public Service(SqlSession sqlSession, CountDownLatch countDownLatch) {
this.sqlSession = sqlSession;
this.countDownLatch = countDownLatch;
}
@Overr