使用mybatis时 有时会出现查询异常,使用debug调试不会出现
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Error preparing statement. Cause: java.lang.ArrayIndexOutOfBoundsException: -1
### The error may exist in com/yanxuemao/dao/IStudentDao.xml
### The error may involve com.yanxuemao.dao.IStudentDao.findAll
### The error occurred while executing a query
### SQL: select * from student;
### Cause: org.apache.ibatis.executor.ExecutorException: Error preparing statement. Cause: java.lang.ArrayIndexOutOfBoundsException: -1
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy22.findAll(Unknown Source)
at com.yanxuemao.service.impl.StudentServiceImpl.findAll(StudentServiceImpl.java:21)
at com.yanxuemao.web.servlet.student.StudentTotalListServlet.doPost(StudentTotalListServlet.java:25)
设置了事务隔离级别 并且关闭了一二级缓存任然会出现
是因为Mybatis的sqlsession线程不安全 都是用的同一个DefaultSqlSession
使用sqlsessionManager或者sqlsessionTemplate
参考:使用safeSqlSession
package com.util.utils;
import com.util.service.AttributeService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSessionManager;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class GetSqlSession {
private static SqlSession sqlSession;
private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder;
private static SqlSessionFactory sqlSessionFactory;
private static SqlSessionManager sqlSessionManager;
public static SqlSession getSqlSession(){
InputStream resourceAsStream = null;
try {
URL path_1 = AttributeService.class.getResource("/");
String xmlPath = path_1 + "SqlMapConfig.xml";
xmlPath = xmlPath.substring(6);
resourceAsStream = new FileInputStream(xmlPath);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
return sqlSession;
}
public static SqlSessionManager safeSqlSession() {
if (sqlSessionManager == null) {
FileInputStream resourceAsStream = null;
try {
URL path_1 = AttributeService.class.getResource("/");
String xmlPath = path_1 + "SqlMapConfig.xml";
xmlPath = xmlPath.substring(6);
resourceAsStream = new FileInputStream(xmlPath);
} catch (IOException var3) {
var3.printStackTrace();
}
sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
sqlSessionManager = SqlSessionManager.newInstance(sqlSessionFactory);
return sqlSessionManager;
} else {
return sqlSessionManager;
}
}
public static void cleanSqlSession(){
if (sqlSession != null){
sqlSession.clearCache();
}
}
}