# cmt: Container Managed Transactions (CMT)

Tom Jenkinson

> The `cmt` quickstart demonstrates Container-Managed Transactions (CMT), showing how to use transactions managed by the container.

## What is it?

The `cmt` quickstart demonstrates how to use container-managed transactions (CMT), which are 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:

- XA transaction control using the container managed transaction annotations

- XA access to the standard default datasource using the JPA API

- XA access to a JMS queue

After you complete this quickstart, you are invited to run through the following quickstarts:

- jts - The `jts` quickstart builds upon this quickstart by distributing the `CustomerManager` and `InvoiceManager`.

- jts-distributed-crash-rec - The crash recovery quickstart builds upon the jts quickstart by demonstrating the fault tolerance of WildFly.

### What are Container Managed Transactions?

Prior to EJB, getting the right incantation to ensure sound transactional operation of the business logic was a highly specialized 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 using the `begin`, `commit`, and `rollback` methods on a `javax.transaction.UserTransaction`.

### What Makes This an Example of Container Managed Transactions?

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 following options are available for this annotation.

Required

  As demonstrated in the quickstart. If a transaction does not already exist, this will initiate a transaction and complete it for you, otherwise the business logic will be integrated into the existing transaction.

RequiresNew

  If there is already a transaction running, it will be suspended, the work performed within a new transaction which is completed at exit of the method and then the original transaction resumed.

Mandatory

  If there is no transaction running, calling a business method with this annotation will result in an error.

NotSupported

  If there is a transaction running, it will be suspended and no transaction will be initiated for this business method.

Supports

  This will run the method within a transaction if a transaction exists, alternatively, if there is no transaction running, the method will not be executed within the scope of a transaction.

Never

  If the client has a transaction running and does not suspend it but calls a method annotated with Never then an EJB exception will be raised.

## Considerations for Use in a Production Environment

H2 Database

  This quickstart uses the H2 database included with WildFly Application Server 13. 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.

## System Requirements

The application this project produces is designed to be run on WildFly Application Server 13 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 to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.

## Use of WILDFLY_HOME

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.

## Start the WildFly Standalone Server

1. Open a terminal and navigate to the root of the WildFly directory.

2. Start the WildFly server with the full profile by typing the following command.

       $ WILDFLY_HOME/bin/standalone.sh -c standalone-full.xml

   Note | For Windows, use the `WILDFLY_HOME\bin\standalone.bat` script.

## Build and Deploy the Quickstart

1. Make sure you start the WildFly server as described above.

2. Open a terminal and navigate to the root directory of this quickstart.

3. Type the following command to build the artifacts.

       $ mvn clean package wildfly:deploy

This deploys the `cmt/target/cmt.war` to the running instance of the server.

You should see a message in the server log indicating that the archive deployed successfully.

## Access the Application

The application will be running at the following URL: http://localhost:8080/cmt/

You are presented with a simple form for adding customers to a database.

After a customer is successfully added to the database, a message is produced containing the details of the customer. An example MDB dequeues this message and print the following contents.

    Received Message: Created invoice for customer named:  Jack

If an existing customer name is provided, no JMS message is sent. Instead of the above message, a duplicate warning is displayed.

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. That is because the `logCreateCustomer` method in the `LogMessageManagerEJB` EJB is decorated with the `@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)` annotation.

## Server Log: Expected Warnings and Errors

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

## Undeploy the Quickstart

When you are finished testing the quickstart, follow these steps to undeploy the archive.

1. Make sure you start the WildFly server as described above.

2. Open a terminal and navigate to the root directory of this quickstart.

3. Type this command to undeploy the archive:

       $ mvn wildfly:undeploy

## Run the Quickstart in Red Hat JBoss Developer Studio or Eclipse

You can also start the server and deploy the quickstarts or run the Arquillian tests in Red Hat JBoss Developer Studio or 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, make sure you define a server runtime environment that uses the `standalone-full.xml` configuration file.

## Debug the Application

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