Connect to the embedded H2 database using the H2 console Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .
Accessing H2 Console. Start the spring boot application and access the console in the browser with this URL : http://localhost:8080/h2 . We can see the console like this. Now enter the configured username and password.
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 .
Pretty much the same question as View content of H2 or HSQLDB in-memory database.
Simply add the following to your configuration.
<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,-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,-webPort,8082"/>
</bean>
This will start both H2 web console and TCP server in the same JVM as your embedded database so that you can access port 8082 with your web browser (enter jdbc:h2:mem:dataSource as URL), or access port 9092 with external SQL client such as SQuirreLSQL and view the same data.
With spring boot you can do this with couple of configurations in the application.properties file.
spring.h2.console.enabled=true
spring.h2.console.path=/console/
Then you can access h2 web console in http://localhost:8080/console/. Default login configuration should work unless you change them.
See spring boot documentation.
The database URL jdbc:h2:mem:dataSource
means you are using an in-memory database. Now if you start a second Java process and connect to this database, you will end up having two in-memory databases (one for each process).
If you want to connect to the existing database, you have multiple options:
Connect to the database from within the same process. Don't start a second process.
Use a persisted database, with a hardcoded absolute path, for example: `jdbc:h2:/data/db/dataSource'.
More complicated / not recommended: If you start a second process, you could theoretically connect to an in-memory database using the server mode. But that means you need to start the server where you ran the test.
When using Spring Boot you can register the H2 Console Servlet as follows:
@Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings("/console/*");
registration.addInitParameter("webAllowOthers", "true");
return registration;
}
If you want the console to be available remotely, the important line is the addInitParameter
to set the "webAllowOthers"
to "true"
.
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