【问题标题】:How to start HSQLDB in server mode from Spring boot application如何从 Spring Boot 应用程序以服务器模式启动 HSQLDB
【发布时间】:2017-02-28 01:56:26
【问题描述】:

我有一个 Spring boot 应用程序,使用 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. 任何方法都可以使内存模式在这种情况下工作(通过 db 创建的 Spring boot 检查数据)吗?

【问题讨论】:

    标签: spring jdbc hsqldb


    【解决方案1】:

    要访问由 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 Boot 应用程序启动。其他应用程序可以使用 JDBC url 连接到 HSQL 服务器

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

    当然,加载JDBC驱动类需要hsqldb.jar

    【讨论】:

      【解决方案2】:

      只是为了补充 beckyang 的答案,这是我的方法。

      包括将日志重定向到 slf4j 的 hack。

      包括指定相应的数据源。

      导入 org.hsqldb.jdbc.JDBCDataSource; 导入 org.hsqldb.server.Server; 导入 org.slf4j.Logger; 导入 org.slf4j.LoggerFactory; 导入 org.springframework.beans.factory.annotation.Value; 导入 org.springframework.boot.context.properties.ConfigurationProperties; 导入 org.springframework.context.annotation.Bean; 导入 org.springframework.context.annotation.Configuration; 导入 org.springframework.context.annotation.Primary; 导入 javax.sql.DataSource; 导入 java.io.ByteArrayOutputStream; 导入 java.io.PrintWriter; @配置 公共类 DataSourceConfiguration { private final Logger log = LoggerFactory.getLogger(getClass()); @Bean(initMethod = "开始", destroyMethod = "停止") @ConfigurationProperties//(前缀 = "alarms.idempotent.server") 公共服务器 idempotentServer(@Value("${alarms.idempotent.server.path}") 字符串路径,@Value("${alarms.idempotent.port}") int 端口,@Value("${alarms.idempotent.名称}") 字符串名称) { 服务器服务器=新服务器(); server.setDatabaseName(0, name); server.setDatabasePath(0, path); server.setPort(端口); server.setLogWriter(slf4jPrintWriter()); server.setErrWriter(slf4jPrintWriter()); 返回服务器; } @Bean("幂等数据源") @基本的 @ConfigurationProperties public DataSource idempotentDataSource(@Value("${alarms.idempotent.datasource.url}") String urlNoPath, @Value("${alarms.idempotent.name}") 字符串名称) { JDBCDataSource jdbcDataSource = new JDBCDataSource(); 字符串 url = urlNoPath; if (!url.endsWith("/")) { 网址+=“/”; } 网址 += 名称; jdbcDataSource.setUrl(url); jdbcDataSource.setUser("sa"); 返回 jdbcDataSource; } 私人 PrintWriter slf4jPrintWriter() { PrintWriter printWriter = new PrintWriter(new ByteArrayOutputStream()) { @覆盖 公共无效println(最终字符串x){ log.debug(x); } }; 返回打印作者; } }

      【讨论】:

        猜你喜欢
        • 2018-07-12
        • 2016-09-01
        • 2011-04-09
        • 2020-11-05
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        相关资源
        最近更新 更多