如何在 Felix 中使用 Derby 客户端?
我想从 OSGi 包中运行 Derby Client。该包由 Maven 构建,因此我添加了对 org.apache.derby:derbyclient 的依赖项。在运行时,我收到以下异常:java.sql.SQLException:没有找到适合 jdbc:derby://localhost:1527/testdb 的合适驱动程序
。
有趣的是,当我使用嵌入式驱动程序和对 org.apache.derby.derby 的依赖项时,整个事情都有效。我只是看不出这两者之间的区别。
我做错了什么以及如何修复它?
一些花絮:
- 在我在互联网上找到一些建议后,我设置了以下 OSGi 标头:
DynamicImport-Package: *
。这解决了嵌入式驱动程序的问题,但客户端仍然失败。 - 我使用的 Derby 版本是 10.7.1.1,它应该启用 OSGi(至少它有 OSGi 标头)。
I want to run Derby Client from within an OSGi bundle. The bundle gets built by Maven so I added a dependency to org.apache.derby:derbyclient
. At runtime I get the following exception: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/testdb
.
Interestingly the whole thing works when I use the embedded driver and a dependency to org.apache.derby.derby
. I just don't see the difference between those two.
What am I doing wrong and how can I fix it?
Some tidbits:
- After some advice I found on the Internet I set the following OSGi header:
DynamicImport-Package: *
. This fixed problems with the embedded driver but the client still fails. - The version of Derby I use is 10.7.1.1 which should be OSGi enabled (at least it has OSGi headers).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 OSGi 中,建议不要使用 DrivverManager 来获取连接。更好的方法是使用数据源。
因此,对于 derby 客户端,您可以使用这个:
由于 DataSource 方法不会干扰类加载器,因此它在 OSGi 中更加可靠。
此外,将数据源与客户端代码分离并将其绑定为 OSGi 服务也是一个很好的做法。这允许在代码中保留对数据库实现的依赖。
最简单的方法是使用 pax-jdbc-config并让它从配置中为您创建数据源。在您自己的代码中,您只需将数据源绑定为服务就可以了。
pax-jdbc 的当前发行版本尚不支持 derbyclient,但我刚刚将其添加到 master 中。所以下一个版本应该包含它。
In OSGi it is recommended to not use the DrivverManager to get a connection. The better way is to use a DataSource.
So for derby client you could use this:
As the DataSource approach does not fiddle with the classloader it is much more reliable in OSGi.
Additionally it is a good practice to separate the DataSource from your client code and bind it as an OSGi service. This allows to keep the dependency to the database impl out of your code.
The easiest approach is to use pax-jdbc-config and let it create the DataSource for you from a configuration. In your own code you then just bind the DataSource as a service and are fine.
The current release version of pax-jdbc does not yet support the derbyclient but I just added this to the master. So the next release should contain it.
好吧,虽然距离我提出问题还不到半个小时,但我找到了解决方案。我不知道它有多干净,但它似乎完成了工作:
Okay, although not even half an hour passed since I asked the question I found a solution. I don't know how clean it is but it seems to get the job done: