Distributed transactions

System integration with jboss

 

Vaclav Chalupa | Quality Engineer at RedHat

What is a transaction?

 

In computer programming, a transaction means a sequence of information exchange (database updates) where data integrity is ensured. 

ACID

ATOMICITY

All, or Nothing

CONSISTENCY

From one valid state to another valid state

ISOLATION

Transactions do not affect each other
Isolation levels (relaxation):

READ_UNCOMMITED < READ_COMMITED < REPEATEBLE_READ <SERIALIZABLE

DURABILITY

Stored permanently

DISTRIBUTED TRANSACTION

 

A transaction in which two and more transactional resources are involved


DT, as any other transactions, must have all four ACID properties
Transactional resources?
  • Databases
  • JMS
  • EIS (Enterprise Integration Systems supporting transactions)

DT Example

How IS ACID achieved?

Transaction manager

Transaction coordinator

Transaction manager is component that is responsible for coordinating of distributed transactions. 

 

Transaction manager uses two-phase commit protocol to perform atomic distributed transactions.

2pc

two-phase commit protocol

An algorithm ensuring correct completion of a distributed transaction = Atomicity

 

It coordinates all participated transactional resources within distributed transaction on whether to commit or rollback (abort).

2PC | Phase I

Voting Phase

Answer NO or no answer causes rollback of DT. 

 

If the transaction will commit, the transaction coordinator records the decision on stable storage, and the protocol enters phase II

 

If the transaction will abort all participant are informed about this decision too.

2PC | Phase II

commit phase

Transaction log

Transaction log is used for transaction recovery in case of failure.
If a distributed transaction will commit, transaction coordinator writes the decision to log.
Each participant must have own transaction log to write commit decision.

Failure recovery

Coordinator failure | Network / Participant failure

2pc optimizations

One-phase

If there is only one involved transactional resouce, transaction manager can use simply commit

last resouce gambit

It is possible to enlist one resource which is not two-phase commit aware.

X/OPEN XA

"eXtented Architecture"

The Open Group standard for Distributed Transaction Processing (DTP)

= standard for distributed transactions

The standard describes how the transaction manager must behave to coordinate distributed transaction and what resource managers of transactional resources must do to support transactional access.

XA Transaction = Distributed Transaction

XA Resource = Transactional Resource (Participant)

JTA

java transaction API

JTA is java implementation of X/OPEN XA specification

JTA specifies standard Java interfaces between a transaction manager and the parties involved in a distributed transaction system: the resource manager, the application server, and the transactional applications.

 

  • High-level application interface for transaction boundaries demarcation
  • High-level transaction manager interface used by container to control transactions

JTA is a specification developed under the Java Community Process as JSR 907. 

 

JTS

Java transaction service

JTS is implementation of another specification: Object Transaction Service (OTS) specified by OMG
JTS specifies an implementation of a transaction manager that support JTA specification = JTS can be used by JTA
JTS uses the standard CORBA ORB/TS interfaces and Internet Inter-ORB Protocol (IIOP) for transaction context propagation between JTS transaction managers.
JTA is high-level API

JTS is low-level API

JTA Interfaces

javax.transaction.UserTransaction
javax.transaction.TransactionManager
javax.transaction.Transaction
javax.transaction.Synchronization
javax.transaction.xa.XAResource
javax.transaction.xa.Xid

User transaction

javax.transaction.usertransaction

Used by JEE applications to programmatically demarcate transactions

 

Obtained via JNDI (java:jboss/UserTransaction) or using @Inject annotation (JEE)

Operations:

 

begin() creates a new transaction and associate it with the current thread

commit() completes the transaction associated with current thread

rollback() rollback the transaction associated with current thread

setRollbackOnly() only possible outcome of the transaction of current thread is rollback

getStatus() obtain the status of the transaction associated with current thread

Transaction manager

javax.transaction.transactionmanager

Used by Java container

 

Obtained via JNDI (java:jboss/TransactionManager)

Operations:

 

begin() creates a new transaction and associate it with the current thread

commit() completes the transaction associated with current thread

rollback() rollback the transaction associated with current thread

