1. 程式人生 > >org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object

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

開發十年,就只剩下這套架構體系了! >>>   

1 問題描述 
mybatis3.2.x版本,做壓力測試,併發200使用者,出現瞭如下異常.

 

 
  1. 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"]

  2. at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:47)

  3. at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:29)

  4. at OgnlConcurrentTest$1.run(OgnlConcurrentTest.java:51)

  5. at java.lang.Thread.run(Thread.java:745)

  6. 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"]

  7. at org.apache.ibatis.ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:837)

  8. at org.apache.ibatis.ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:61)

  9. at org.apache.ibatis.ognl.OgnlRuntime.callMethod(OgnlRuntime.java:860)

  10. at org.apache.ibatis.ognl.ASTMethod.getValueBody(ASTMethod.java:73)

  11. 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

 

 
  1. public static Object invokeMethod(Object target, Method method, Object[] argsArray)

  2. throws InvocationTargetException, IllegalAccessException

  3. {

  4. boolean wasAccessible = true;

  5.  
  6. if (securityManager != null) {

  7. try {

  8. securityManager.checkPermission(getPermission(method));

  9. } catch (SecurityException ex) {

  10. throw new IllegalAccessException("Method [" + method + "] cannot be accessed.");

  11. }

  12. }

  13. if (((!Modifier.isPublic(method.getModifiers())) || (!Modifier.isPublic(method.getDeclaringClass().getModifiers()))) &&

  14. (!(wasAccessible = method.isAccessible()))) {

  15. method.setAccessible(true);

  16. }

  17.  
  18. Object result = method.invoke(target, argsArray);

  19. if (!wasAccessible) {

  20. method.setAccessible(false);

  21. }

  22. return result;

  23. }

參考資料 

MyBatis 3.2.x版本在併發情況下可能出現的b