问题
I am using HSQLDB(file mode), Spring 4.2.2 , Hibernate 5, JavaFX in my project.
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();
}
...
}
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?**
回答1:
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.
来源:https://stackoverflow.com/questions/38026321/how-to-restart-hsqldbfile-mode-programmatically-after-the-shutdown