org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object

1 问题描述 
mybatis3.2.x版本,做压力测试,并发200用户,出现了如下异常.

org.apache.ibatis.builder.BuilderException: Error evaluating expression 'size() > 0'. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [one, two] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public"]  
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:47)  
    at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:29)  
    at OgnlConcurrentTest$1.run(OgnlConcurrentTest.java:51)  
    at java.lang.Thread.run(Thread.java:745)  
Caused by: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [one, two] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public"]  
    at org.apache.ibatis.ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:837)  
    at org.apache.ibatis.ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:61)  
    at org.apache.ibatis.ognl.OgnlRuntime.callMethod(OgnlRuntime.java:860)  
    at org.apache.ibatis.ognl.ASTMethod.getValueBody(ASTMethod.java:73)  
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)  
2 解决方案  

在网上搜了一桶,找到原因是mybatis3.2.x版本引用ognl的版本为2.6.9,出问题的地方代码如下。Modifier.isPublic存在问题,好在ognl2.7版本已经修复,故将mybatis版本升级到mybatis3.3.x以上,同时升级mybatis-spring到1.3.x以上即可。 
因为mybatis-spring1.2.x引用的mybatis版本还是mybatis3.2.x,故需要升级。 
ognl

public static Object invokeMethod(Object target, Method method, Object[] argsArray)
    throws InvocationTargetException, IllegalAccessException
  {
    boolean wasAccessible = true;

    if (securityManager != null) {
      try {
        securityManager.checkPermission(getPermission(method));
      } catch (SecurityException ex) {
        throw new IllegalAccessException("Method [" + method + "] cannot be accessed.");
      }
    }
    if (((!Modifier.isPublic(method.getModifiers())) || (!Modifier.isPublic(method.getDeclaringClass().getModifiers()))) && 
      (!(wasAccessible = method.isAccessible()))) {
      method.setAccessible(true);
    }

    Object result = method.invoke(target, argsArray);
    if (!wasAccessible) {
      method.setAccessible(false);
    }
    return result;
  }
参考资料 

MyBatis 3.2.x版本在并发情况下可能出现的bug及解决办法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值