Enterprise Java Development@TOPIC@

Chapter 6. H2 Database Setup

6.1. Locate the h2*.jar file
6.2. Start Server
6.3. Access DB User Interface
6.4. Activate H2 Server Profile for Builds
6.5. Update JBoss to use Server Mode

The application server and application clients used in class require a relational database. Application server vendors generally package a lightweight database with their downloads so that the server can be used immediately for basic scenarios. JBoss comes packaged with the H2 database. This database can run in one of three modes

The in-memory and file-based embedded modes require no administrative setup. The database runs within the JVM of its host process. This makes it easy to startup and run tests or demonstrations. The in-memory mode keeps everything in-memory and should be the fastest. The file-based mode stores information to disk and is basically the same thing as the remote server except that it runs local to the client process. In both the file-based and server-based modes -- there can only be one process working on the database file(s) at a time. In server-mode, you can connect your applications and a UI (to manually inspect and modify) the database at the same time.

JBoss and the class examples come setup with the embedded drivers. The application server is initially set to in-memory and the class examples are set to file-based. The class examples are set to file-based mode so that you can test manual DDL schema creation prior to running unit tests while still allowing for zero administration to run tests. The in-memory mode requires that all schema be created by the process hosting the database. JBoss and the class development environment can both be easily modified to use the same database server instance in server-mode and switched back. You will learn how to do that here.

Choose Right Mode for Right Need

  • Embedded mode requires less administration overhead in the test environment but is restricted to a single client.

  • Server mode provides access to database state during application execution from multiple clients -- which is good for debugging.

  • File-based embedded and server-modes allow you to leverage external plugins that access your database independently to create and populate your database tables separate from your application code.

Obtain a copy of the H2 database jar from one of the following sources

Note

This will create a database file(s) prefixed with ejava in a folder called h2db relative to where you started the database server.

Do one of the following

If you look at the root pom.xml (build/ejava-build-parent/pom.xml), the database server profile defines the following


<profile> <!-- H2 server-based DB -->
    <id>h2srv</id>
    <properties>
          <jdbc.driver>org.h2.Driver</jdbc.driver>
          <jdbc.url>jdbc:h2:tcp://${db.host}:9092/./h2db/ejava</jdbc.url>
          <jdbc.user>sa</jdbc.user>
          <jdbc.password/>
          <hibernate.dialect>
              org.hibernate.dialect.H2Dialect
          </hibernate.dialect>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</profile>