ssm集成mybatis提交commit异常问题

来源:互联网 发布:java cs的前景 编辑:程序博客网 时间:2024/06/07 03:27

ssm集成mybatis提交commit异常问题

Manual commit is not allowed over a Spring managed SqlSession Manual close is not allowed over a Spring managed SqlSession 

出现这种异常是因为spring在管理sqlsession对象的时候使用了代理类,通过这个代理类来实现对所有的事物实现提交以及关闭

跟踪源码我们可以发现

    //这是我调用的方法,现在进入session的insert的方法内    public boolean insert(Users users) {        SqlSession session = super.getSqlSession();        System.out.println(session);        int insert = session.insert("com.cc.dao.UserDao.insert", users);        return insert>0?true:false;    }//发现原来是用代理类实现的,接着跟踪  @Override  public int insert(String statement, Object parameter) {    return this.sqlSessionProxy.insert(statement, parameter);  }    //这是这个代理类的创建方式,于是,找到了SqlSessionInterceptor这个类  this.sqlSessionProxy = (SqlSession) newProxyInstance(        SqlSessionFactory.class.getClassLoader(),        new Class[] { SqlSession.class },        new SqlSessionInterceptor());                //这就是这个代理类的invoke方法,可以看到,他确实是帮我们实现了commit方法以及close方法    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {      SqlSession sqlSession = getSqlSession(          SqlSessionTemplate.this.sqlSessionFactory,          SqlSessionTemplate.this.executorType,          SqlSessionTemplate.this.exceptionTranslator);      try {        Object result = method.invoke(sqlSession, args);        if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {          // force commit even on non-dirty sessions because some databases require          // a commit/rollback before calling close()          sqlSession.commit(true);        }        return result;      } catch (Throwable t) {        Throwable unwrapped = unwrapThrowable(t);        if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {          // release the connection to avoid a deadlock if the translator is no loaded. See issue #22          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);          sqlSession = null;          Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);          if (translated != null) {            unwrapped = translated;          }        }        throw unwrapped;      } finally {        if (sqlSession != null) {          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);        }      }    }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 照相机不能拍照了怎么办 红色衣服不红怎么办 拍摄画面过暗怎么办 手机照相机镜头脏了怎么办 单反相机对不了焦怎么办 单反相机聚不了焦怎么办 单反相机对不上焦怎么办 手机拍不了照怎么办 摄影师老了以后怎么办 淘宝卖窗帘配件怎么办 唯品会包裹丢了怎么办 左右脸咬肌不一样大怎么办 脸两边不一样大怎么办 淘宝买的出问题怎么办 淘宝店铺忘记了怎么办 买宠物被骗了怎么办 养羊刚进的羊苗怎么办 孕后期便秘出血怎么办 针织裙子变形了怎么办 背带牛仔裙太短怎么办 牛仔裙太短了怎么办 牛仔裤裤腿瘦了怎么办 牛仔裤买肥了怎么办 牛仔短裤裤腰肥怎么办 白色洞洞鞋脏了怎么办 菜刀快递不让寄怎么办 淘宝退款后悔了怎么办 淘宝退货还收货怎么办 淘宝售后没人理怎么办 淘宝店铺不理人怎么办 客服半天不理人怎么办 淘宝优惠券删了怎么办 ip地址受限制怎么办 订单地址错了怎么办 闲鱼恶意退货怎么办 闲鱼上遇到恶意退货怎么办 快递没修改地址怎么办 买家退货寄错怎么办 收件人想改地址怎么办 淘宝不给你验收怎么办 王者订单不验收怎么办