如何从Spring Boot应用程序以服务器模式启动HSQLDB [英] How to start HSQLDB in server mode from Spring boot application

查看:140
本文介绍了如何从Spring Boot应用程序以服务器模式启动HSQLDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring启动应用程序,它使用jpa数据和hsqldb 2.3.3(在Centos 7中)运行,该应用程序运行良好,但是我想使用HSQLDB数据库管理器检查数据状态,但是失败了:

application.properties:

spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

启动HSQLDB的命令:

java -cp /home/mycentos/.m2/repository/org/hsqldb/hsqldb/2.3.3/hsqldb-2.3.3.jar org.hsqldb.util.DatabaseManagerSwing

如果我尝试以HSQLDB服务器模式登录,则会弹出Connection refused错误

jdbc:hsqldb:hsql://localhost/testdb

如果我尝试登录内存数据库,则可以登录,但没有显示任何表和数据

jdbc:hsqldb:hsql:testdb

问题:

  1. 如何使其起作用?
  2. 我是否必须从tomcat部署文件夹中引用hsqldb.jar,因为这是应用程序使用的那个?
  3. 从Spring应用程序在服务器模式或内存模式下配置hsqldb是否有任何配置差异?
  4. 任何方法都可以使内存模式在这种情况下工作(通过创建的Spring Boot数据库检查数据)吗?

解决方案

要访问由Spring Boot应用创建的HSQL DB,您必须启动HSQL服务器.例如,创建一个XML配置文件hsql_cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hqlServer" class="org.hsqldb.server.Server" init-method="start" destroy-method="stop">
    <property name="properties"><bean class="org.hsqldb.persist.HsqlProperties">
        <constructor-arg><props>
        <prop key="server.database.0">mem:testdb</prop>
        <prop key="server.dbname.0">testdb</prop><!--DB name for network connection-->
        <prop key="server.no_system_exit">true</prop>
        <prop key="server.port">9001</prop><!--default port is 9001 -->
        </props></constructor-arg>
    </bean></property>
</bean>
</beans>

这里是在主应用程序类中导入XML配置的示例.

@SpringBootApplication
@ImportResource(value="classpath:/package/hsql_cfg.xml")
public class MyApplication {
}

HSQL服务器将从Spring启动应用程序启动.其他应用程序可以使用JDBC网址

连接到HSQL服务器

jdbc:hsqldb:hsql://ip_address:port/testdb

当然,hsqldb.jar是加载JDBC驱动程序类所必需的.

I have a Spring boot application, running with jpa data and hsqldb 2.3.3 (in Centos 7), the application runs fine but I would like to use HSQLDB database manager to check the data status, however it failed:

application.properties:

spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

Command to start HSQLDB:

java -cp /home/mycentos/.m2/repository/org/hsqldb/hsqldb/2.3.3/hsqldb-2.3.3.jar org.hsqldb.util.DatabaseManagerSwing

If I tried to log in with HSQLDB server mode, it pops Connection refused error

jdbc:hsqldb:hsql://localhost/testdb

If I tried to log in in-memory db, I can log in but no table and data showing up

jdbc:hsqldb:hsql:testdb

Question:

  1. How to make it works?
  2. Do I have to refer to the hsqldb.jar from tomcat deployment folder because that is the one using by the application?
  3. Any configuration difference to configure hsqldb in server mode or in-memory mode from Spring application?
  4. Can any method make in-memory mode working in such situation (to check data by db created Spring boot)?

解决方案

To access the HSQL DB created by Spring boot app, you have to start HSQL server. For example, create a XML configuration file hsql_cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hqlServer" class="org.hsqldb.server.Server" init-method="start" destroy-method="stop">
    <property name="properties"><bean class="org.hsqldb.persist.HsqlProperties">
        <constructor-arg><props>
        <prop key="server.database.0">mem:testdb</prop>
        <prop key="server.dbname.0">testdb</prop><!--DB name for network connection-->
        <prop key="server.no_system_exit">true</prop>
        <prop key="server.port">9001</prop><!--default port is 9001 -->
        </props></constructor-arg>
    </bean></property>
</bean>
</beans>

Here is a example to import the XML configuration in main application class.

@SpringBootApplication
@ImportResource(value="classpath:/package/hsql_cfg.xml")
public class MyApplication {
}

The HSQL server will start with Spring boot app. Other applications could connect to the HSQL server using JDBC url

jdbc:hsqldb:hsql://ip_address:port/testdb

Of course, hsqldb.jar is required for loading JDBC driver class.

这篇关于如何从Spring Boot应用程序以服务器模式启动HSQLDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