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
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.
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
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 server only
java -jar h2.jar -tcp -web
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;LOCK_MODE=0
Log in as user "sa" and empty password
This will create a database folder called "ejava" relative to where you started the database server.
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
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, 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 create a database folder called "ejava" relative to where you started the database server.