Enterprise Java Development@TOPIC@
This chapter will cover setup required to start the development database in server-mode. The database has at least three (3) modes.
In-memory mode
File-based mode
Server-based mode
The in-memory option manages all tables within the memory footprint of the JVM. This is very fast and requires no server-process setup -- which makes it a good option for automated unit tests. However, since the DB instance is created and destroyed with each JVM execution it makes a bad choice for modules relying on multiple tools to pre-populate the database prior to executing tests.
The file-based option stores all information in the filesystem. It is useful for multi-JVM (sequential) setup and post-mortem analysis. However only a single client may access the files at one time. I have seen this used effectively when simulating external databases -- where an external setup script populates the database and the JVM running the unit tests just queries the information as they would do in production. We will use this as an option to server-based mode since we are using separate plugins to initialize the database. We also want to treat our database schema as a first-class artifact for our application -- and not rely on test capabilities to instantiate the database for each test.
The server-based option requires a separate process activated but allows concurrent connections from database user interface while the JVM is under test. This chapter will focus on the setup required to run the database in server mode.
Prepare your environment to run the database in server mode for this exercise by following the instructions defined in Development Environment Setup.
Start the database and web server server in a directory where you wish to create database files. Your h2.jar file source be located in M2_REPO/com/h2database/h2/*/h2*.jar to name at least one location. Another location is JBOSS_HOME/modules/com/h2database/h2/main/h2*.jar
cd /tmp
java -jar h2.jar
This should result in a database server process and access to a web-based database UI through the following local URL: http://localhost:8082/login.jsp
Connect to the database server from the web-based UI.
Driver Class: org.h2.Driver
JDBC URL: jdbc:h2:tcp://localhost:9092/./h2db/ejava</jdbc.url>
User Name: sa
Password:
Look in the directory where you started the server. After connecting with a relative URL using the UI, there should be a new "h2db" directory with one or more files called "ejava*". You want to make sure you use the same URL in the UI and application so you are seeing the same instance of the database.
If you use file-based mode, the connection information would look like the following where "./h2db/ejava" must point to the exact same path your JVM under test uses. This can be a relative or fully-qualified path.
Driver Class: org.h2.Driver
JDBC URL: jdbc:h2:./target/h2db/ejava
User Name: sa
Password:
In this chapter you...
located a copy of the Java archive required to run the server
located a scratch area to run the server
started the server
launched the web-based UI
connected to the server using the web-based UI
This is the only database mode that requires administrative setup. You cannot connect to the database running in-memory but you can connect to the database using file-mode once all other JVMs have released their exclusive lock on the database storage files.