如何正确关闭 Derby 内存数据库

发布于 2024-08-22 13:19:55 字数 1343 浏览 7 评论 0原文

我使用 derby 作为嵌入式数据库。此外,我在单元测试中使用它的内存数据库选项。

我不知道如何正确关闭 (快速查看代码) Derby 数据库。我相信我让它适用于标准数据库,但是当在内存数据库上尝试类似的代码时,我遇到了不同的异常。

我将省略细节,如果需要其他感觉我会添加它们。

基本上,我试图以这两种方式关闭我的数据库,其中我的内存数据库始终被称为“eh”:

DriverManager.getConnection("jdbc:derby:memory:eh;shutdown=true");

然后:

DriverManager.getConnection("jdbc:derby:eh;shutdown=true");

前者导致 一个例外,但不是预期的。详细信息是:

java.sql.SQLNonTransientConnectionException:数据库“内存:eh”关闭。

后者的结果是

java.sql.SQLException:找不到数据库“eh”。

根据我的了解,我们想要一个 SQLException,但不是我们收到的那个。另一方面,SQLNonTransientConnectionException 错误似乎更合适,但不是正确的类型(尽管它派生自 SQLException),也没有正确的状态代码。州代码最终为:08006

我的示例代码说明了 SQL 状态为“XJ015”的 SQLException

注意:我引用的示例是: WwdEmbedded Program ( Java 代码) 。

I'm using derby as an embedded database. Furthermore, I'm using it's in-memory database option for my unit tests.

What I can't figure out is how to properly shut down (A quick look at the code) the Derby database. I beleive I have it working for a standard database but I'm getting different exceptions when attempt similar code on a in-memory database.

I'm going to omit details, I'll add them if other feel are needed.

Basically, I'm trying to shut down my database in these two fashions where my in-memory database is consistently called "eh":

DriverManager.getConnection("jdbc:derby:memory:eh;shutdown=true");

then:

DriverManager.getConnection("jdbc:derby:eh;shutdown=true");

The former results in an exception but not the one expected. The details are:

java.sql.SQLNonTransientConnectionException: Database 'memory:eh' shutdown.

The latter results in

java.sql.SQLException: Database 'eh' not found.

Based on what I've been able to figure out, we want a SQLException but not the one we receive. On the other hand, the SQLNonTransientConnectionException error seems more appropriate but isn't the right type (though it is derived from SQLException) nor does it have the right state code. The state code end up being: 08006.

The example code I have illustrates that a SQLException with a SQL state of "XJ015".

Note: The example I'm referencing is: WwdEmbedded Program (Java Code).

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

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

发布评论

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

评论(3

风苍溪 2024-08-29 13:19:55

XJ015(SQLCODE 50000)是系统完全关闭的预期(成功)SQLSTATE。另一方面,08006(SQLCODE 45000)是仅关闭单个数据库的预期 SQLSTATE

DriverManager.getConnection("jdbc:derby:;shutdown=true");

关闭整个系统并应导致 XJ015

XJ015 (with SQLCODE 50000) is the expected (successful) SQLSTATE for complete system shutdown. 08006 (with SQLCODE 45000), on the other hand, is the expected SQLSTATE for shutdown of only an individual database.

DriverManager.getConnection("jdbc:derby:;shutdown=true");

Shuts down the entire system and should result in XJ015.

亚希 2024-08-29 13:19:55

URL“jdbc:derby:memory:eh;shutdown=true”会产生预期的 08006 错误代码,但实际上并未从内存中删除数据库。如果稍后您尝试使用“jdbc:derby:memory:eh;create=true”创建新数据库,您将收到一条错误消息,指出该数据库已存在。

幸运的是,从 Derby 10.6.1.0(2010 年 5 月 17 日发布)开始,可以使用“jdbc:derby:memory:eh;drop=true”形式的 URL 实际上删除内存数据库。请参阅 发行说明和页面使用内存数据库。

The URL "jdbc:derby:memory:eh;shutdown=true" results in the expected 08006 error code, but doesn't actually remove the DB from memory. If later on, you try to create a new database with "jdbc:derby:memory:eh;create=true", you'll get an error saying that the database already exists.

Fortunately, as of Derby 10.6.1.0 (released on May 17, 2010), it is possible to actually drop an in-memory database using a URL of the form "jdbc:derby:memory:eh;drop=true". See the release notes and the page Using in-memory databases.

︶ ̄淡然 2024-08-29 13:19:55

我相信你的第一个代码示例很好。我相信,您所看到的 SQL 状态差异是因为您运行的是嵌入式 Derby,但您看到的示例代码(具有 SQL 状态 XJ015)是在客户端-服务器配置中运行的。

正如您所指出的, SQLNonTransientConnectionException 是 SQLException 的子类,因此我很困惑为什么您认为没有获得正确类型的异常。

I believe that your first code example is fine. The SQL State difference that you are seeing, I believe, is because you are running Derby embedded, but the example code that you saw (with SQL state XJ015) was running in a client-server configuration.

As you noted, the SQLNonTransientConnectionException is a subclass of SQLException, so I am confused as to why you think you are not getting the right type of exception.

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