如何在Spring Boot测试中连接到HSQLDB内存数据库以执行查询。

huangapple go评论86阅读模式
英文:

How to connect to HSQLDB in-memory in Spring Boot tests to make a query

问题

我有一个使用mysql数据库的Spring Boot应用程序,但在运行测试时会使用HSQLDB。**是否可以连接到这个内存数据库来执行查询?**在测试/应用程序中需要更改什么才能运行它?我已经尝试运行hsql数据库管理器并连接到这个数据库,但它似乎是空的,但实际上它不是空的,因为在应用程序中,我正在调试代码并从这个数据库中读取数据。

英文:

I have a Spring Boot application with mysql db, but when running tests HSQLDB is used. Is it possible to connect to this in-memory database to execute a query?
What I have to change in tests/application to run it? I've already tried to run hsql database manager and connect to this database but it seems to be empty but actually it is not empty because in the application I'm debugging the code and read data from this database.

答案1

得分: 3

你有两个选项。

一个选项是在Spring中启动HyperSQL(HSQLDB)服务器。此选项在此答案中讨论:https://stackoverflow.com/questions/40114231/how-to-start-hsqldb-in-server-mode-from-spring-boot-application 然后,您可以在单独的Java进程中启动HyperSQL DatabaseManagerSwing,并使用URL连接到HyperSQL服务器:jdbc:hsqldb:hsql://localhost/testdb

另一种选择是在Spring中启动HyperSQL DatabaseManagerSwing。您可以修改为服务器提供的bean模板,并启动org.hsqldb.util.DatabaseManagerSwing,而不是服务器。在这种情况下,DatabaseManager应该连接到URL:jdbc:hsqldb:mem:testdb

无论哪种情况,都需要将jdbc:hsqldb:mem:testdb URL指定为Spring数据源。

英文:

You have two options.

One option is to start an HyperSQL (HSQLDB) Server in Spring. This option is discussed here in the answer https://stackoverflow.com/questions/40114231/how-to-start-hsqldb-in-server-mode-from-spring-boot-application You then start the HyperSQL DatabaseManagerSwing in a separate Java process and connect to the HyperSQL Server with the URL: jdbc:hsqldb:hsql://localhost/testdb.

The alternative option is to start the HyperSQL DatabaseManagreSwing in Spring. You can modify the bean template given for the Server and start org.hsqldb.util.DatabaseManagerSwing instead of the Server. In this case, the DatabaseManager should connect with the URL: jdbc:hsqldb:mem:testdb.

Note in both cases, you need to specify the jdbc:hsqldb:mem:testdb URL as the Spring data source.

huangapple
  • 本文由 发表于 2020年8月4日 14:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63241429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定