Java 调试时在内存中检查 hsqldb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2697341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inspect in memory hsqldb while debugging
提问by Albert
We're using hdsqldb in memory to run junit tests which operate against a database. The db is setup before running each test via a spring configuration. All works fine. Now when a tests fails it can be convinient to be able to inspect the values in the in memory database. Is this possible? If so how? Our url is:
我们在内存中使用 hdsqldb 来运行对数据库进行操作的 junit 测试。在通过 spring 配置运行每个测试之前设置数据库。一切正常。现在,当测试失败时,可以方便地检查内存数据库中的值。这可能吗?如果是这样怎么办?我们的网址是:
jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true
jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true
The database is destroyed after each tests. But when the debugger is running the database should also still be alive. I've tried connecting with the sqldb databaseManager. That works, but I don't see any tables or data. Any help is highly appreciated!
每次测试后都会销毁数据库。但是当调试器运行时,数据库也应该还活着。我试过与 sqldb 数据库管理器连接。这有效,但我没有看到任何表格或数据。任何帮助表示高度赞赏!
采纳答案by Nick Holt
HSQL is in memory, so when you say that you're connecting with SQLDB Database Manager, you're not - you are instead connecting to another database in the memory space of the SQLDB Database Manager, not the one in the memory space of the unit test. This is why the database in the SQLDB Database Manager is empty.
HSQL 在内存中,所以当你说你正在与 SQLDB 数据库管理器连接时,你不是 - 而是连接到 SQLDB 数据库管理器内存空间中的另一个数据库,而不是连接到 SQLDB 数据库管理器内存空间中的数据库。单元测试。这就是 SQLDB 数据库管理器中的数据库为空的原因。
You can run HSQL as a server using org.hsqldb.Server
as described here.
您可以使用此处org.hsqldb.Server
所述将HSQL 作为服务器运行。
Although the org.hsqldb.Server
class is typically used to start-up a seperate process, you could instantiate and configure it in your unit test, which should allow a remote process to connect and query the database.
尽管org.hsqldb.Server
该类通常用于启动一个单独的进程,但您可以在单元测试中实例化和配置它,这应该允许远程进程连接和查询数据库。
Alternatively, you'll have to write some sort of dump functionality that is called from within your unit test as need be.
或者,您必须根据需要编写某种从单元测试中调用的转储功能。
As an aside, using HSQL in unit tests is just proving your code works against HSQL, which is different to the actual database. This means you can get false positives and vice versa. The same thing can be achieved with a mocking API or better, save the database testing for some decent integration tests that works with the real database.
顺便说一句,在单元测试中使用 HSQL 只是证明您的代码适用于 HSQL,这与实际数据库不同。这意味着您可能会得到误报,反之亦然。同样的事情可以通过模拟 API 或更好的方式来实现,将数据库测试保存为一些与真实数据库一起工作的体面的集成测试。
回答by dimdm
In your unit test or in the @Before
/ setUp()
method, you can add the following line to launch the HSQL Database Manager:
在您的单元测试或@Before
/setUp()
方法中,您可以添加以下行来启动 HSQL 数据库管理器:
org.hsqldb.util.DatabaseManager.main(new String[] {
"--url", "jdbc:hsqldb:mem:testdb", "--noexit"
});
or for the improved Swing version
或者对于改进的 Swing 版本
org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
"--url", "jdbc:hsqldb:mem:testdb", "--noexit"
});
The DB manager lets you inspect your schema and run SQL queries on the live in-memory database while the application is running.
数据库管理器允许您在应用程序运行时检查架构并在实时内存数据库上运行 SQL 查询。
Make sure to set a beakpoint or pause the execution one way or another if you want to check the state of your database at a specific line.
如果您想在特定行检查数据库的状态,请确保设置一个断点或以一种或另一种方式暂停执行。
回答by Gwaptiva
Run your unit test to a breakpoint, then in the Debug perspective of Eclipse, open the Display view (Window, Show View, Display) and enter
运行你的单元测试到一个断点,然后在 Eclipse 的 Debug 透视图中,打开 Display 视图(Window、Show View、Display)并输入
org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
"--url", "jdbc:hsqldb:mem:testdb", "--noexit"
});
(as per dimdm's post), highlight it, right-click and choose Execute.
(根据dimdm 的帖子),突出显示它,右键单击并选择执行。
回答by mic.sca
You could also use the DatabaseManagerSwing class included in [HSQLDB][1] passing to it an open connection, that allows you to see the state of the database in the transaction the connection is in.
您还可以使用 [HSQLDB][1] 中包含的 DatabaseManagerSwing 类向其传递一个打开的连接,该类允许您查看连接所在事务中的数据库状态。
DatabaseManagerSwing manager = new DatabaseManagerSwing();
manager.main();
manager.connect(connection);
manager.start();
回答by Thiagarajan Ramanathan
The above accepted answer works perfectly only if the database name, username and password is untouched (testdb, SA, blank password).
仅当数据库名称、用户名和密码未受影响(testdb、SA、空白密码)时,上述接受的答案才能完美运行。
For custom database name, username and password you will get the following exception
对于自定义数据库名称、用户名和密码,您将收到以下异常
java.sql.SQLInvalidAuthroizationSpecException: invalid authorization specification - not found: SA
java.sql.SQLInvalidAuthroizationSpecException:无效的授权规范 - 未找到:SA
Then, you have to connect manually.
然后,您必须手动连接。
To connect directly use the following snippet
要直接连接,请使用以下代码段
org.hsqldb.util.DatabaseManager.main(new String[] {
"--url", "jdbc:hsqldb:mem:yourdbname", "--noexit",
"--user", "dbusername", "--password", "dbpassword"
});
or for the improved Swing version
或者对于改进的 Swing 版本
org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
"--url", "jdbc:hsqldb:mem:yourdbname", "--noexit",
"--user", "dbusername", "--password", "dbpassword"
});
Before executing the above snippet update the following
在执行上述代码段之前更新以下内容
- yourdbname - Update yourdbname with real database name
- dbusername - Update dbusername with your database username
- dbpassword - Update dbpassword with your database password
- yourdbname - 用真实的数据库名称更新 yourdbname
- dbusername - 使用您的数据库用户名更新 dbusername
- dbpassword - 使用您的数据库密码更新 dbpassword
回答by Marius Kohmann
The problem with the database-manager freezing can be resolved by starting the database-manager in a new thread, and sleeping the thread the test runs on.
数据库管理器冻结的问题可以通过在新线程中启动数据库管理器并休眠测试运行的线程来解决。
Add this code snippet to your test and you will be able to inspect the database while debugging. Remember to change the database-url to your database.
将此代码片段添加到您的测试中,您将能够在调试时检查数据库。请记住将 database-url 更改为您的数据库。
Thread t = new Thread(new Runnable() {
@Override
public void run() {
org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
"--url", "jdbc:hsqldb:mem://localhost:9001", "--noexit"
});
}
});
t.start();
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
e.printStackTrace();
}