Consider we have a H2
database which is started from a web-application under Tomcat
using Hibernate
. In other words it is an embedded H2
database into the application.
The question: is it possible to programmatically stop this H2
server from this application and then start it again?
P.S. Server.createTcpServer(args).start();
or Server.shutdown(...)
is not the way, because it is in the embedded mode.
Step 3: Verify H2 Database InstallationClick Windows → type H2 Console → Click H2 console icon. Connect to the URL http://localhost:8082. At the time of connecting, the H2 database will ask for database registration as shown in the following screenshot.
In this Hibernate H2 database tutorial, you will learn how to create a Hibernate Application to connect the H2 in-memory database. Hibernate is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.
In this particular case such an approach will be a workable solution:
To stop H2 database just use SHUTDOWN
sql command:
session.createSQLQuery("SHUTDOWN").executeUpdate();
To restart H2 you don't need to do anything: the Tomcat's connection pool will do it automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With