【问题标题】:Connect HSQLDB Server连接 HSQLDB 服务器
【发布时间】:2015-12-28 20:44:29
【问题描述】:

我想通过像松鼠这样的客户端连接我的 hsqldb。在我在内存中使用 hsqldb 之前,为了让该功能与另一个客户端连接,我现在想使用服务器。但我坚持连接它。

到目前为止我所拥有的:

我将 hsqldb 作为 Spring-Boot-Application 启动:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class ApplicationInfra {
    public static void main(final String[] args) {
        final ConfigurableApplicationContext context = SpringApplication.run(ApplicationInfra.class, args);

        final HyperSqlDbServer dbServer = context.getBean(HyperSqlDbServer.class);
        dbServer.displayInfo();

        try (final Scanner sc = new Scanner(System.in)) {
            do {
                System.out.println("Shutdown HSQLDB?[Y/N]: ");
            } while (sc.hasNext() && (!sc.next().equalsIgnoreCase("y")));
        }

        // =============================================================

        // SHUTDOWN DATABASE ...
        final DataSource dataSource = context.getBean(DataSource.class);
        final JdbcTemplate template = new JdbcTemplate(dataSource);
        template.execute("SHUTDOWN");

        context.close();
    }
}

我的 HyperSqlDbServer 类:

@Configuration
public class HyperSqlDbServer implements SmartLifecycle {
    private final Logger logger = LoggerFactory.getLogger(HyperSqlDbServer.class);
    private HsqlProperties properties;
    private Server server;
    private boolean running = false;

    public HyperSqlDbServer() {
        final Properties props = new Properties();
        props.setProperty("server.database.0", "file:./hsqldb/bbsng");
        props.setProperty("server.dbname.0", "bbsng");
        props.setProperty("server.remote_open", "true");
        props.setProperty("server.trace", "true");
        props.setProperty("hsqldb.reconfig_logging", "false");
        properties = new HsqlProperties(props);
    }

    @Override
    public boolean isRunning() {
        if (server != null)
            server.checkRunning(running);
        return running;
    }

    @Override
    public void start() {
        if (server == null) {
            logger.info("Starting HSQL server...");
            server = new Server();
            try {
                server.setProperties(properties);
                server.start();
                running = true;
            } catch (AclFormatException afe) {
                logger.error("Error starting HSQL server.", afe);
            } catch (IOException e) {
                logger.error("Error starting HSQL server.", e);
            }
        }
    }

    @Override
    public void stop() {
        logger.info("Stopping HSQL server...");
        if (server != null) {
            server.stop();
            running = false;
        }
    }

    @Override
    public int getPhase() {
        return 0;
    }

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable runnable) {
        stop();
        runnable.run();
    }

}

我的应用程序属性

# DATA-SOURCE CONFIGURATION:
spring.datasource.url=jdbc\:hsqldb\:bbsng
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.hsqldb.jdbcDriver

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

控制台说休眠创建了新表:

Hibernate: create table apprentice (id bigint not null, city varchar(255), street varchar(255), street_number varchar(255), urban_district varchar(255), zip varchar(255), email varchar(255), fax varchar(255), mobile varchar(255), phone varchar(255), first_name varchar(255) not null, last_name varchar(255) not null, version integer, rural_district bigint, primary key (id))
Hibernate: create table company (id bigint not null, city varchar(255), street varchar(255), street_number varchar(255), urban_district varchar(255), zip varchar(255), email varchar(255), fax varchar(255), mobile varchar(255), phone varchar(255), name varchar(255) not null, number varchar(255) not null, version integer, rural_district bigint, primary key (id))
Hibernate: create table company_occupation_combination (id bigint not null, version integer, company bigint not null, occupation_combination bigint not null, primary key (id))
Hibernate: create table contract (id bigint not null, education_end date not null, education_start date not null, status integer not null, version integer, apprentice bigint not null, company_occupation_combination bigint not null, office bigint not null, primary key (id))
Hibernate: create table district (id bigint not null, name varchar(255) not null, version integer, primary key (id))

...

并且 HSQLDB 服务器已启动:

2015-12-28 21:20:24.765  INFO 9832 --- [           main] at.compax.bbsng.infra.HyperSqlDbServer   : Starting HSQL server...
[Server@35c9a231]: [Thread[main,5,main]]: checkRunning(false) entered
[Server@35c9a231]: [Thread[main,5,main]]: checkRunning(false) exited
[Server@35c9a231]: Initiating startup sequence...
[Server@35c9a231]: Server socket opened successfully in 0 ms.
[Server@35c9a231]: Database [index=0, id=1, db=file:./hsqldb/bbsng, alias=bbsng] opened sucessfully in 40 ms.
[Server@35c9a231]: Startup sequence completed in 50 ms.
[Server@35c9a231]: 2015-12-28 21:20:24.815 HSQLDB server 2.3.3 is online on port 9001
[Server@35c9a231]: To close normally, connect and execute SHUTDOWN SQL
[Server@35c9a231]: From command line, use [Ctrl]+[C] to abort abruptly

但我找不到有松鼠的桌子! 我的配置如下:

【问题讨论】:

    标签: spring-boot server client database-connection hsqldb


    【解决方案1】:

    您只需将您的问题写到 stackoverflow,您就会看到自己的错误。

    我的数据源不正确,休眠将表添加到另一个创建的数据库中。解决办法是在application.properties中修复:

    spring.datasource.url=jdbc\:hsqldb\:file\:./hsqldb/bbsng
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 2019-10-08
      • 2019-10-21
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多