Problem deserializing objects from cache on MyBatis 3/Java

人盡茶涼 提交于 2019-12-10 23:37:42

问题


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.

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;

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.


回答1:


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



来源:https://stackoverflow.com/questions/4893668/problem-deserializing-objects-from-cache-on-mybatis-3-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!