Problem deserializing objects from cache on MyBatis 3/Java
-
28-10-2019 - |
Question
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.
Solution
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....