在第一次创建mybits项目时,做一个数据的提交如图所示
public static void main(String[] args) throws IOException {
userEntity users= new userEntity();
userService service = new userService();
users.setUsername("赵六");
users.setJobs("测试");
users.setPhone("0000000");
service.insertUser(users);
}
提交到数据库之后会出现下面的情况‘
一次提交会增加多组数据,导致主键id出现不连续的情况
排查了很久,发现是在service层时 插入方法的sqlsession资源未关闭
修改之后的代码如图所示
public int insertUser(userEntity userEntity) {
int result = userMapper.intsertUser(userEntity);
sqlSession.commit();// 提交事务
sqlSession.close();//关闭资源
return result;
}
我想大概是因为没有及时关闭sqlSession资源,导致在一次链接时出现多次提交的情况(仅是个人猜测,有没有懂的大佬能解释一下原理吗?)