Enterprise Java Development@TOPIC@
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
Embedded/in-memory
Embedded/file
Server-based
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.
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
Within the JBoss installation
wildfly-x.x.x.Final/modules/.../com/h2database/h2/main/
Internet Maven Repository ( http://repo2.maven.org/maven2/com/h2database/h2/)
Local Maven Repository: HOME/.m2/repository/com/h2database/h2/ if previously downloaded by a DB example module
Product Web Site ( http://h2database.com/html/download.html)
Start database, web server, and launch browser page
java -jar h2.jar
Start database and web server only
java -jar h2.jar -tcp -web
If the database comes up delayed and slow and your development system (like mine) has a lot of virtual networks defined, override the default behavior of binding to all addresses and specify a specific one.
java -Dh2.bindAddress=127.0.0.1 -jar h2.jar
Connect to URL http://127.0.1.1:8082 from a browser
Use JDBC URL: jdbc:h2:tcp://127.0.0.1:9092/./h2db/ejava
Log in as user "sa" and empty password
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
add -P\!Ph2db -Ph2srv to the command line of each build
The bang ("!") character turns off a profile. Unix shells require the bang ("!") character to be escaped. Windows DOS does not.
change the settings.xml activeProfile specification from embedded mode (h2db)
<activeProfile>h2db</activeProfile>
to server mode (h2srv)
<activeProfile>h2srv</activeProfile>
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>
Change standalone/configuration/standalone.xml from
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
...
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
to
<connection-url>jdbc:h2:tcp://${jboss.bind.address:127.0.0.1}:9092/./h2db/ejava</connection-url>
...
<security>
<user-name>sa</user-name>
<password></password>
</security>
This will use the same database file(s) as before in folder
called h2db
relative to where you started the database server.