sqlSession 数据库会话链接问题。

执行单元测试:

创建一个链接,无法关闭掉,while循环中,该进程一直在,虽然上面的方法关闭了,但是没有立即关闭。

遗留问题:

1.后面整合SpringMVC层,再测试,是不是http请求结束时,会话在此时关闭???

2.之前自己做的性能测试,多线程制作模拟数据,2小时1亿,写死数据库

3.jvm内存监控,内存泄露问题。

@Test
    public void test01() throws IOException {
        SqlSessionFactory sqlSessionFactory= getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
        try {
                Employee employee=mapper.getEmpById(1);
                System.out.println(employee);
        }finally {
            openSession.close();
        }
        while(true) {
            
        }
    
    }

navicat 观察,有个数据库链接。query:  show variables like 'max_connections'; 查看最大连接数。

 

 

创建10个新的链接,引用都是新的,可以看到10个链接。

@Test
    public void test01() throws IOException {
        SqlSessionFactory sqlSessionFactory= getSqlSessionFactory();
        SqlSession openSession=null;
        try {
            for(int i=0;i<10;i++) {
                //循环10次,创建10个链接
                 openSession = sqlSessionFactory.openSession();
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                //执行查询语句,真正的去链接数据库
                Employee employee=mapper.getEmpById(1);
            }
        }finally {
            openSession.close();
        }
        while(true) {
            
        }
        //System.out.println(employee);
    }

 

posted on 2017-11-05 00:49  老曹123  阅读(284)  评论(0)    收藏  举报

导航