What is it?
This quickstart demonstrates using transactions managed by the container. 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 a PostgreSQL database using the JPA API
- XA access to a JMS queue
After users complete this quickstart, they 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 JBossAS
What are container managed transactions?
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 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
What makes this an example of container managed transactions?
Take a look at
org.jboss.as.quickstarts.cmt.ejb.CustomerManagerEJBImpl. 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:
- 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 is annotated with this 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.
All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven 3.0 or better.
The application this project produces is designed to be run on JBoss Enterprise Application Platform 6 or JBoss AS 7.
If you have not yet done so, you must Configure Maven before testing the quickstarts.
Configure the application server to use PostgreSQL
This quickstart requires the PostgreSQL database. Instructions to install an configure PostgreSQL can be found here: Install and Configure the PostgreSQL Database
Note: For the purpose of this quickstart, replace the word QUICKSTART_DATABASENAME with
cmt-quickstart-database in the PostgreSQL instructions.
- Be sure to start the PostgreSQL database. Unless you have set up the database to automatically start as a service, you must repeat the instructions "Start the database server" for your operating system every time you reboot your machine.
Add the PostgreSQL Module to the JBoss server
- Add the PostgreSQL driver to the JBoss server configuration file.
Start JBoss Enterprise Application Platform 6 or JBoss AS 7 with the Full Profile
- Open a command line and navigate to the root of the JBoss server directory.
The following shows the command line to start the server with the full profile:
For Linux: JBOSS_HOME/bin/standalone.sh -c standalone-full.xml For Windows: JBOSS_HOME\bin\standalone.bat -c standalone-full.xml
Build and Deploy the Quickstart
NOTE: The following build command assumes you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See Build and Deploy the Quickstarts for complete instructions and additional options.
- Make sure you have started the JBoss Server with the PostgreSQL driver
- Open a command line and navigate to the root directory of this quickstart.
Type this command to build and deploy the archive:
mvn clean package jboss-as:deploy
This will deploy
target/jboss-as-cmt.warto the running instance of the server.
Access the application
To access the application type the following into a browser: http://localhost:8080/jboss-as-cmt/
The application will be running at the following URL: http://localhost:8080/jboss-as-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: Tom
Undeploy the Archive
- Make sure you have started the JBoss Server as described above.
- Open a command line and navigate to the root directory of this quickstart.
When you are finished testing, type this command to undeploy the archive:
Run the Quickstart in JBoss Developer Studio or Eclipse
You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see Use JBoss Developer Studio or Eclipse to Run the Quickstarts
Debug the Application
If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.
mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc
Find this guide useful?
Find a bug in the guide? Something missing? You can fix it by forking the repository, making the correction and sending a pull request. If you're just plain stuck, feel free to ask a question in the user discussion forum.
- Feb 12, 2013: Add quickstart source repository of record to the readme files Sande Gilda
- Dec 12, 2012: Fix issue 378: issues with cmt and jts readme files Sande Gilda
- Sep 11, 2012: Add target product (https://issues.jboss.org/browse/jdf-108) Sande Gilda
- May 18, 2012: Move metadata to quickstarts Pete Muir
- Apr 11, 2012: Remove odd trailing slashes Pete Muir
- Apr 11, 2012: Update quickstart table. clarify postgresql configuration Sande Gilda
- Apr 10, 2012: Solve issue #194 :-) auto transform links from readme.md -> readme.html Pete Muir
- Apr 05, 2012: Missed adding the arquillian instructions, fixed indentations, modified jta-crash-rec and jts-distributed-crash-rec, add missing authors Sande Gilda
- Apr 05, 2012: Use separate databases for cmt and jts Tom Jenkinson
- Jan 23, 2012: Initial version of distributed crashrec Tom Jenkinson
- Apr 01, 2012: More readme cleanup Sande Gilda
- Mar 26, 2012: Attempt to standardize readme.md files Sande Gilda
- Mar 14, 2012: Jbpapp-8403: updated the instructions for starting the server when using eap 6 in the readme.md files for cmt, helloworld-mdb, and jts. also fixed spacing in the jts/readme.md file so that the code blocks in the generated html files will be rendered properly without escaping the tags Farah Juma
- Mar 01, 2012: Updated to remove the test Tom Jenkinson
- Feb 15, 2012: Make all indents 4 spaces for code blocks Pete Muir