如何在 Felix 中使用 Derby 客户端?

发布于 2024-10-31 19:41:45 字数 495 浏览 3 评论 0原文

我想从 OSGi 包中运行 Derby Client。该包由 Maven 构建,因此我添加了对 org.apache.derby:derbyclient 的依赖项。在运行时,我收到以下异常:java.sql.SQLException:没有找到适合 jdbc:derby://localhost:1527/testdb 的合适驱动程序

有趣的是,当我使用嵌入式驱动程序和对 org.apache.derby.derby 的依赖项时,整个事情都有效。我只是看不出这两者之间的区别。

我做错了什么以及如何修复它?

一些花絮:

  1. 在我在互联网上找到一些建议后,我设置了以下 OSGi 标头:DynamicImport-Package: *。这解决了嵌入式驱动程序的问题,但客户端仍然失败。
  2. 我使用的 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:

  1. 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.
  2. The version of Derby I use is 10.7.1.1 which should be OSGi enabled (at least it has OSGi headers).

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

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

发布评论

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

评论(2

柳若烟 2024-11-07 19:41:45

在 OSGi 中,建议不要使用 DrivverManager 来获取连接。更好的方法是使用数据源。

因此,对于 derby 客户端,您可以使用这个:

ClientDataSource ds = new ClientDataSource();
... // set properties here
Connection connection = dataSource.getConnection();

由于 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:

ClientDataSource ds = new ClientDataSource();
... // set properties here
Connection connection = dataSource.getConnection();

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.

∞觅青森が 2024-11-07 19:41:45

好吧,虽然距离我提出问题还不到半个小时,但我找到了解决方案。我不知道它有多干净,但它似乎完成了工作:

ClassLoader ctxtCl = Thread.currentThread().getContextClassLoader();
try {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    dbConnection = DriverManager.getConnection("jdbc:derby://localhost:1527/testdb");
} catch (SQLException e) {
    /* log, etc. */
} finally {
    Thread.currentThread().setContextClassLoader(ctxtCl);
}

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:

ClassLoader ctxtCl = Thread.currentThread().getContextClassLoader();
try {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    dbConnection = DriverManager.getConnection("jdbc:derby://localhost:1527/testdb");
} catch (SQLException e) {
    /* log, etc. */
} finally {
    Thread.currentThread().setContextClassLoader(ctxtCl);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文