1 問題描述
mybatis3.2.x版本,作壓力測試,併發200用戶,出現了以下異常.java
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"]
spring
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:47)
express
at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:29)
apache
at OgnlConcurrentTest$1.run(OgnlConcurrentTest.java:51)
mybatis
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)
ui
at org.apache.ibatis.ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:61)
lua
at org.apache.ibatis.ognl.OgnlRuntime.callMethod(OgnlRuntime.java:860)
.net
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,故須要升級。
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;
}
參考資料