SlideShare a Scribd company logo
1 of 21
Download to read offline
Distributed Transactions with Java
Anjana Fernando
Senior Technical Lead
WSO2 Inc.
Agenda
● What are Distributed Transactions
● Java Distributed Transaction Support
● Transaction Management Models
● XA Transaction Flow
● XA Transaction Optimizations
● XA Transaction Error Handling
● The XA Internals
● Demo
● Recommendations
What is a Distributed Transaction?
Why do we need it?
How Java Supports Distributed
Transactions
● Java Transactions API (JTA) implements the X/Open XA
specification
● XA - A system interface which defines how a distributed
transaction should be done
● Mainly used with database drivers and message queues, e.g.
MySQL, Oracle, ActiveMQ etc..
● As per the XA specification, the transaction context is retained in
the current thread context, i.e. all operations of resource
managers must be done in the same thread where the global
transaction started
Two Phase Commit (2PC) Protocol
JTA Transaction Manager
● The third party that manages the global transaction
● Resource managers enlist their resources with the transaction
manager
● Transaction manager tracks the resource managers and executes
the two phase commit protocol on behalf of the application
● Popular implementations:-
Bitronix
JBoss JTS
Transaction Management Models
● Programmatic Transaction Model (Bean Managed Transactions)
○ Full control of the transaction operations
○ Application has to handle the transaction
start/commit/rollback, and exception handling semantics
○ More error prone, can be hard to manage
● Declarative Transaction Model (Container Managed Transactions)
○ The application server does the transaction handling
○ The application declares/defines how transactions should
behave
○ Less error prone, less boilerplate code, often suitable for
most of the data operations
○ Supported by Spring Framework, JavaEE
What do I need to be XA compliant
● Databases (JDBC): A JDBC driver which exposes an XADataSource
implementation
● Messaging (JMS): An XAConnectionFactory for creating XA aware
JMS connections
● In container managed transactions, like in Spring, the
aforementioned entities must be carefully configured in creating
the data sources / connection factories, with the use of a JTA
transaction manager, or else, undesired behavior such as some
operations not rollbacking can happen in failure scenarios
Flow of a Distributed Transaction
try {
txManager.begin();
msg = XASession -> removeMessage
conn1 = XADataSource -> getConnection
doDataOperation1(msg, conn1);
conn2 = XADataSource -> getConnection
doDataOperation2(msg, conn2);
txManager.commit();
} catch (SomeException e) {
txManager.rollback();
}
Flow of a Distributed Transaction
● A reference to “txManager” mentioned earlier must be of type
javax.transaction.UserTransaction or javax.transaction.TransactionManager
● UserTransaction interface is the application level API exposed to application, while
TransactionManager interface is meant to be used by application servers
● Typically, inside an application server, the UserTransaction and TransactionManager objects can
be looked up by doing a JNDI lookup to a specific resource name. This JNDI resource name is
dependent on the application server vendor, e.g. “java:comp/UserTransaction”. This approach is
mostly used in the programmatic transaction model
● setRollbackOnly method is used to signal the active transaction that, the only outcome of the
global transaction is a rollback, regardless of all the operations that happened/happens
○ A similar approach is used in CMT also, in signalling the application server, that the
ultimate global transaction should rollback, or else, it could be configured for this to
happen is a certain type of an exception is thrown in the middle of a transaction
Enlisting XA Resources with the
Transaction Manager
● The XAResources created by each resource manager, must enlist them in the current thread's
transaction context of the transaction manager
● This can be done manually by programmatically enlisting the XAResource objects by looking up
the transaction manager and getting the current thread context
● The manual operation can be error prone, where the user should properly at the right time
should enlist and delist XA resources
● Most of the application servers automatically wraps the RDBMS data sources and JMS
connection factories so it automatically enlist their XAResources with the application server’s
configured transaction manager
○ In other environments, such as when using Spring framework, connection pooling libraries
like DBCP can be used to provide the transaction manager object and wrap an existing
XADataSource datasource to automatically do the enlistment
XA Transaction Optimizations
● 1PC Optimization
○ In a distributed transaction, if there is only one resource, the
transaction manager can do a more efficient direct commit,
rather than doing 2PC
● Last Resource Commit Optimization (Last Resource Gambit)
○ Allows you to add a non-2PC support resource to a global
transaction
○ Works by committing the one phase aware transaction at the
end of all prepare operations, and if the commit is successful,
the second phase is executed for all the others
XA Transaction Error Handling
● The first phase is executed on all the resource managers to get their promise that, it can
properly commit the transaction later for sure, when the transaction manager tells it
● The transaction manager is suppose to persist the state of the global transaction in the case of a
failure of the client application
○ This would mean, information about the XA resources must be remembered by the
transaction manager, either by making sure the XAResource objects are serializable or else,
have other means of re-creating those objects, e.g. JBoss’s XAResourceRecovery interface
● In the case of the client application crash with the transaction manager, or else, a specific
resource manager fails, after the XA resources objects are recovered, the recover() method on
those are called to retrieve the Xids of those transaction branches in order to continue the 2PC
commit operations
Transaction Manager keeping it promises -> recovery after first phase
XA Transaction Error Handling
● HeuristicRollback
○ This exception is thrown by the transaction manager if all the resources participating in the
global transaction made an heuristic decision to rollback, before the transaction manager
could execute the second phase of 2PC. This most probably happens if the resource
manager times out for his second phase execution by the transaction manager
● HeuristicCommit
○ This is the opposite of HeuristicRollback, where all have committed before the transaction
manager executes his second phase decision
● HeuristicMixed
○ This exception is thrown, if some of the resources have made heuristic decisions to commit
and others to rollback. This is a potential problematic scenario, where now the data most
probably is in an inconsistent state, where earlier two scenarios, the data would always be
consistent regardless it succeeded or not
○ This situation requires manual fixing of transactions by analyzing the Xids of each
transaction branch, and fixing them in target resource managers in question, e.g. database
servers (resource managers would keep a log of Xids when an Heuristic exception occurs)
When things go wrong -> Heuristic Exceptions!
How does he do it? Playing Transaction
Manager
● The first job of the transaction manager is to create Xids (javax.transaction.xa.Xid) for each
transaction branch
○ An Xid mainly contains the following information
■ gtrid - an identifier for the global transaction
■ bqual - an identifier for this specific transaction branch
● Extract XAResource objects for each resource of the resource managers, e.g. database
connections, JMS sessions
● Call XAResource#start() by parsing in the Xid created to represent that transaction branch
● Execute operations for resource that was started, e.g. executing SQL statements against a
database connection, removing a message from a message queue
● Call XAResource#end() by parsing in the same Xid which was used for starting it
● Call XAResource#prepare() by parsing in each ones respective Xids to execute the first phase of
2PC
● If everyone returns XAResource.XA_OK in prepare(), call XAResource#commit() for all the
participating XAResources in the global transaction
So… Should I Always Play Transaction
Manager?
● No!
● There are lot of behind the scenes complex operations done by the
transaction manager to handle all the operations such as persisting
transaction status and recovery
● It doesn’t make sense for an user application to handle this complex
logic
● Don’t try to solve a problem that is already solved by someone else
(and has most probably done a good job at it!)
Demo
Recommendations in Using XA
Transactions
● If you can avoid it, do avoid it! :)
● XA transactions are inherently expensive operations, because of the extra operations required
for the coordination of a global transaction. So for performance critical applications, this can be
a bottleneck
● Workarounds may be added in place of a distributed transaction, such as compensation actions
in the case of a inconsistent state at the end of a set of operations. This maybe considered if the
operations are not very sensitive to being in an inconsistent state for a short period
● The decision will depend on a balance between performance and how critical the
data/operations should be consistent at all times
● If things do go wrong in XA, it can actually go wrong really badly! .. as we have seen with
Heuristic exception scenario, it may be somewhat hard to recover from that, and also, recover
from physical failures may be impossible, e.g. the instance that has the transaction manager
binary logs are lost
Questions?
Thank You!

More Related Content

Similar to Java Distributed Transactions

Spring Transaction Management
Spring Transaction ManagementSpring Transaction Management
Spring Transaction ManagementYe Win
 
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...Dmytro Sokolov
 
Atomic Service Transactions
Atomic Service Transactions Atomic Service Transactions
Atomic Service Transactions WSO2
 
Trafodion Distributed Transaction Management
Trafodion Distributed Transaction ManagementTrafodion Distributed Transaction Management
Trafodion Distributed Transaction ManagementRohit Jain
 
SAPHana_basic12345678234567890865456.pdf
SAPHana_basic12345678234567890865456.pdfSAPHana_basic12345678234567890865456.pdf
SAPHana_basic12345678234567890865456.pdfSravanthiVaka1
 
BEA_eworld_2001_jta.ppt
BEA_eworld_2001_jta.pptBEA_eworld_2001_jta.ppt
BEA_eworld_2001_jta.pptssuser670564
 
LeanXcale Presentation - Waterloo University
LeanXcale Presentation - Waterloo UniversityLeanXcale Presentation - Waterloo University
LeanXcale Presentation - Waterloo UniversityRicardo Jimenez-Peris
 
Narayana 5: The premier open source transaction manager
Narayana 5: The premier open source transaction manager Narayana 5: The premier open source transaction manager
Narayana 5: The premier open source transaction manager Virtual JBoss User Group
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - TransactionsDmitry Buzdin
 
