Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing play project database with h2-browser

I am having some trouble accessing the mem database via the h2-browser on a Play framework project.

With the configuration below, that I think is the correct one (apparently not!) I am getting a h2-browser, but with no tables (beside schema, that is), even though I have applied some migrations.

What am I missing here? Thanks in advance.

conf/application.conf:

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.user=sa
db.default.password=""

conf

like image 716
nunos Avatar asked Apr 13 '13 15:04

nunos


People also ask

How do I read H2 database files?

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 .


2 Answers

When you are using in-mem databases actually you are accessing two different databases (although with the same path). You have two solutions:

  1. First from your console start bare 'play' console, run h2-browser and finally run your app (withinh the same play console)
  2. Run H2 in server mode, additional beneffit from this approach is fact that you won't loose your data at every app's restart as DB is stored in the file. Then you can access this database from many points (also from 'standalone' H@ browser or some other GUI) with path similar to:

Unix (~ means your home directory)

db.default.url="jdbc:h2:tcp://localhost/~/some/path/to/MyPlayDB"

or Windows

db.default.url="jdbc:h2:tcp://localhost/c:/some/path/to/MyPlayDB"

Note that for best performance in production mode it's worthy switch back to embedded mode, however for dev stage that solution should be good enough (still faster than for an example MySQL)

like image 165
biesior Avatar answered Nov 23 '22 13:11

biesior


To browse the contents of your database via the H2 Web Console, start both the web console and the Play application via the same Play console:

  1. At first, enter the Play console by running the Typesafe Activator:
    ~/Projects/play-app $ activator
  2. Start the web console:
    [play-app] $ h2-browser
    This should open the H2 console interface in your browser.
  3. Run the Play app:
    [play-app] $ run
  4. Access the Play app in your browser. This will cause the Play application to connect to the in-memory H2 database and to initialize it with some default data, if any.
  5. Log in to the H2 web console in your browser to inspect the database content. Use the following settings which you can save (for instance, as Play-App In-Memory Database) to easily access them again later:
    • Driver Class: org.h2.Driver
    • JDBC URL: jdbc:h2:mem:play
    • User Name: sa
    • Password: <blank>

Note that the above settings have to match your actual configuration (db.default.url and so on).

like image 24
Martin Avatar answered Nov 23 '22 13:11

Martin