Search code examples
javaintellij-ideajbosswildflyh2

How to add H2 to Wildfly, so I can see the data in the database?


I planned to use ExampleDS (java:jboss/datasources/ExampleDS) for Java development before going to production. It is the default data source in Wildfly, configured using the embedded H2 database for developer convenience. I need to see the data in these tables during development. I am developing in IntelliJ. There is a ”database” tab to the upper right. I have previously added databases here and I could see their content. But I don’t know how to add the ExampleDS database.

I then tried this approach instead: 1) Deploy a prebuilt WAR-file for H2 Console: https://www.cs.hs-rm.de/~knauf/JavaEE6/kuchen/H2Console.war )

2) Open URL http://localhost:8080/H2Console/h2

3) login with username/password = "sa"

I could then access the H2 console. It was however impossible to login with the default JDBC URL java:jboss/datasources/ExampleDS I tried with name and password sa, but it always says "wrong password".

So I read the documentation on https://developers.redhat.com/quickstarts/eap/h2-console/

It says: To access the test database that the greeter quickstart uses, enter these details:

JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1 User Name: sa Password: sa

So I tried that and I could finally connect. But there seems to be no tables in the database. The documentation says:

Take a look at the data added by the greeter application. Run the following SQL command:

select * from users;

I tried that, but got the message

Table "USERS" not found

The tables that I have created in my JPA app can also not be found. I assume that is because my persistance file points to

java:jboss/datasources/ExampleDS

So I changed it to

jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1

Then I got this error when trying to run my JPA app from IntelliJ:

"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1"], "WFLYCTL0180: Services with missing/unavailable dependencies" => [

Does anypne know how I should proceed with this to actually add my tables to a H2 database and then to see the data in these tables?


Solution

  • I think you don't have some of the concepts clear: H2 has several options to create a database, using a file as basis or in-memory.

    The in-memory is not stored in any place, for this reason if you configure it in your app like this:

    jdbc:h2:mem:whatever
    

    The jvm starts a database in their own memory, only available for the vm.

    If you want to connect from outside I recommend to use the embedded or server mode.

    Ofc an in-memory database will be empty, you have to populate it in start-up.

    I used it a lot for testing. It's pretty cool.

    Check the H2 cheatsheet: https://www.h2database.com/html/cheatSheet.html