Project description2012
Project description2012Project description2012
Project description2012ashish61_scs
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transactionpatinijava
 
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Gyanmanjari Institute Of Technology
 
deadlock prevention
deadlock preventiondeadlock prevention
deadlock preventionNilu Desai
 
24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMS24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMSkoolkampus
 
Saga about distributed business transactions in microservices world
Saga about distributed business transactions in microservices worldSaga about distributed business transactions in microservices world
Saga about distributed business transactions in microservices worldMikalai Alimenkou
 

Similar to Java Distributed Transactions (20)

Spring Transaction Management
Spring Transaction ManagementSpring Transaction Management
Spring Transaction Management
 
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
 
Managing transactions 11g release 1 (10.3
Managing transactions   11g release 1 (10.3Managing transactions   11g release 1 (10.3
Managing transactions 11g release 1 (10.3
 
Atomic Service Transactions
Atomic Service Transactions Atomic Service Transactions
Atomic Service Transactions
 
Trafodion Distributed Transaction Management
Trafodion Distributed Transaction ManagementTrafodion Distributed Transaction Management
Trafodion Distributed Transaction Management
 
SAPHana_basic12345678234567890865456.pdf
SAPHana_basic12345678234567890865456.pdfSAPHana_basic12345678234567890865456.pdf
SAPHana_basic12345678234567890865456.pdf
 
Transactions
TransactionsTransactions
Transactions
 
Autonomous transaction
Autonomous transactionAutonomous transaction
Autonomous transaction
 
BEA_eworld_2001_jta.ppt
BEA_eworld_2001_jta.pptBEA_eworld_2001_jta.ppt
BEA_eworld_2001_jta.ppt
 
LeanXcale Presentation - Waterloo University
LeanXcale Presentation - Waterloo UniversityLeanXcale Presentation - Waterloo University
LeanXcale Presentation - Waterloo University
 
Transaction design patterns
Transaction design patternsTransaction design patterns
Transaction design patterns
 
Narayana 5: The premier open source transaction manager
Narayana 5: The premier open source transaction manager Narayana 5: The premier open source transaction manager
Narayana 5: The premier open source transaction manager
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
 
Project description2012
Project description2012Project description2012
Project description2012
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
 
Weblogic - overview of application server
Weblogic - overview of application serverWeblogic - overview of application server
Weblogic - overview of application server
 
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
 
deadlock prevention
deadlock preventiondeadlock prevention
deadlock prevention
 
24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMS24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMS
 
Saga about distributed business transactions in microservices world
Saga about distributed business transactions in microservices worldSaga about distributed business transactions in microservices world
Saga about distributed business transactions in microservices world
 

More from Anjana Fernando

Ballerina – An Open-Source, Cloud-Native Programming Language for Microservices
Ballerina – An Open-Source, Cloud-Native Programming Language for MicroservicesBallerina – An Open-Source, Cloud-Native Programming Language for Microservices
Ballerina – An Open-Source, Cloud-Native Programming Language for MicroservicesAnjana Fernando
 
Automatic Microservices Observability with Ballerina - GIDS 2021
Automatic Microservices Observability with Ballerina - GIDS 2021Automatic Microservices Observability with Ballerina - GIDS 2021
Automatic Microservices Observability with Ballerina - GIDS 2021Anjana Fernando
 
Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021
Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021
Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021Anjana Fernando
 
Monitoring Your Business with WSO2 BAM
Monitoring Your Business with WSO2 BAMMonitoring Your Business with WSO2 BAM
Monitoring Your Business with WSO2 BAMAnjana Fernando
 
Data Services: Getting Your Data Into APIs
Data Services: Getting Your Data Into APIsData Services: Getting Your Data Into APIs
Data Services: Getting Your Data Into APIsAnjana Fernando
 
Scalable Log Analysis with WSO2 BAM
Scalable Log Analysis with WSO2 BAMScalable Log Analysis with WSO2 BAM
Scalable Log Analysis with WSO2 BAMAnjana Fernando
 
Data integration and Business Processes
Data integration and Business ProcessesData integration and Business Processes
Data integration and Business ProcessesAnjana Fernando
 
Simultaneous analysis of massive data streams in real time and batch
Simultaneous analysis of massive data streams in real time and batchSimultaneous analysis of massive data streams in real time and batch
Simultaneous analysis of massive data streams in real time and batchAnjana Fernando
 
Ballerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOpsBallerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOpsAnjana Fernando
 
Ballerina - Cloud Native Programming Language
Ballerina - Cloud Native Programming LanguageBallerina - Cloud Native Programming Language
Ballerina - Cloud Native Programming LanguageAnjana Fernando
 
Ballerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOpsBallerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOpsAnjana Fernando
 
Effective microservices development with ballerina
Effective microservices development with ballerinaEffective microservices development with ballerina
Effective microservices development with ballerinaAnjana Fernando
 

More from Anjana Fernando (13)

Ballerina – An Open-Source, Cloud-Native Programming Language for Microservices
Ballerina – An Open-Source, Cloud-Native Programming Language for MicroservicesBallerina – An Open-Source, Cloud-Native Programming Language for Microservices
Ballerina – An Open-Source, Cloud-Native Programming Language for Microservices
 
Automatic Microservices Observability with Ballerina - GIDS 2021
Automatic Microservices Observability with Ballerina - GIDS 2021Automatic Microservices Observability with Ballerina - GIDS 2021
Automatic Microservices Observability with Ballerina - GIDS 2021
 
Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021
Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021
Ballerina: An Open-Source, Cloud-Native Programming Language - GIDS 2021
 
IoT Analytics
IoT AnalyticsIoT Analytics
IoT Analytics
 
Monitoring Your Business with WSO2 BAM
Monitoring Your Business with WSO2 BAMMonitoring Your Business with WSO2 BAM
Monitoring Your Business with WSO2 BAM
 
Data Services: Getting Your Data Into APIs
Data Services: Getting Your Data Into APIsData Services: Getting Your Data Into APIs
Data Services: Getting Your Data Into APIs
 
Scalable Log Analysis with WSO2 BAM
Scalable Log Analysis with WSO2 BAMScalable Log Analysis with WSO2 BAM
Scalable Log Analysis with WSO2 BAM
 
Data integration and Business Processes
Data integration and Business ProcessesData integration and Business Processes
Data integration and Business Processes
 
Simultaneous analysis of massive data streams in real time and batch
Simultaneous analysis of massive data streams in real time and batchSimultaneous analysis of massive data streams in real time and batch
Simultaneous analysis of massive data streams in real time and batch
 
Ballerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOpsBallerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOps
 
Ballerina - Cloud Native Programming Language
Ballerina - Cloud Native Programming LanguageBallerina - Cloud Native Programming Language
Ballerina - Cloud Native Programming Language
 
Ballerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOpsBallerina - A Programming Language for Cloud and DevOps
Ballerina - A Programming Language for Cloud and DevOps
 
Effective microservices development with ballerina
Effective microservices development with ballerinaEffective microservices development with ballerina
Effective microservices development with ballerina
 

Recently uploaded

Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Recently uploaded (20)

Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

Java Distributed Transactions

  • 1. Distributed Transactions with Java Anjana Fernando Senior Technical Lead WSO2 Inc.
  • 2. Agenda ● What are Distributed Transactions ● Java Distributed Transaction Support ● Transaction Management Models ● XA Transaction Flow ● XA Transaction Optimizations ● XA Transaction Error Handling ● The XA Internals ● Demo ● Recommendations
  • 3. What is a Distributed Transaction?
  • 4. Why do we need it?
  • 5. How Java Supports Distributed Transactions ● Java Transactions API (JTA) implements the X/Open XA specification ● XA - A system interface which defines how a distributed transaction should be done ● Mainly used with database drivers and message queues, e.g. MySQL, Oracle, ActiveMQ etc.. ● As per the XA specification, the transaction context is retained in the current thread context, i.e. all operations of resource managers must be done in the same thread where the global transaction started
  • 6. Two Phase Commit (2PC) Protocol
  • 7. JTA Transaction Manager ● The third party that manages the global transaction ● Resource managers enlist their resources with the transaction manager ● Transaction manager tracks the resource managers and executes the two phase commit protocol on behalf of the application ● Popular implementations:- Bitronix JBoss JTS
  • 8. Transaction Management Models ● Programmatic Transaction Model (Bean Managed Transactions) ○ Full control of the transaction operations ○ Application has to handle the transaction start/commit/rollback, and exception handling semantics ○ More error prone, can be hard to manage ● Declarative Transaction Model (Container Managed Transactions) ○ The application server does the transaction handling ○ The application declares/defines how transactions should behave ○ Less error prone, less boilerplate code, often suitable for most of the data operations ○ Supported by Spring Framework, JavaEE
  • 9. What do I need to be XA compliant ● Databases (JDBC): A JDBC driver which exposes an XADataSource implementation ● Messaging (JMS): An XAConnectionFactory for creating XA aware JMS connections ● In container managed transactions, like in Spring, the aforementioned entities must be carefully configured in creating the data sources / connection factories, with the use of a JTA transaction manager, or else, undesired behavior such as some operations not rollbacking can happen in failure scenarios
  • 10. Flow of a Distributed Transaction try { txManager.begin(); msg = XASession -> removeMessage conn1 = XADataSource -> getConnection doDataOperation1(msg, conn1); conn2 = XADataSource -> getConnection doDataOperation2(msg, conn2); txManager.commit(); } catch (SomeException e) { txManager.rollback(); }
  • 11. Flow of a Distributed Transaction ● A reference to “txManager” mentioned earlier must be of type javax.transaction.UserTransaction or javax.transaction.TransactionManager ● UserTransaction interface is the application level API exposed to application, while TransactionManager interface is meant to be used by application servers ● Typically, inside an application server, the UserTransaction and TransactionManager objects can be looked up by doing a JNDI lookup to a specific resource name. This JNDI resource name is dependent on the application server vendor, e.g. “java:comp/UserTransaction”. This approach is mostly used in the programmatic transaction model ● setRollbackOnly method is used to signal the active transaction that, the only outcome of the global transaction is a rollback, regardless of all the operations that happened/happens ○ A similar approach is used in CMT also, in signalling the application server, that the ultimate global transaction should rollback, or else, it could be configured for this to happen is a certain type of an exception is thrown in the middle of a transaction
  • 12. Enlisting XA Resources with the Transaction Manager ● The XAResources created by each resource manager, must enlist them in the current thread's transaction context of the transaction manager ● This can be done manually by programmatically enlisting the XAResource objects by looking up the transaction manager and getting the current thread context ● The manual operation can be error prone, where the user should properly at the right time should enlist and delist XA resources ● Most of the application servers automatically wraps the RDBMS data sources and JMS connection factories so it automatically enlist their XAResources with the application server’s configured transaction manager ○ In other environments, such as when using Spring framework, connection pooling libraries like DBCP can be used to provide the transaction manager object and wrap an existing XADataSource datasource to automatically do the enlistment
  • 13. XA Transaction Optimizations ● 1PC Optimization ○ In a distributed transaction, if there is only one resource, the transaction manager can do a more efficient direct commit, rather than doing 2PC ● Last Resource Commit Optimization (Last Resource Gambit) ○ Allows you to add a non-2PC support resource to a global transaction ○ Works by committing the one phase aware transaction at the end of all prepare operations, and if the commit is successful, the second phase is executed for all the others
  • 14. XA Transaction Error Handling ● The first phase is executed on all the resource managers to get their promise that, it can properly commit the transaction later for sure, when the transaction manager tells it ● The transaction manager is suppose to persist the state of the global transaction in the case of a failure of the client application ○ This would mean, information about the XA resources must be remembered by the transaction manager, either by making sure the XAResource objects are serializable or else, have other means of re-creating those objects, e.g. JBoss’s XAResourceRecovery interface ● In the case of the client application crash with the transaction manager, or else, a specific resource manager fails, after the XA resources objects are recovered, the recover() method on those are called to retrieve the Xids of those transaction branches in order to continue the 2PC commit operations Transaction Manager keeping it promises -> recovery after first phase
  • 15. XA Transaction Error Handling ● HeuristicRollback ○ This exception is thrown by the transaction manager if all the resources participating in the global transaction made an heuristic decision to rollback, before the transaction manager could execute the second phase of 2PC. This most probably happens if the resource manager times out for his second phase execution by the transaction manager ● HeuristicCommit ○ This is the opposite of HeuristicRollback, where all have committed before the transaction manager executes his second phase decision ● HeuristicMixed ○ This exception is thrown, if some of the resources have made heuristic decisions to commit and others to rollback. This is a potential problematic scenario, where now the data most probably is in an inconsistent state, where earlier two scenarios, the data would always be consistent regardless it succeeded or not ○ This situation requires manual fixing of transactions by analyzing the Xids of each transaction branch, and fixing them in target resource managers in question, e.g. database servers (resource managers would keep a log of Xids when an Heuristic exception occurs) When things go wrong -> Heuristic Exceptions!
  • 16. How does he do it? Playing Transaction Manager ● The first job of the transaction manager is to create Xids (javax.transaction.xa.Xid) for each transaction branch ○ An Xid mainly contains the following information ■ gtrid - an identifier for the global transaction ■ bqual - an identifier for this specific transaction branch ● Extract XAResource objects for each resource of the resource managers, e.g. database connections, JMS sessions ● Call XAResource#start() by parsing in the Xid created to represent that transaction branch ● Execute operations for resource that was started, e.g. executing SQL statements against a database connection, removing a message from a message queue ● Call XAResource#end() by parsing in the same Xid which was used for starting it ● Call XAResource#prepare() by parsing in each ones respective Xids to execute the first phase of 2PC ● If everyone returns XAResource.XA_OK in prepare(), call XAResource#commit() for all the participating XAResources in the global transaction
  • 17. So… Should I Always Play Transaction Manager? ● No! ● There are lot of behind the scenes complex operations done by the transaction manager to handle all the operations such as persisting transaction status and recovery ● It doesn’t make sense for an user application to handle this complex logic ● Don’t try to solve a problem that is already solved by someone else (and has most probably done a good job at it!)
  • 18. Demo
  • 19. Recommendations in Using XA Transactions ● If you can avoid it, do avoid it! :) ● XA transactions are inherently expensive operations, because of the extra operations required for the coordination of a global transaction. So for performance critical applications, this can be a bottleneck ● Workarounds may be added in place of a distributed transaction, such as compensation actions in the case of a inconsistent state at the end of a set of operations. This maybe considered if the operations are not very sensitive to being in an inconsistent state for a short period ● The decision will depend on a balance between performance and how critical the data/operations should be consistent at all times ● If things do go wrong in XA, it can actually go wrong really badly! .. as we have seen with Heuristic exception scenario, it may be somewhat hard to recover from that, and also, recover from physical failures may be impossible, e.g. the instance that has the transaction manager binary logs are lost