Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop H2 database programmatically

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.

like image 915
Andremoniy Avatar asked Nov 26 '13 10:11

Andremoniy


People also ask

How do I know if H2 is running?

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.

Can we use hibernate with H2 database?

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.


1 Answers

In this particular case such an approach will be a workable solution:

  1. To stop H2 database just use SHUTDOWN sql command:

    session.createSQLQuery("SHUTDOWN").executeUpdate();

  2. To restart H2 you don't need to do anything: the Tomcat's connection pool will do it automatically.

like image 167
Andremoniy Avatar answered Oct 04 '22 20:10

Andremoniy