mybatis不时出现NegativeArrayException、ArrayIndexOutOfBoundsException或者cannot be cast to java.util.List异常

使用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();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值