我有一个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
问题:
答案 0 :(得分:5)
要访问Spring启动应用程序创建的HSQL数据库,您必须启动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 url连接到HSQL服务器
JDBC:HSQLDB:HSQL:// IP地址:端口/ TESTDB
当然,加载JDBC驱动程序类需要hsqldb.jar
。
答案 1 :(得分:1)
只是为了回答beckyang的回答,这是我的方法。
包含将日志重定向到slf4j的hack。
包括指定相应的数据源。
import org.hsqldb.jdbc.JDBCDataSource; import org.hsqldb.server.Server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; @Configuration public class DataSourceConfiguration { private final Logger log = LoggerFactory.getLogger(getClass()); @Bean(initMethod = "start", destroyMethod = "stop") @ConfigurationProperties//(prefix = "alarms.idempotent.server") public Server idempotentServer(@Value("${alarms.idempotent.server.path}") String path, @Value("${alarms.idempotent.port}") int port, @Value("${alarms.idempotent.name}") String name) { Server server = new Server(); server.setDatabaseName(0, name); server.setDatabasePath(0, path); server.setPort(port); server.setLogWriter(slf4jPrintWriter()); server.setErrWriter(slf4jPrintWriter()); return server; } @Bean("idempotentDataSource") @Primary @ConfigurationProperties public DataSource idempotentDataSource(@Value("${alarms.idempotent.datasource.url}") String urlNoPath, @Value("${alarms.idempotent.name}") String name) { JDBCDataSource jdbcDataSource = new JDBCDataSource(); String url = urlNoPath; if (!url.endsWith("/")) { url += "/"; } url += name; jdbcDataSource.setUrl(url); jdbcDataSource.setUser("sa"); return jdbcDataSource; } private PrintWriter slf4jPrintWriter() { PrintWriter printWriter = new PrintWriter(new ByteArrayOutputStream()) { @Override public void println(final String x) { log.debug(x); } }; return printWriter; } }