![Page 2: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/2.jpg)
.com
Dmytro Sokolov▷10 years Java Developer▷3 years Trainer & Mentor▷Java Trainer / Lead Software Engineer
▷Full-stack experience▷Project: BecomeJavaSenior.com▷email: [email protected]
Hello!
.com
![Page 4: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/4.jpg)
.com
Agenda▷The internal structure of Spring JTA;
▷Possible configuration with Spring AOP;
▷Transaction Concepts
▷Benefits of Spring JTA
▷Best Practices and pitfalls
![Page 7: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/7.jpg)
.com
Local Transaction model with JDBC
connection.setAutoCommit(false);
preparedStatement.executeUpdate();
connection.commit();
} catch (SQLException e) {
connection.rollback();
![Page 8: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/8.jpg)
.com
Programmatic Transaction model with JPA
UserTransaction utx =
entityManager.getTransaction();
try {
utx.begin();
businessLogic();
utx.commit();
} catch(Exception ex) {
utx.rollback();
throw ex;
![Page 14: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/14.jpg)
.com
Bean registrationWhat does
do ?
1.ExecuteAopAutoProxyConfigurer.configureAutoProxyCreator()
![Page 15: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/15.jpg)
.com
Bean registrationWhat does
do ?
2. Which createTransactionInterceptor
![Page 16: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/16.jpg)
.com
Bean registrationWhat does
do ?
3. Search declared TransactionManager and attached it to TransactionInterceptor
![Page 17: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/17.jpg)
.com
Bean registrationWhat does
do ?
4. Register TransactionInterceptor bean in Spring Context
![Page 18: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/18.jpg)
.com
@Transactional parsingHow @Transactional annotation is parsed during runtime by Spring?
![Page 19: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/19.jpg)
.com
@Transactional parsingHow @Transactional annotation is parsed during runtime by Spring?
Registers the SpringTransactionAnnotationParser as default parser for the @Transactional annotation
![Page 20: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/20.jpg)
.com
Transactional interceptor invocation
What does TransactionInterceptor do?
![Page 21: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/21.jpg)
.com
Transactional interceptor invocation
What does TransactionInterceptor do?
1.Retrieves the transaction attributes
txAttr = getTransactionAttributeSource().
getTransactionAttribute(invocation.getMetho
d(), targetClass);
![Page 22: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/22.jpg)
.com
Transactional interceptor invocation
What does TransactionInterceptor do?
2. Gets the transaction manager from the Spring context and transaction attributes
PlatformTransactionManager tm =
determineTransactionManager(txAttr);
![Page 23: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/23.jpg)
.com
Transactional interceptor invocation
What does TransactionInterceptor do?
4. A transaction is created by the underlying entity managerTransactionInfo txInfo =
createTransactionIfNecessary(tm, txAttr,
joinpointIdentification);
![Page 24: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/24.jpg)
.com
Transactional interceptor invocation
What does TransactionInterceptor do?
5. The target method is invoked retVal = invocation.proceed();
} catch (Throwable ex)
{ completeTransactionAfterThrowing(txInfo,
ex);
throw ex;
![Page 25: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/25.jpg)
.com
Transactional interceptor invocation
What does TransactionInterceptor do?
5. The transaction is committed commitTransactionAfterReturning(txInfo);
![Page 26: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/26.jpg)
.com
Transactional interceptor invocation
What does AbstractPlatformTransactionManager do?
![Page 27: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/27.jpg)
.com
TransactionManager invocation
What does AbstractPlatformTransactionManager do?
Delegates the creation and the start of the transaction itself to the underlying JpaTransactionManager.
![Page 29: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/29.jpg)
.com
TransactionManager invocation
What does JpaTransactionManager do?
1. Tries to look an existing entity manager using the entity manager factory
EntityManagerHolder emHolder =
TransactionSynchronizationManager.getResource
( getEntityManagerFactory());
![Page 30: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/30.jpg)
.com
TransactionManager invocation
What does JpaTransactionManager do?
2. Retrieves the dataSource declared for this transaction manager
ConnectionHolder conHolder =
TransactionSynchronizationManager.getResource
( getDataSource());
![Page 31: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/31.jpg)
.com
TransactionManager invocation
What does JpaTransactionManager do?
3. Delegates the creation of a new JDBC transaction to the underlying JPA Dialect
Object transactionData =
getJpaDialect().beginTransaction(...)
![Page 32: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/32.jpg)
.com
TransactionManager invocation
What does JpaTransactionManager do?
4. Registers the current JDBC connection to the TransactionSynchronizationManager ThreadLocal map
TransactionSynchronizationManager.bindResour
ce(getDataSource(), conHolder);
![Page 33: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/33.jpg)
.com
TransactionManager invocation
And finally execute:
beginTransaction(entityManager, definition);
prepareTransaction(entityManager, ...);
commitTransactionAfterReturning(...);
![Page 36: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/36.jpg)
.com
X/Open XA standardXA (eXtended Architecture) ishow one/multiple resources is integrating with a transaction manager
![Page 37: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/37.jpg)
.com
X/Open XA standardsupports both the 1-phase commit protocol and the 2-phase commit protocol.
![Page 38: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/38.jpg)
.com
Managing single or multiple resources
single resource - 1-phase commit multiple resources - 2-phase commit
![Page 39: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/39.jpg)
.com
X/Open XA standardResources that support the XA standard expose a special object, the XA switch Interface javax.transaction.xa.XAResource
![Page 41: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/41.jpg)
.com
Transaction contextAt a minimum, the transaction context contains a unique transaction identifier.
Depends on the relevant transaction manager implementation
![Page 42: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/42.jpg)
.com
Transaction managerThe features determine the quality of service of a transaction manager:▷Support for multiple resources
▷Distributed transactions▷Transaction monitoring▷Recovery from failure▷Support for suspend/resume and attach/detach
![Page 44: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/44.jpg)
.com
Transaction managers in Spring
+ JdoTransactionManagerJmsTransactionManager,WebLogicJtaTransactionManagerWebSphereUowTransactionManager
![Page 45: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/45.jpg)
.com
Transaction managers in Spring
PlatformTransactionManager Interface
getTransaction(TransactionDefinition definition)void commit(TransactionStatus status) void rollback(TransactionStatus status)
![Page 46: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/46.jpg)
.com
Transaction managers in Spring
TransactionDefinition interface
isolation level and the propagation policy
![Page 47: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/47.jpg)
.com
Transaction managers in Spring
TransactionStatus interface
check the status of the current transaction
![Page 48: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/48.jpg)
.com
Transaction managers in Spring
AbstractPlatformTransactionManager class
▷determines transaction;▷applies propagation behavior;▷suspends and resumes transactions;▷checks the rollback-only flag on commit;
![Page 49: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/49.jpg)
.com
Local transaction managers in Spring
can coordinate transactions over a single resource only
typically embedded in the resource itself
![Page 50: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/50.jpg)
.com
Local transaction managers in Spring JDBC
Spring Transaction ManagerJDBC resource
Oracle DB
built-in TransactionManager
BEGIN, COMMIT, ROLLBACKnative Oracle API
![Page 51: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/51.jpg)
.com
Local transaction managers in Spring JMS
Spring Transaction Manager(facade)Resource
instance of the JMS queues
topics
![Page 53: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/53.jpg)
.com
Global transaction managers in Spring
Global transaction manager is TP monitor(transaction processing monitor )
▷implements the 2-phase commit
▷coordinate multiple XA resources
![Page 54: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/54.jpg)
.com
Global transaction managers in Spring
JtaTransactionManagerOC4JJtaTransactionManagnerWebLogicJtaTransactionManagerWebSphereUowTransactionManager
![Page 57: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/57.jpg)
.com
Global transactions with JTA
To manage custom global JDBC and JMS transaction see opensource Atomikos or Bitronix
![Page 58: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/58.jpg)
.com
Distributed transaction managers in Spring
Distributed transaction managers does not supported from boxOutside the scope of the Spring framework.
![Page 60: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/60.jpg)
.com
Spring JTA benefitsWork with ▷JTA transactions▷JDBC ▷JPA▷Hibernate ▷JDO ▷JMS
![Page 64: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/64.jpg)
.com
Spring JTA benefitsCustomize transactional behavior
▷insert custom behavior in the case of transaction rollback
▷add arbitrary advice▷rollback the transaction automatically on an application exception
![Page 69: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/69.jpg)
.com
Different behavior for diff Tx Managers
@Transactional(readOnly = true, propagation=Propagation.REQUIRED)
public long insertTrade(TradeData trade) throws Exception { //JDBC code...}
![Page 70: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/70.jpg)
.com
Different behavior for diff Tx Managers
Not always the same:@Transactional(readOnly = true, propagation=Propagation.REQUIRED)
public long insertTrade(TradeData trade) throws Exception { em.persist(trade); return trade.getTradeId();}
![Page 71: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/71.jpg)
.com
Be aware of Propagation behavior
@Transactional(readOnly = true, propagation=Propagation.SUPPORT)public long insertTrade(TradeData trade) throws Exception { //JDBC code...}
Allows to write data
![Page 72: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/72.jpg)
.com
Be aware of Propagation behavior
@Transactional(readOnly = true)public TradeData getTrade(long tradeId) throws Exception { return em.find(TradeData.class, tradeId);}
Will use Propagation.REQUIREDDo you really need transaction for reading?
![Page 73: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/73.jpg)
.com
Be aware of Propagation behavior
How about
@Transactional(readOnly = true, propagation=Propagation.SUPPORTS)public TradeData getTrade(long tradeId) throws Exception { return em.find(TradeData.class, tradeId);}
![Page 74: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/74.jpg)
.com
Be aware of Propagation behavior
Or even no transaction public TradeData getTrade(long tradeId) throws Exception { return em.find(TradeData.class, tradeId);
![Page 75: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/75.jpg)
.com
Be aware of Propagation behavior
@Transactional(propagation=Propagation.REQUIRES_NEW)public long insertTrade(TradeData trade) throws Exception {...}
@Transactional(propagation=Propagation.REQUIRES_NEW)public void updateAcct(TradeData trade) throws Exception {...}
Do you really need always new Transaction?
![Page 76: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/76.jpg)
.com
Rollback behavior@Transactional(propagation=Propagation.REQUIRED)public TradeData placeTrade(TradeData trade) throws Exception { try { insertTrade(trade); updateAcct(trade); return trade; } catch (Exception up) { //log the error throw up; }Will transaction Rollback in case of throw Exception?
![Page 77: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/77.jpg)
.com
Rollback behavior@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)public TradeData placeTrade(TradeData trade) throws Exception { try { insertTrade(trade); updateAcct(trade); return trade; } catch (Exception up) { //log the error throw up; }}
![Page 78: [Java eeconf 2016] spring jta principles of work with transactions. Dmytro Sokolov](https://reader035.vdocuments.site/reader035/viewer/2022062412/587ce7d41a28ab564b8b48b3/html5/thumbnails/78.jpg)
.com
Transaction isolation@Transactional(propagation= Propagation.REQUIRED, Isolation = Isolation.REPEATABLE_READ)public long insertTrade(TradeData trade) throws Exception { //JDBC or JPA code...}
Will isolation always be changed?