我在我的项目中使用HSQLDB(文件模式),Spring 4.2.2,Hibernate 5,JavaFX。
我的数据库以文件模式启动。 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>
一切正常,没有一些问题。
但我需要对数据库脚本文件进行一些操作,我必须关闭我的数据库几秒钟。为此,我使用这种方法并且它也正常工作:
public class HibernateUtilDao extends AbstractDao {
public void shutdownServer(){
getSession().createSQLQuery("SHUTDOWN").executeUpdate();
}
...
}
对于那些操作,必须由主线程释放database.script文件。但现在我需要以某种方式重启我的数据库。
问题: 如何从我的程序重启我的数据库?你能否就这个问题提出一些建议或代码示例?**
答案 0 :(得分:1)
对于file:
数据库,在关闭后,您只需创建与数据库的新连接,新连接将自动打开数据库。如果在此之后创建新连接,它们将连接到打开的数据库。