Enterprise Java Development@TOPIC@

Chapter 7. H2 Database Setup

7.1. Locate the h2*.jar file
7.2. Start Server
7.3. Access DB User Interface
7.4. Activate H2 Server Profile for Builds
7.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

File-based versus in-memory allows you to do post-mortem analysis of the database after a test completes. File-based also allows you to initialize the database schema in one process and use the database within another. Using server-based mode allows you to inspect the database while the application is running.

JBoss and the class examples come setup with embedded drivers. You can change the configuration at any time to a server-based configuration using the following instructions.

Choose Right Mode for Right Need

Using embedded mode requires less administration overhead in the test environment.

Using server mode provides access to database state during application execution -- which is good for debugging.

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

Note

This will create a database folder called "ejava" relative to where you started the database server.

Note

LOCK_MODE refers to how you want your connection impacted by other transactions in progress. A normal application would want some isolation between transactions, but it is useful to have the UI be able to watch in-progress transactions (i.e., perform dirty reads). The options include:

  • 0 - Read Uncommitted - transaction isolation disabled

  • 1 - Serializable - database is (read and write) locked until transaction commits

  • 3 - Read Committed (default) - read locks released after statement completes

Do one of the following

If you look at the root 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>