Author: Tom Jenkinson
Level: Intermediate
Technologies: EJB, CMT, JMS
Summary: The cmt
quickstart demonstrates Container-Managed Transactions (CMT), showing how to use transactions managed by the container.
Target Product: WildFly
Source: https://github.com/wildfly/quickstart/
The cmt
quickstart demonstrates using transactions managed by the container in WildFly Application Server. It is a fairly typical scenario of updating a database and sending a JMS message in the same transaction. A simple MDB is provided that prints out the message sent but this is not a transactional MDB and is purely provided for debugging purposes.
Aspects touched upon in the code:
After users complete this quickstart, they are invited to run through the following quickstarts:
Note: This quickstart uses the H2 database included with WildFly Application Server 11. It is a lightweight, relational example datasource that is used for examples only. It is not robust or scalable, is not supported, and should NOT be used in a production environment!
Prior to EJB, getting the right incantation to ensure sound transactional operation of the business logic was a highly specialised skill. Although this still holds true to a great extent, EJB has provided a series of improvements to allow simplified transaction demarcation notation that is therefore easier to read and test.
With CMT, the EJB container sets the boundaries of a transaction. This differs from BMT (bean managed transactions) where the developer is responsible for initiating and completing a transaction via the methods begin, commit, rollback on a javax.transaction.UserTransaction
.
Take a look at org.jboss.as.quickstarts.cmt.ejb.CustomerManagerEJB
. You can see that this stateless session bean has been marked up with the @javax.ejb.TransactionAttribute annotation.
The available options for this annotation are as follows:
The application this project produces is designed to be run on WildFly Application Server 11 or later.
All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later. See Configure Maven for WildFly 11 to make sure you are configured correctly for testing the quickstarts.
In the following instructions, replace WILDFLY_HOME
with the actual path to your WildFly installation. The installation path is described in detail here: Use of WILDFLY_HOME and JBOSS_HOME Variables.
For Linux: WILDFLY_HOME/bin/standalone.sh -c standalone-full.xml
For Windows: WILDFLY_HOME\bin\standalone.bat -c standalone-full.xml
mvn clean install wildfly:deploy
This will deploy target/cmt.war
to the running instance of the server.
The application will be running at the following URL: http://localhost:8080/cmt/.
You will be presented with a simple form for adding customers to a database.
After a user is successfully added to the database, a message is produced containing the details of the user. An example MDB will dequeue this message and print the following contents:
Received Message: Created invoice for customer named: Jack
When the same customer name is given, a duplicate warning is given and no JMS-message is send to cause the above message.
The customer name should match: letter & '-', otherwise an error is given. This is to show that a 'LogMessage' entity is still stored in the database thanks to the @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
that the method logCreateCustomer in the EJB LogMessageManagerEJB is decorated with.
Note: You will see the following warnings in the server log. You can ignore these warnings.
HHH000431: Unable to determine H2 database version, certain features may not work
mvn wildfly:undeploy
You can also start the server and deploy the quickstarts or run the Arquillian tests from Eclipse using JBoss tools. For general information about how to import a quickstart, add a WildFly server, and build and deploy a quickstart, see Use JBoss Developer Studio or Eclipse to Run the Quickstarts.
NOTE: Within JBoss Developer Studio, be sure to define a server runtime environment that uses the standalone-full.xml
configuration file.
If you want to debug the source code of any library in the project, run the following command to pull the source into your local repository. The IDE should then detect it.
mvn dependency:sources