Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the function of the Size () method in Mybatis

Shulou Source: shulou.com Published: 2022-05-31 19:36:37 09月29日 Update

This article will explain in detail what is the role of Size () method in Mybatis. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Preface

MyBatis is an open source lightweight semi-automated ORM framework for object-oriented and relational database mapping, in which xml files, combined with sql statements, is the biggest feature of application sql decoupling. OGNL expression, which is widely used in MyBatis, is an EL language, which is used to set and get the properties of Java object, and can project the list and execute lambda expression. Ognl provides a simple and easy to execute ognl expression. An exception often occurs in an online service, which will be reproduced if various OGNL expressions are empty. The specific stack information is as follows:

# Error querying database. Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'list! = null and list.size () > zero. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [1] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$SingletonList with modifiers "public"] # Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'list! = null and list.size () > zero. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [1] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$SingletonList with modifiers "public"] at org.apache.ibatis.exceptions.ExceptionFactory.wrapException (ExceptionFactory.java:23) org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (DefaultSqlSession.java:107) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (DefaultSqlSession.java: 98) at cn.com.shaobingmm.MybatisBugTest$2.run (MybatisBugTest.java:88) at java.lang.Thread.run (Thread.java:745) Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'list! = null and list.size () > zero. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [1] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$SingletonList with modifiers "public"] at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue (OgnlCache.java at:47) at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean (ExpressionEvaluator.java:29) at org.apache.ibatis.scripting.xmltags. IfSqlNode.apply (IfSqlNode.java:30) at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply (MixedSqlNode.java:29) at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply (TrimSqlNode.java:51) at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply (MixedSqlNode.java:29) at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql (DynamicSqlSource.java:37) at org.apache.ibatis.mapping.MappedStatement.getBoundSql (MappedStatement.java At org.apache.ibatis.executor.CachingExecutor.query (CachingExecutor.java:79) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList (DefaultSqlSession.java:104)... 3 more Caused by: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [1] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$SingletonList with modifiers "public"] at org.apache .ibatis.ognl.OgnlRuntime.callAppliateMethod (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) at org.apache.ibatis.ognl.SimpleNode.getValue (SimpleNode.java:210) At org.apache.ibatis.ognl.ASTChain.getValueBody (ASTChain.java:109) at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody (SimpleNode.java:170) at org.apache.ibatis.ognl.SimpleNode.getValue (SimpleNode.java:210) at org.apache.ibatis.ognl.ASTGreater.getValueBody (ASTGreater.java:49) at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody (SimpleNode.java:170) at org.apache.ibatis.ognl.SimpleNode.getValue ( SimpleNode.java:210) at org.apache.ibatis.ognl.ASTAnd.getValueBody (ASTAnd.java:56) at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody (SimpleNode.java:170) at org.apache.ibatis.ognl.SimpleNode.getValue (SimpleNode.java:210) at org.apache.ibatis.ognl.Ognl.getValue (Ognl.java:333) at org.apache.ibatis.ognl.Ognl.getValue (Ognl.java:413) at org.apache.ibatis. Ognl.Ognl.getValue (Ognl.java:395) at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue (OgnlCache.java:45)... 12 more

The size method of List clearly has public and is not accessible. The exception has not been reproduced in the test environment, but the number of errors in the complete call link of the interface accounts for 0.01% of the total calls. This is a probabilistic event.

Simulation test

Write test code that simulates multithreading to read the company list concurrently.

Select * from company and id in # {id}

Stress testing under multithreading

String resource = "mybatis-config.xml"; InputStream in = null; try {in = Resources.getResourceAsStream (resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (in); final List ids = Collections.singletonList (1L); final SqlSession session = sqlSessionFactory.openSession (); final CountDownLatch mCountDownLatch = new CountDownLatch (1); for (int I = 0; I < 50) Thread thread +) {Thread thread = new Thread (new Runnable () {public void run () {try {mCountDownLatch.await ();} catch (InterruptedException e) {e.printStackTrace ()) } for (int k = 0; k < 100; KBG +) {session.selectList ("CompanyMapper.getCompanysByIds", ids);}); thread.start () } mCountDownLatch.countDown (); synchronized (MybatisBugTest.class) {try {MybatisBugTest.class.wait ();} catch (InterruptedException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();} catch (Throwable e) {e.printStackTrace ();} finally {if (in! = null) try {in.close () } catch (IOException e) {e.printStackTrace ();}}

The above code will have an exception when it is concurrently.

Caused by: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [1] [java.lang.IllegalAccessException: Class org.apache.ibatis.ognl.OgnlRuntime can not access a member of class java.util.Collections$SingletonList with modifiers "public"] at org.apache.ibatis.ognl.OgnlRuntime.callAppropriateMethod (OgnlRuntime.java:837)

The exception message indicates that the ognlRuntime class cannot access

Check the source code and solve the case.

SingletonList, a private member of java.util.Collections. Looking at the source code, you can see that it is locked on the invokeMethod method.

Public static Object callAppropriateMethod (OgnlContext context, Object source, Object target, String methodName, String propertyName, List methods, Object [] args) throws MethodFailedException {Object reason = null; Object [] actualArgs = objectArrayPool.create (args.length); try {Method e = getAppropriateMethod (context, source, target, methodName, propertyName, methods, args, actualArgs) If (e = = null | |! isMethodAccessible (context, source, e, propertyName)) {StringBuffer buffer = new StringBuffer (); if (args! = null) {int I = 0; for (int ilast = args.length-1; I

Tags: Methods code threads expressions tests functions information content objects articles more times knowledge articles questions applications good one line events preface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei macOS Shulou Information Shulou Tech Info