OSGi + 休眠

发布于 2024-07-15 03:39:11 字数 136 浏览 9 评论 0原文

数据库操作不是分散在四个 (osgi) 包中,而是全部执行略有不同的操作。 我想创建一个(简单的)OSGi 包来负责所有持久性问题。 我觉得这并不像听起来那么简单,因为“每个包都有唯一的类加载器”。 所以我真正感激的是如果有人知道此类问题的解决方案。

Instead of having database actions scattered in four (osgi) bundles, all doing there slightly different things. I want to create a (simple) OSGi bundle that is responsible for all persistance issues. I feel that this is not as simple as it sounds because of the "unique classloader per bundle" thing.
So what I really would appreciate is if someone knows the solution(s) to this kind of problem.

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

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

发布评论

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

评论(5

小伙你站住 2024-07-22 03:39:11

我可以想到两种方法来解决类加载问题。

  • 您是否依赖于特定的 OSGi 框架,或者您希望尽可能保持兼容? 或者您可以使用春分来实现吗? 在 Equinox 中,有一种称为 Buddy Classloading 的机制。 此添加允许您提高不同 OSGi-Bundles 之间某些类的可见性。 如果您对此主题感兴趣,我想引导您阅读这两篇文章: 了解 Eclipse 插件如何与 OSGi 配合使用Eclipse -两个虚拟机(和许多类加载器)的故事
  • 如果您希望保持 OSGi 实现独立,那么您可能会考虑将您希望保留的类提取到一个单独的包中,Hibernate-Bundle 和其他包都依赖于该包。 这样,他们都可以访问持久化类的类定义。

There are two ways I can think of to solve the classloading-issue.

  • Are you tied to a specific OSGi-Framework, or do you want to stay as compatible as possible? Or can you use equinox for your implementation? In Equinox you have a mechanism called Buddy Classloading. This addition allows you to increase the visibility of certain classes between different OSGi-Bundles. If you are interested in this topic, I would like to direct you to these two articles: Understanding how Eclipse plug-ins work with OSGi, Eclipse - a tale of two VMs (and many classloaders).
  • If you prefere to stay OSGi-implementation independent, then you might considre extracting your classes, that you wish to persist, into a separate bundle on which both the Hibernate-Bundle and your other bundles depend. That way, they all have access to the class definition of the persisted classes.
葬﹪忆之殇 2024-07-22 03:39:11

(如果您使用 Hibernate Annotations)

当 Hibernate 捆绑包获悉带注释的类时,保存所有实体类加载器。

然后在构建 SessionFactory 之前执行类似的操作。

ClassLoad cl = Thread.currentThread().getContextClassLoader();
try {
 Thread.currentThread().setContextClassLoader(yourClassLoader);
 factory = cfg.buildSessionFactory(); 
}finally {
 Thread.currentThread().setContextClassLoader(cl);  // restore the original class loader
}

(If you are using Hibernate Annotations)

Save all the Entities class loaders when the Hibernate bundle is informed about an annotated class.

Then do something like this before building your SessionFactory.

ClassLoad cl = Thread.currentThread().getContextClassLoader();
try {
 Thread.currentThread().setContextClassLoader(yourClassLoader);
 factory = cfg.buildSessionFactory(); 
}finally {
 Thread.currentThread().setContextClassLoader(cl);  // restore the original class loader
}
鸵鸟症 2024-07-22 03:39:11

刚刚在 Bundle 类/api 中发现了一个有趣的方法。

public java.lang.Class loadClass(java.lang.String name) throws java.lang.ClassNotFoundException

这必须解决一些类加载器问题吗?

Just found an interesting method in the Bundle class/api.

public java.lang.Class loadClass(java.lang.String name) throws java.lang.ClassNotFoundException

This must solve some class loader issues?

原谅过去的我 2024-07-22 03:39:11

我建议远离伙伴类加载,因为它特定于 Eclipse 的 Equinox 实现,在我看来,人们让它工作,但他们不明白为什么,每个人最终都会成为其他人的伙伴。 这会阻止您正确理解 OSGi 类加载的工作原理以及使用它所需使用的模式(复合类加载器、上下文类加载、OSGi 服务……)。

如果您的持久性捆绑包提前知道需要持久保存什么类型,那么该捆绑包可以导入包含您的域类的所有必需包(Require-Bundle 是邪恶的)。

管理上下文类加载器(如 Roger 的回复)可以帮助 Hibernate,尽管我建议使用 Spring dm 之类的东西将其隐藏在 OSGi 服务后面。

I'm going to recommend to stay away from buddy class loading as it's specific to Eclipse's Equinox implementation and, in my opinion, people get it to work but they don't understand why and everyone ends up as a buddy of everyone else. This stops you gaining a proper understanding of how OSGi classloading works and the patterns you need to use (composite class loaders, context class loading, OSGi services, ...) to work with it.

If your persistence bundle knows ahead-of-time what types it needs to persist, then the bundle can import all required packages (Require-Bundle is evil) that contain your domain classes.

Managing the context class loader (as in Roger's reply) can help with Hibernate, though I'd suggest using something like Spring dm to hide that behind an OSGi service.

木緿 2024-07-22 03:39:11

Hibernate 确实支持 OSGi 但它是一个 持续努力

Hibernate does support OSGi but it's an ongoing effort.

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