I am creating a H2 database in my unit tests. The database uses the following properties:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:target/db/testdb"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
I am using version 1.3.166 of the com.h2database.h2
jar file.
When I run my tests, I see the database created in target/db
directory, and a testdb.h2.db
file exists. My tests run and load data from the database. I can open the target/db/testdb.h2.db
file and see the SQL statements that I used to create the database.
However, when I try to load the target/db/testsb.h2.db
file into a database browsing tool such as DBVisualizer, I cannot see any tables or data. For DBVisualizer I specify the H2(Embedded) mode.
I also tried the H2 console but a show tables
command returns an empty result set.
I can't see what I am doing wrong: the database file exists, the tests run against it correctly, but I cannot open this file in a database browser.
Any suggestions?
Access the H2 Console You can access the console at the following URL: http://localhost:8080/h2-console/. You need to enter the JDBC URL, and credentials. 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.
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:8082/h2-console.
Click 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.
Most likely, the problem is the database URL you use.
jdbc:h2:file:target/db/testdb
This means the database file is stored relative to the current working directory. So it depends where you started the application. If you started DBVisualizer in a different directory (which most likely you did), then it's creating a new database in a different directory.
To ensure you are using the same database, I suggest to use an absolute directory name, or relative to the current user home directory, as described in the H2 documentation:
jdbc:h2:~/relative/to/user/home/dir/testdb
jdbc:h2:/absolute/path/testdb
The prefix file:
is optional.
If you get an error like
Unsupported database file version or invalid file header in file "Old database:
/path/to/databasename.data.db - please convert the database to a SQL script and
re-create it." [90048-167]
the problem is (as the message says) that you are using a database created with an old version of h2. In order to browse the database with dbVisualizer you can create a new database driver:
Tools
> Driver manager...
> Create a new driver
Fill in the fields and select the right h2 jar file, and you should be good to go.
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