关闭后如何以编程方式重新启动HSQLDB(文件模式)? [英] How to restart HSQLDB(file mode) programmatically after the shutdown?

查看:94
本文介绍了关闭后如何以编程方式重新启动HSQLDB(文件模式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用的是HSQLDB(文件模式),Spring 4.2.2,Hibernate 5,JavaFX.

I am using HSQLDB(file mode), Spring 4.2.2 , Hibernate 5, JavaFX in my project.

我的数据库以文件模式启动. database.xml:

My database is starting in the file-mode. database.xml:

  <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
                 xmlns:beans="http://www.springframework.org/schema/beans"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

                 http://www.springframework.org/schema/tx
                 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                 http://www.springframework.org/schema/jdbc
                 http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

        <beans:bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
            <beans:property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
            <beans:property name="url"
                            value="jdbc:hsqldb:file:D:/database/statistical_reports;hsqldb.write_delay=true;shutdown=false"/>
            <beans:property name="username" value="sa"/>
            <beans:property name="password" value=""/>
        </beans:bean>

        <jdbc:initialize-database data-source="dataSource">
            <jdbc:script location="classpath*:database/database_schema.sql"/>
        </jdbc:initialize-database>

        <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <beans:property name="dataSource" ref="dataSource"/>
            <beans:property name="configLocation" value="hibernate/hibernate.cfg.xml"/>
            <beans:property name="annotatedClasses">
                <beans:list>
                    ... list of hibernate entities...
                </beans:list>
            </beans:property>
        </beans:bean>

        <beans:bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <beans:property name="sessionFactory" ref="sessionFactory"/>
        </beans:bean>

    </beans:beans>

一切正常,没有任何问题.

All is working fine and without some problems.

但是我需要对数据库脚本文件进行一些操作,并且必须关闭数据库几秒钟.为此,我正在使用这种方法,而且效果还不错:

But I need to make some manipulations with database script file and I must to shutdown for my database for a few second. For this, I am using this method and it's also working fine:

    public class HibernateUtilDao extends AbstractDao {
        public void shutdownServer(){
            getSession().createSQLQuery("SHUTDOWN").executeUpdate();
        }
        ...
    }

对于这些操作,必须由主线程释放database.script文件.但是现在我需要以某种方式重新启动数据库.

And for those manipulations, database.script file must to be released by main thread. But now I need to restart my database somehow.

问题: 如何从程序重新启动数据库?您可以为此问题提供一些建议或代码示例吗?**

Question: How to restart my database from my program? Can you please give some advice or code example for this question?**

推荐答案

对于file:数据库,关闭后,您可以简单地创建与数据库的新连接,新连接将自动打开数据库.如果您在此之后创建新的连接,它们将连接到打开的数据库.

With file: databases, after shutdown, you can simply create a new connection to the database and the new connection will open the database automatically. If you create new connections after this, they will connect to the open database.

这篇关于关闭后如何以编程方式重新启动HSQLDB(文件模式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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