本文介绍了为什么MyBatis有时有时找不到映射器,但在大多数情况下会找到呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tomcat Web应用程序中间歇性地收到MyBatis错误:

I'm getting a MyBatis error intermittently in a Tomcat web application:

org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.cisco.salesconnect.screport.mapper.hubportfolio.Authorization.getAuthorizedHubSupervisorIds
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.cisco.salesconnect.screport.mapper.hubportfolio.Authorization.getAuthorizedHubSupervisorIds
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.5.2.jar:3.5.2]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149) ~[mybatis-3.5.2.jar:3.5.2]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.2.jar:3.5.2]
    at com.cisco.salesconnect.screport.dao.SCHubAuthorizationServiceHandler.getAuthorizedHubSupervisorIds(SCHubAuthorizationServiceHandler.java:168) ~[classes/:?]
    at com.cisco.salesconnect.screport.dao.SCHubAuthorizationServiceHandler$7.run(SCHubAuthorizationServiceHandler.java:404) [classes/:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_181]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_181]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_181]
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.cisco.salesconnect.screport.mapper.hubportfolio.Authorization.getAuthorizedHubSupervisorIds
    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:964) ~[mybatis-3.5.2.jar:3.5.2]
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:755) ~[mybatis-3.5.2.jar:3.5.2]
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:748) ~[mybatis-3.5.2.jar:3.5.2]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:146) ~[mybatis-3.5.2.jar:3.5.2]
    ... 6 more

我可以在日志中得到一次错误,然后立即转到浏览器并单击编辑"->重新发送",它将可以正常工作.通过在Postman中设置一个Web服务运行程序并将其设置为10次迭代,我能够重现此问题.

I could get the error once in the log, then immediately go to the browser and click edit->resend and it would work fine. I was able to recreate this issue by making setting up a web service runner in Postman and setting it to 10 iterations.

我正在使用Java对象而不是XML文件进行配置.我曾经在多线程Web请求中共享一个SqlSession对象,但是由于读取它不是线程安全的,因此不再是.我还尝试在多个请求之间共享Configuration对象,以使它不必花费每个请求查找XML映射器文件的开销.

I'm getting the configuration using Java objects and not an XML file. I used to be sharing a SqlSession object in a multithreaded web request, but no longer am since reading it's not thread safe. I was also trying to share the Configuration object amongst multiple requests so that it doesn't have to go through the expense of finding the XML mapper files with each request.

我还应该寻找什么?出于IP原因,我无法发布原始源代码,但是一旦我知道人们在寻找什么,便可以发布类似的内容.

What other things should I look for? I can't post the raw source code for IP reasons, but can post something like it once I know what people are looking for.

谢谢the夫.

推荐答案

Configuration对象不是线程安全的.

Configuration object is not thread-safe.

您面临的问题可能是由于对配置它的线程中的Configuration对象所做的更改在其他线程中不可见的,因为这样的更改不是安全发布.

The problem you are facing may be caused by the fact that changes done to Configuration object in the thread that configured it are not visible in other threads because such changes are not safely published.

因此,您需要使用每个线程的配置,或者确保安全发布配置对象.

So you either need to use configuration per-thread or do make sure that you safely publish you configuration objects.

这篇关于为什么MyBatis有时有时找不到映射器,但在大多数情况下会找到呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:28