本文介绍了在MyBatis 3/Java上从缓存反序列化对象时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用MySQL/MyBatis3/Tomcat进行辅助项目.我目前正在努力打开MyBatis中的缓存.当我第一次尝试打开缓存时,由于我的对象未实现Serializable的事实,我遇到了异常.因此,在使用对象实现Serializable之后,我尝试进行缓存;它似乎可以很好地缓存.

So I'm working on a side project using MySQL/MyBatis3/Tomcat. I'm currently working on turning on caching in MyBatis. When I first tried to turn on caching, I got exceptions due to the fact that my object didn't implement Serializable. So, after implementing Serializable with the object I was trying to cache; it appeared to cache fine.

但是;当我再次遇到同样情况的servlet,并且对象映射器尝试从缓存反序列化我的对象时,我得到以下堆栈跟踪:

But; when I hit my servlet a second time with the same situation, and the object mapper attempts to deserialize my object from the cache, I get the following stack trace:

### Error querying database.  Cause: org.apache.ibatis.cache.CacheException: Error deserializing object.  Cause: java.lang.ClassNotFoundException: my.package.MyClass
### Cause: org.apache.ibatis.cache.CacheException: Error deserializing object.  Cause: java.lang.ClassNotFoundException: my.package.MyClass
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:77)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:69)
at org.apache.ibatis.binding.MapperMethod.executeForList(MapperMethod.java:85)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:65)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38)
at $Proxy5.selectAllArgs(Unknown Source)

我不明白的另一件事是:

The other thing I don't understand is this:

Serializable result;
try {
  ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) value);
  ObjectInputStream ois = new ObjectInputStream(bis);

  // LINE THROWING EXCEPTION IN org.apache.ibatis.cache.decorators.SerializedCache
  result = (Serializable) ois.readObject();
  // -- -----------------------------------

  ois.close();
} catch (Exception e) {
  throw new CacheException("Error deserializing object.  Cause: " + e, e);
}
return result;

为什么还要尝试加载该类呢?它只需要强制转换为Serializable.值得注意的是,当我没有打开缓存时;一切都按预期进行.

Why is it even trying to load the class to begin with? It just needs to cast to Serializable. Its worth noting that when I don't have caching turned on; everything works as expected.

推荐答案

基本上,发生这种情况的原因是因为我在Eclipse中的项目设置不正确,导致服务器启动时某些类不可用.基本上,我所做的只是将构建输出目录从"workspace/project/build"更改为"workspace/project/WebContent/WEB-INF/build".我现在将阅读有关适当的servlet部署的更多信息....

Basically, reason this happened was because I had my project in Eclipse set up incorrectly and this resulted in some classes not being available when my server started. Basically, all I did was change my build output directory from "workspace/project/build" to "workspace/project/WebContent/WEB-INF/build". I'm going to read more about proper servlet deployment now....

这篇关于在MyBatis 3/Java上从缓存反序列化对象时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:25