Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View content of H2 or HSQLDB in-memory database

Is there a way to browse the content of an H2 or an HSQLDB in-memory database for viewing? For example, during a debugging session with Hibernate in order to check when the flush is executed; or to make sure the script that instantiates the DB gives the expected result.

Does it exist an addon or a library that you can embed with your code in order to allow this?

Please, mention which one you're talking about (H2 or HSQLDB) in case you have an answer specific to one of them.

like image 988
jplandrain Avatar asked Sep 05 '11 14:09

jplandrain


People also ask

How do I view H2 in-memory database?

To access an in-memory database from another process or from another computer, you need to start a TCP server in the same process as the in-memory database was created. The other processes then need to access the database over TCP/IP or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1 .

How do I open H2 in-memory database in browser?

H2 Console Before accessing the H2 database, we must enable it by using the following property. Once we have enabled the H2 console, now we can access the H2 console in the browser by invoking the URL http://localhost:8080/h2-console.

Is H2 in-memory database?

H2 is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It stores data in memory, not persist the data on disk.

What is Hsqldb in-memory database?

HSQLDB (HyperSQL Database) HSQLDB is an open source project, also written in Java, representing a relational database. It follows the SQL and JDBC standards and supports SQL features such as stored procedures and triggers. It can be used in the in-memory mode, or it can be configured to use disk storage.


2 Answers

You can run H2 web server within your application that will access the same in-memory database. You can also access the H2 running in server mode using any generic JDBC client like SquirrelSQL.

UPDATE:

Server webServer = Server.createWebServer("-web,-webAllowOthers,true,-webPort,8082").start(); Server server = Server.createTcpServer("-tcp,-tcpAllowOthers,true,-tcpPort,9092").start(); 

Now you can connect to your database via jdbc:h2:mem:foo_db URL within the same process or browse the foo_db database using localhost:8082. Remember to close both servers. See also: H2 database in memory mode cannot be accessed by Console.

You can also use Spring:

<bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop" depends-on="h2WebServer">     <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,9092"/> </bean> <bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer" init-method="start" destroy-method="stop">     <constructor-arg value="-web,-webAllowOthers,true,-webPort,8082"/> </bean>  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" depends-on="h2Server">     <property name="driverClass" value="org.h2.Driver"/>     <property name="jdbcUrl" value="jdbc:h2:mem:foo_db"/> </bean> 

BTW you should only depend on assertions and not on manual peeking the database contents. Use this only for troubleshooting.

N.B. if you use Spring test framework you won't see changes made by a running transaction and this transaction will be rolled back immediately after the test.

like image 152
Tomasz Nurkiewicz Avatar answered Sep 24 '22 00:09

Tomasz Nurkiewicz


For H2, you can start a web server within your code during a debugging session if you have a database connection object. You could add this line to your code, or as a 'watch expression' (dynamically):

org.h2.tools.Server.startWebServer(conn); 

The server tool will start a web browser locally that allows you to access the database.

like image 23
Thomas Mueller Avatar answered Sep 25 '22 00:09

Thomas Mueller