Author: Tom Jenkinson
Level: Intermediate
Technologies: JTS
Summary: The jts
quickstart shows how to use JTS to perform distributed transactions across multiple containers, fulfilling the properties of an ACID transaction.
Prerequisites: cmt
Target Product: JBoss EAP
Source: https://github.com/jboss-developer/jboss-eap-quickstarts/
The jts
quickstart demonstrates how to perform distributed transactions across multiple containers in an application deployed to Red Hat JBoss Enterprise Application Platform. A distributed transaction is a set of operations performed by two or more nodes, participating in an activity coordinated as a single entity of work, and fulfilling the properties of an ACID transaction.
ACID is a set of 4 properties that guarantee the resources are processed in the following manner:
- Atomic - if any part of the transaction fails, all resources remain unchanged.
- Consistent - the state will be consistent across resources after a commit
- Isolated - the execution of the transaction for each resource is isolated from each others
- Durable - the data will persist after the transaction is committed
The example uses Java Transaction Service (JTS) to propagate a transaction context across two Container-Managed Transaction (CMT) EJBs that, although deployed in separate servers, participate in the same transaction. In this example, one server processes the Customer and Account data and the other server processes the Invoice data.
The code base is essentially the same as the cmt quickstart, however in this case the InvoiceManager
has been separated to a different deployment archive to demonstrate the usage of JTS. You can see the changes in the
following ways:
cmt/src/main/java/org/jboss/as/quickstarts/cmt/ejb/InvoiceManagerEJB.java
has been moved toapplication-component-2/src/main/java/org/jboss/as/quickstarts/cmt/jts/ejb/InvoiceManagerEJB
cmt/src/main/java/org/jboss/as/quickstarts/cmt/ejb/CustomerManagerEJB.java
has been moved tojts/application-component-1/src/main/java/org/jboss/as/quickstarts/cmt/jts/ejb/CustomerManagerEJB.java
The changes to CustomerManagerEJB
are purely to accommodate the fact that InvoiceManager
is now distributed.
You will see that the CustomerManagerEJB
uses the EJB home for the remote EJB, this is expected to connect to remote EJBs. The example expects the EJBs to be deployed onto the same physical machine. This is not a restriction of JTS and the example can easily be converted to run on separate machines by editing the hostname value for the InvoiceManagerEJB
in org.jboss.as.quickstarts.cmt.jts.ejb.CustomerManagerEJB
.
A simple MDB has been provided that prints out the messages sent but this is not a transactional MDB and is purely provided for debugging purposes.
After you complete this quickstart, you are invited to run through the jts-distributed-crash-rec quickstart. The crash recovery quickstart builds upon this quickstart by demonstrating the fault tolerance of Red Hat JBoss Enterprise Application Platform.
Note: This quickstart uses a *-ds.xml
datasource configuration file for convenience and ease of database configuration. These files are deprecated in JBoss EAP 6.4 and should not be used in a production environment. Instead, you should configure the datasource using the Management CLI or Management Console. Datasource configuration is documented in the Administration and Configuration Guide for Red Hat JBoss Enterprise Application Platform.
The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 6.1 or later.
All you need to build this project is Java 6.0 (Java SDK 1.6) or later, Maven 3.0 or later.
If you have not yet done so, you must Configure Maven before testing the quickstarts.
In the following instructions, replace EAP_HOME
with the actual path to your JBoss EAP 6 installation. The installation path is described in detail here: Use of EAP_HOME and JBOSS_HOME Variables.
Developers should be familiar with the concepts introduced in the cmt quickstart.
This quickstart requires the configuration of two servers. The first server must be configured to use the PostgreSQL database. Instructions to install and configure PostgreSQL are below.
This quickstart requires the PostgreSQL database. Instructions to install and configure PostgreSQL can be found here: Download and Install PostgreSQL
Note: For the purpose of this quickstart, replace the word QUICKSTART_DATABASE_NAME
with jts-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.
Wait until later in these instructions to add the PostgreSQL module and driver configuration to the first JBoss EAP server.
For this example, you will need two instances of the application server, with a subtle startup configuration difference. Application server 2 must be started up with a port offset parameter provided to the startup script as "-Djboss.socket.binding.port-offset=100".
Since both application servers must be configured in the same way, you must configure the first server and then clone it. After you clone the second server, the first server must be configured for PostgreSQL.
Note: This quickstart README file use the following replaceable values. When you encounter these values in a README file, be sure to replace them with the actual path to the correct JBoss EAP 6 server.
EAP_HOME
denotes the path to the original JBoss EAP 6 installation.EAP_HOME_1
denotes the path to the modified JBoss EAP server 1 configuration.EAP_HOME_2
denotes the path to the modified JBoss EAP server 2 configuration.
You configure the security domain by running JBoss CLI commands. For your convenience, this quickstart batches the commands into a configure-transactions.cli
script provided in the root directory of this quickstart.
-
Before you begin, back up your server configuration file
- If it is running, stop the JBoss EAP server.
- Backup the file:
EAP_HOME/standalone/configuration/standalone-full.xml
- After you have completed testing this quickstart, you can replace this file to restore the server to its original configuration.
-
Start the JBoss EAP server with the full profile, passing a unique node ID by typing the following command. Be sure to replace
UNIQUE_NODE_ID_1
with a node identifier that is unique to both servers.For Linux: EAP_HOME/bin/standalone.sh -c standalone-full.xml -Djboss.tx.node.id=UNIQUE_NODE_ID_1 For Windows: EAP_HOME\bin\standalone.bat -c standalone-full.xml -Djboss.tx.node.id=UNIQUE_NODE_ID_1
-
Review the
configure-jts-transactions.cli
file in the root of this quickstart directory. This script configures the server to use jts transaction processing. -
Open a new command prompt, navigate to the root directory of this quickstart, and run the following command, replacing EAP_HOME with the path to your server:
For Linux: EAP_HOME/bin/jboss-cli.sh --connect --file=configure-jts-transactions.cli For Windows: EAP_HOME\bin\jboss-cli.bat --connect --file=configure-jts-transactions.cli
You should see the following result when you run the script:
The batch executed successfully.
{"outcome" => "success"}
- Stop the JBoss EAP server.
NOTE: When you have completed testing this quickstart, it is important to Remove the JTS Configuration from the JBoss EAP Server.
After stopping the server, open the EAP_HOME/standalone/configuration/standalone-full.xml
file and review the changes.
-
The orb initializers
transactions
attribute is changed from "spec" to "on" in thejacorb
subsystem to enable JTS. A naming root is also added to the subsystem.<subsystem xmlns="urn:jboss:domain:jacorb:1.4"> <orb name="${jboss.node.name}" socket-binding="jacorb" ssl-socket-binding="jacorb-ssl"> <initializers security="identity" transactions="on"/> </orb> <naming root-context="${jboss.node.name}/Naming/root"/> </subsystem>
-
An empty
<jts/>
element is added to the the end of thetransactions
subsystem to enable JTS.<subsystem xmlns="urn:jboss:domain:transactions:1.5"> <core-environment node-identifier="${jboss.tx.node.id}"> <process-id> <uuid/> </process-id> </core-environment> <recovery-environment socket-binding="txn-recovery-environment" status-socket-binding="txn-status-manager"/> <coordinator-environment default-timeout="300"/> <jts/> </subsystem>
NOTE: When you have completed testing this quickstart, it is important to Remove the JTS Configuration from the JBoss EAP Server.
Make a copy of this JBoss EAP directory structure to use for the second server.
Application server 1 must be now configured to use PostgreSQL.
- 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.
- Follow the instructions to Add the PostgreSQL Module to the JBoss EAP Server to the server 1 install only.
- Follow the instructions to Configure the PostgreSQL Driver in the JBoss EAP Server for the server 1 configuration.
Start the the two JBoss EAP servers with the full profile, passing a unique node ID by typing the following command. You must pass a socket binding port offset on the command to start the second server. Be sure to replace UNIQUE_NODE_ID
with a node identifier that is unique to both servers.
If you are using Linux:
Server 1: EAP_HOME_1/bin/standalone.sh -c standalone-full.xml -Djboss.tx.node.id=UNIQUE_NODE_ID_1
Server 2: EAP_HOME_2/bin/standalone.sh -c standalone-full.xml -Djboss.tx.node.id=UNIQUE_NODE_ID_2 -Djboss.socket.binding.port-offset=100
If you are using Windows
Server 1: EAP_HOME_1\bin\standalone.bat -c standalone-full.xml -Djboss.tx.node.id=UNIQUE_NODE_ID_1
Server 2: EAP_HOME_2\bin\standalone.bat -c standalone-full.xml -Djboss.tx.node.id=UNIQUE_NODE_ID_2 -Djboss.socket.binding.port-offset=100
Since this quickstart builds two separate components, you can not use the standard Build and Deploy commands used by most of the other quickstarts. You must follow these steps to build, deploy, and run this quickstart.
-
Make sure you have started the JBoss EAP server with the PostgreSQL driver
-
Open a command prompt and navigate to the root directory of this quickstart.
-
Type this command to build and deploy the archive:
mvn clean install jboss-as:deploy
-
This will deploy
application-component-1/target/jboss-jts-application-component-1.war
andapplication-component-2/target/jboss-jts-application-component-2.jar
to the running instance of the server.
The application will be running at the following URL: http://localhost:8080/jboss-jts-application-component-1/.
When you enter a name and click to "Add" that customer, you will see the following in the application server 1 console:
INFO [org.jboss.ejb.client] (http-/127.0.0.1:8080-1) JBoss EJB Client version 1.0.26.Final-redhat-1
WARN [com.arjuna.ats.jts] (RequestProcessor-5) ARJUNA022261: ServerTopLevelAction detected that the transaction was inactive
You will also see the following in application-server-2 console:
INFO [org.jboss.ejb.client] (RequestProcessor-10) JBoss EJB Client version 1.0.26.Final-redhat-1
INFO [class org.jboss.as.quickstarts.cmt.jts.mdb.HelloWorldMDB] (Thread-2 (HornetQ-client-global-threads-2003471369)) Received Message: Created invoice for customer named: Tom
The web page will also change and show you the new list of customers.
Note: You will see the following warnings in the server log. You can ignore these warnings.
JBAS010489: -ds.xml file deployments are deprecated. Support may be removed in a future version.
ARJUNA022261: ServerTopLevelAction detected that the transaction was inactive
-
Make sure you have started the JBoss EAP server as described above.
-
Open a command prompt and navigate to the root directory of this quickstart.
-
When you are finished testing, type this command to undeploy the archive:
mvn package jboss-as:undeploy
You must remove the JTS server configuration you did during setup because it interferes with the JTA quickstarts.
You can modify the server configuration by running the remove-jts-transactions.cli
script provided in the root directory of this quickstart, by using the JBoss CLI interactively, or by manually editing the configuration file.
-
Start the JBoss EAP server with the full profile.
For Linux: EAP_HOME_1/bin/standalone.sh -c standalone-full.xml For Windows: EAP_HOME_1\bin\standalone.bat -c standalone-full.xml
-
Open a new command prompt, navigate to the root directory of this quickstart, and run the following command, replacing EAP_HOME with the path to your server:
For Linux: EAP_HOME_1/bin/jboss-cli.sh --connect --file=remove-jts-transactions.cli For Windows: EAP_HOME_1\bin\jboss-cli.bat --connect --file=remove-jts-transactions.cli
This script removes the JTS configuration from the jacorb
and transactions
subsystems in the server configuration. You should see the following result when you run the script:
The batch executed successfully.
{"outcome" => "success"}
-
Start the JBoss EAP server with the full profile.
For Linux: EAP_HOME_1/bin/standalone.sh -c standalone-full.xml For Windows: EAP_HOME_1\bin\standalone.bat -c standalone-full.xml
-
To start the JBoss CLI tool, open a new command prompt, navigate to the EAP_HOME directory, and type the following:
For Linux: EAP_HOME_1/bin/jboss-cli.sh --connect For Windows: EAP_HOME_1\bin\jboss-cli.bat --connect
-
At the prompt, type the following:
/subsystem=jacorb/:write-attribute(name=transactions,value=spec) /subsystem=jacorb/:undefine-attribute(name=name) /subsystem=transactions/:undefine-attribute(name=jts) /subsystem=transactions/:undefine-attribute(name=node-identifier) :reload
-
Stop the server.
-
If you backed up the EAP_HOME/standalone/configuration/standalone-full.xml,simply replace the edited configuration file with the backup copy.
-
If you did not make a backup copy, open the file EAP_HOME/standalone/configuration/standalone-full.xml and disable JTS as follows:
-
Find the orb subsystem and change the configuration back to:
<subsystem xmlns="urn:jboss:domain:jacorb:1.4"> <orb> <initializers security="on" transactions="spec"/> </orb> </subsystem>
-
Find the transaction subsystem and remove the
<jts/>
element:<subsystem xmlns="urn:jboss:domain:transactions:1.5"> <!-- REMOVE node-identifier ATTRIBUTE FROM core-environment ELEMENT --> <!-- LEAVE EXISTING CONFIG AND REMOVE THE </jts> --> </subsystem>
-