setRollbackOnly() only possible outcome of the transaction of current thread is rollback

int getStatus() obtain the status of the transaction associated with current thread

Transaction getTransaction() gets the transaction of the calling thread

Transaction suspend() suspend transaction of the calling thread

resume(Transaction t) resumes the transaction context of the calling thread with t

Transaction

javax.transaction.transaction

Represents transaction started by transaction manager

Controls transaction outcome: commit, rollback, setRollbackOnly

Enlists/delists resources to transaction

Registers synchornizations with transaction

Operations:

 

commit() completes the transaction associated with current thread

rollback() rollback the transaction associated with current thread

setRollbackOnly() only possible outcome of the transaction of current thread is rollback

enlistResource(XAResouce xar) enlist the specified resource to transaction

delistResource(XAResouce xar, int flag) delist the specified resource from transaction

registerSynchronization(Synchronization sync) registers synchronization

synchronization

javax.transaction.synchronization

Callback interface to inform about transaction completion

Typically used by cache to update/invalidate its contents


Best-effort only, not guaranteed to be called in case of crash

Operations:

 

beforeCompletion()

afterCompletion()

xa resource

javax.transaction.xa.xaresource

Represents any object that supports one or two phase protocol to participate in transaction and can ensure ACID properties
  • Database connection
  • JMS connection
Operations (called exclusively by TransactionManager):

 

start(Xid xid, int flag) starts the work on behalf of transaction branch specified in xid 

end(Xid xid, int flag) ends the work performed on behalf of transaction branch

prepare(Xid xid) asks the resource manager to prepare

commit(Xid xid, boolean onePhase) commit

rollback(Xid xid) rollback

Xid[] recover(int flag) obtains a list of prepared transaction branches from RM

forget(Xid xid) tells the resource manager to forget about a heuristically completed transaction branch

XID

javax.transaction.xa.xid

Unique identifier of transaction


Industry standard

  • Fully portable across different Transaction Managers, allows creation of delegated transactions in case of hierarchical Transaction Managers

 

Consists of three components

  • Format identifier – must be unique across the transaction systems
  • Global transaction identifier
  • Branch qualifier

container transaction management

EJB

Transaction demarcation beans in JEE application

 

Container managed transaction: TransactionAttribute (REQUIRED, REQUIRES_NEW, ... )

Bean managed transactions: @Inject UserTransaction

 

Message Driven Bean

Transactional services

in jee

transaction flow

Jboss fuse guides

 

 

 

XA Transaction declaration in JBoss Fuse

https://github.com/FuseByExample/esb-transactions

 

 

 

Transaction guide for JBoss Fuse

https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.2/html/Transaction_Guide/index.html

 

 

Transaction and web services

Common atomic transactions are short-running

= short atomic operations

For Web Services, we need long-running transaction which are propagated between communicating Web Service.

BEA, IBM, and Microsoft developed WS-Coordinator and WS-Tx specs.

WS-Tx was split to WS-Atomic Transaction ans WS-BusinessActivity.

WS-Coordinator

There is a need for a coordination infrastructure in a Web Service environmen.

 

WS-Coordinator defines a framework that allows different coordination participant to be plugged-in to coordinate work between clients, services and participans.

 

Consists of three main modules:

  • Activation Service    Installation of a new coordinator
  • Registration Service    Registration of participant with the coordinator
  • Context    Contains information necessary to perform coordination, context is propagated between participants

ws-aTOMIC tRANSACTION

This specification:

  • extends the coordinator context to create transaction context
  • augments Activation and Registration services to support atomic transaction (emulates ACID)

 

This ensure automatic activation, registration, propagation and atomic termination of Web services.

ws-ATOMIC TRANSACTION

COORDINATING PROTOCOLS

 

There are three coordinating protocols defined to ensure atomicity.

 

Each client / participant is registered to some of the protocols:

  • completion initiates commitment processing, following 2PC protocols are then used to ensure atomicity
  • volatile two-phase commit - commit notification received from completion protocol, new participants can register until durable 2PC participant is requested to prepare
  • durable two-phase commit - after successful completing of the prepare phase for Volatile 2PC participant

THANK YOU!