在 MyBatis 3/Java 上从缓存反序列化对象时出现问题

发布于 2024-10-16 02:53:14 字数 1649 浏览 6 评论 0原文

所以我正在使用 MySQL/MyBatis3/Tomcat 进行一个业余项目。我目前正在致力于在 MyBatis 中打开缓存。当我第一次尝试打开缓存时,由于我的对象没有实现可序列化,所以出现了异常。因此,在使用我尝试缓存的对象实现 Serialized 之后;它看起来缓存得很好。

但;当我在同样的情况下第二次访问我的 servlet 时,对象映射器尝试从缓存中反序列化我的对象,我得到以下堆栈跟踪:

### 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)

我不明白的另一件事是:

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;

为什么它甚至试图首先加载类?它只需要转换为可序列化即可。值得注意的是,当我没有打开缓存时;一切都按预期进行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-10-23 02:53:14

基本上,发生这种情况的原因是因为我在 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....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文