SlideShare une entreprise Scribd logo
1  sur  45
1
Transaction Design patterns
Helmi Ben Abdallah @rchitect J2EE
2
A Brief Introduction to the Patterns
• The transaction design patterns that are described in the next are :
• Client Owner Transaction Design Pattern
• Domain Service Owner Transaction Design Pattern
• Server Delegate Owner Transaction Design Pattern
3
CLIENT OWNER TRANSACTION DESIGN PATTERN
4
Context
• Although undesirable, in certain situations it is necessary to force
transaction management up to the client layer within an enterprise
Java application.
• Consider the all to common situation where a single request is made
from the client, but the client requires multiple (remote) calls to the
server to fulfill that request.
• This situation occurs most often when domain services are too fine-
grained and no aggregate services exist. When this type of situation
occurs, we are required to push transaction management up to the
presentation layer to maintain ACID properties.
5
public void placeFixedIncomeTrade(TradeData trade)
throws Exception {
try {
...
Placement placement =
placementService.placeTrade(trade);
executionService.executeTrade(placement);
} catch (Exception e) {
log.fatal(e);
throw e;
}
6
Forces
• The client requires multiple remote or local calls to do
main service objects to fulfill a single business request.
• ACID properties must be maintained, meaning that
transaction processing is required to maintain data integrity.
• Domain service objects are fine-grained and no aggregate
services exist to combine business requests.
• It is not possible to re-architect or refactor the application
to provide a single domain service method invocation for
a single client request for all client requests.
7
Solution
8
Consequences
• The client object managing the transaction must use programmatic transactions if
using EJB, or can use declarative transactions if the client object is managed by
Spring.
• Server-based domain objects must use declarative transactions since a
programmatic transaction cannot be propagated to other programmatic transaction-
managed beans.
• To maintain ACID properties server-based objects never start or commit a
transaction. Also, server-based objects never mark a transaction for rollback.
• No transaction logic is necessary in the client or the server for query-related
methods. However, server-based domain objects must have a transaction attribute
setting of Supports for query methods.
• Persistence objects used by the Domain Service components, regardless of the
persistence framework, must not contain any transaction or rollback logic.
9
Implementationpublic class ClientModel
{
public void updateTradeOrder(TradeData trade)
throws Exception {
InitialContext ctx = new InitialContext();
UserTransaction txn = (UserTransaction)
ctx.lookup("java:comp/UserTransaction");
try {
txn.begin();
TradingService tradingService =
ServiceLocator.getTradingService();
tradingService.updateTradeOrder(trade);
txn.commit();
} catch (TradeUpdateException e) {
txn.rollback();
EJB
10
@Stateless
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class TradingServiceImpl implements TradingService
{
public void updateTradeOrder(TradeData trade)
throws TradeUpdateException {
validateUpdate();
TradeOrderDAO dao = new TradeOrderDAO();
dao.update(trade);}}
the client is completely responsible for managing the transaction, the Domain Service
component must never start a transaction, which is why it is assigned the transaction
attribute of Mandatory.
11
<!-- Define the Client Delegate Bean -->
<bean id="clientModelTarget"
class="com.trading.client.ClientModel">
<property name="tradingService" ref="tradingService"/>
</bean>
<bean id="clientModel"
class="org.springframework.transaction.interceptor.
TransactionProxyFactoryBean">
<property name="transactionManager" ref="txnMgr"/>
<property name="target" ref="clientModelTarget"/>
<property name="transactionAttributes">
<props>
<prop key="*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="get*">PROPAGATION_SUPPORTS</prop> </props> </property> </bean>
spring
12
<!-- Define the Domain Service Bean -->
<bean id="tradingServiceTarget"
class="com.trading.server.TradingServiceImpl">
</bean>
<bean id="tradingService"
class="org.springframework.transaction.interceptor.
TransactionProxyFactoryBean">
<property name="transactionManager" ref="txnMgr"/>
<property name="target" ref="tradingServiceTarget"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_MANDATORY</prop>
<prop key="get*">PROPAGATION_SUPPORTS</prop>
13
public class ClientModel
{
public void updateTradeOrder(TradeData trade)
throws Exception {
tradingService.updateTradeOrder(trade);
}
public TradeData getTradeOrder(TradeKey key)
throws Exception {
return tradingService.getTrade(key);
}
}
public class TradingServiceImpl implements
TradingService
{
public void updateTradeOrder(TradeData trade)
throws TradeUpdateException {
validateUpdate();
TradeOrderDAO dao = new TradeOrderDAO();
dao.update(trade);
}
public TradeData getTrade(TradeKey key)
throws Exception {
TradeOrderDAO dao = new TradeOrderDAO();
return dao.get(key);
}
}
14
DOMAIN SERVICE OWNER TRANSACTION
DESIGN PATTERN
15
• The Domain Service Owner Transaction Design Pattern is the most commonly
used transaction design pattern and applies to most Java-based application
architectures you are likely to en-counter.
• In EJB the Domain Service component is typically implemented as a Stateless
SessionBean and therefore uses the Declarative Transaction Model to manage
transactions.
• In Spring the Domain Service component is mplemented as a POJO and also
uses declarative transaction management.
16
the server layer should be responsible for
managing transactions.
• First, we may not know the type of client accessing the backend
services. For example, a Web Services client cannot feasibly
start and propagate a transaction to our domain service.
• Second, assigning responsibility for transaction management at
the client layer typically places too much of a burden on the
client, which should primarily be responsible for presentation
logic, not server-based or middleware logic like transaction
processing.
17
• For these reasons, enterprise Java applications are usually
designed with course-grained domain services that handle a single
business request as a single unit of work. This simplifies the overall
architecture and increases overall application performance.
• It therefore becomes a question of which components should be
responsible for starting the transaction, committing the transaction,
marking the transaction for rollback, and how the transaction should
be propagated to the persistence framework.
18
Using course-grained services :
public void placeFixedIncomeTrade(TradeData trade)
throws Exception {
try {
...
tradingService.placeFixedIncomeTrade(trade);
} catch (Exception e) {
log.fatal(e);
throw e; } }
the transaction management must reside somewhere in the
server-based objects.
19
Forces• The client always makes a single request to domain service
objects to fulfill a single client request.
• ACID properties must be maintained, meaning that transaction
processing is required to maintain data integrity.
• Domain service objects are course-grained and/or use inter-
service communication to perform aggregate service requests.
20
Solution
21
• This is the most common transaction model used for enterprise
Java applications.
• It should be used when the services provided by the Domain
Service components are course grained and the client only
makes one request to the server for a single business request.
• Not only does this type of application architecture offer better
performance than the multiple remote request, but transaction
processing is placed on the server where it really should reside.
22
Consequences
• The client (regardless of its type) does not contain any transaction logic
and does not manage any aspect of the transaction processing.
• Since the Domain Service component is starting and managing the
transaction, update methods must invoke the setRollbackOnly()method on
application (checked) exceptions.
• Persistence objects used by the Domain Service, regardless of the
persistence framework, do not contain any transaction or rollback logic.
• Server-based domain objects use declarative transactions
and use a transaction attribute of Requiredfor update
related methods and Supports for read operations.
23
• To maintain ACID properties client-based objects can never
start a transaction, commit a transaction, or mark a
transaction for rollback.
• Persistence objects used by the Domain Service components,
regardless of the persistence framework, must not contain
any transaction or rollback logic.
24
• If EJB 2.1 Entity Beans are used, the transaction attribute for
update operations must be set to Mandatory and no rollback logic
is used.
• For read operations the Entity Bean method should have a
transaction attribute setting of Required for Container-managed
Persistence (CMP) Entity Beans or Supports for Bean managed
Persistence (BMP) Entity Beans.
25
Implementation@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class TradingServiceImpl implements TradingService
{
public void updateTradeOrder(TradeData trade)
throws TradeUpdateException {
try {
validateUpdate();
TradeOrderDAO dao = new TradeOrderDAO();
dao.update(trade);
} catch (TradeUpdateException e) {
sessionContext.setRollbackOnly();
log.fatal(e);
throw e; } }
26
@TransactionAttribute(
TransactionAttributeType.SUPPORTS)
public TradeData getTrade(TradeKey key)
throws Exception {
TradeOrderDAO dao = new TradeOrderDAO();
return dao.get(key);
}
}
27
Spring Framework
<!-- Define the Domain Service Bean -->
<bean id="tradingServiceTarget"
class="com.trading.server.TradingServiceImpl">
</bean>
<bean id="tradingService"
class="org.springframework.transaction.interceptor.
TransactionProxyFactoryBean">
<property name="transactionManager" ref="txnMgr"/>
<property name="target" ref="tradingServiceTarget"/>
<property name="transactionAttributes">
<props>
<prop key="*">
PROPAGATION_REQUIRED,-Exception
</prop> <prop key="get*">PROPAGATION_SUPPORTS</prop>
28
public class TradingServiceImpl implements TradingService
{
public void updateTradeOrder(TradeData trade)
throws TradeUpdateException {
validateUpdate();
TradeOrderDAO dao = new TradeOrderDAO();
dao.update(trade);
}
public TradeData getTrade(TradeKey key)
throws Exception {
TradeOrderDAO dao = new TradeOrderDAO();
return dao.get(key);
}
}
29
Server Delegate Owner Transaction Design
Pattern
30
• The Command Pattern is a useful design pattern that solves many issues
with regards to client-based transaction management and EJB in general.
• The basic principle behind this pattern is that client functionality is placed in
a command and sent to the server for execution.
• That command may contain one or more Domain Service method
invocations. However, those Domain Service method invocations are all
executed on the server via a corresponding Command Implementation
object rather than on the client.
• Use of the Command Pattern allows you to make single requests to the
Domain Services components from the client and also allows transaction
processing to be managed by the server rather than the client.
31
Context
public class ClientModel
{
public void placeFixedIncomeTrade(TradeData trade)
throws Exception {
InitialContext ctx = new InitialContext();
UserTransaction txn = (UserTransaction)
ctx.lookup("java:comp/UserTransaction");
try {
txn.begin();
...
Placement placement =
placementService.placeTrade(trade);
executionService.executeTrade(placement);
txn.commit();
} catch (TradeUpdateException e) {
32
• In a move towards simplifying this type of situation, we would refactor the
Stateless SessionBean Domain Services to POJOs, remove transaction logic
from the client, and only make a single call to the server for any given client
request.
public class ClientModel
{
public void placeFixedIncomeTrade(TradeData trade)
throws Exception {
PlaceFITradeCommand command = new PlaceFITradeCommand();
command.setTrade(trade);
CommandHandler.execute(command); }}
33
Forces
• The Command Pattern or Server Delegate Design Pattern is used in the
application architecture for client/server communication.
• The client always makes a single request to a Server Delegate to fulfill a
single client request.
• ACID properties must be maintained, meaning that transaction processing
is required to maintain data integrity.
• Domain service objects are implemented as POJOs and may be remote or
local.
• The Command Processor or Server Delegate is the only entry point into
the domain services from the client.
34
Solution
35
Consequences
• The client (regardless of its type) does not contain any transaction logic and
does not manage any aspect of the transaction processing.
• Since the Server Delegate component is starting and managing the
transaction, update methods must invoke the setRollbackOnly() method on
application exceptions.
• The transaction established by the Server Delegate is propagated to the
POJO-based Domain Services objects as well as the Persistence objects used
by the Domain Service (regardless of the persistence framework).
Furthermore, none of these components contain any transaction or rollback
logic.
• Server Delegate objects use declarative transactions and use a transaction
attribute of Requiredfor update-related methods and Supportsfor read
operations.
36
• To maintain ACID properties client-based objects can never start a
transaction, commit a transaction, or mark a transaction for rollback.
• Persistence objects and Domain Services do not contain any transaction or
rollback logic.
• If EJB 2.1 Entity Beans are used, the transaction attribute for update
operations must be set to Mandatoryand no rollback logic is used. For read
operations the Entity Bean method should have a transaction attribute
setting of Requiredfor Container-managed Persistence (CMP) Entity Beans
or Supportsfor Bean-managed Persistence (BMP) Entity Beans.
37
Enterprise JavaBeans (EJB)
@Stateless
public class CommandProcessorImpl
implements CommandProcessor
{
@TransactionAttribute(
TransactionAttributeType.SUPPORTS)
public BaseCommand executeRead(BaseCommand command)
throws Exception {
CommandImpl implementationClass =
getCommandImpl(command); //introspection
return implementationClass.execute(command);
38
@TransactionAttribute(
TransactionAttributeType.REQUIRED)
public BaseCommand executeUpdate(BaseCommand command) //le
nom du classe qui appelle les services
throws Exception {
try {
CommandImpl implementationClass =
getCommandImpl(command);
return implementationClass.execute(command); //introspection
} catch (Exception e) {
sessionCtx.setRollbackOnly();
throw e; }}}
39
<!-- Define the Server Delegate Command Processor -->
<bean id="commandProcessorTarget"
class="com.commandframework.server.
commandProcessorImpl">
</bean>
<bean id="commandProcessor"
class="org.springframework.transaction.interceptor.
TransactionProxyFactoryBean">
<property name="transactionManager" ref="txnMgr"/>
<property name="target" ref="tradingServiceTarget"/>
<property name="transactionAttributes">
<props>
<prop key="executeUpdate">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="executeRead">
PROPAGATION_SUPPORTS </prop> </props> </property> </bean>
40
public class CommandProcessorImpl
implements CommandProcessor
{
public BaseCommand executeRead(BaseCommand command)
throws Exception {
CommandImpl implementationClass =
getCommandImpl(command);
return implementationClass.execute(command);
}
public BaseCommand executeUpdate(BaseCommand command)
throws Exception {
CommandImpl implementationClass =
getCommandImpl(command);
return implementationClass.execute(command); }}
41
Template and Command Pattern
42
L'interface Commande
public abstract class Commande {
public abstract void execute(int somme);
}
The concrete command : TransactionDebit, uses Template pattern :
public abstract class TransactionDebit extends Commande{
protected Compte compte;
public abstract void beginTransaction(); // Template Method
public abstract void endTransaction();
public abstract void rollbackTransaction();
public TransactionDebit(Compte compte){
this.compte = compte;
}
43
public void execute(int somme){ // commande atomique
try{
beginTransaction();
compte.debit(somme);
endTransaction();
}catch(Exception e){
rollbackTransaction();
}
44
• http://www.theserverside.com/patterns/thread.tss?thread_i
d=14387.
• http://www.ibm.com/developerworks/ibm/library/i-
extreme13/
45
Transaction processing is very important and also necessary to
maintain data integrity in both your application and database.
Transaction management in Java does not have to be
complicated using the transaction design patterns described
in this chapter makes transaction processing easy to
understand, implement, and maintain.

Contenu connexe

Similaire à Transaction design patterns

Domain Logic Patterns
Domain Logic PatternsDomain Logic Patterns
Domain Logic PatternsElifTech
 
Distributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEEDistributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEEMushfekur Rahman
 
5 application serversforproject
5 application serversforproject5 application serversforproject
5 application serversforprojectashish61_scs
 
Wcf Transaction Handling
Wcf Transaction HandlingWcf Transaction Handling
Wcf Transaction HandlingGaurav Arora
 
Transaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in JavaTransaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in JavaErsen Öztoprak
 
Informatica powercenter8.x Aarchitecture
Informatica powercenter8.x AarchitectureInformatica powercenter8.x Aarchitecture
Informatica powercenter8.x AarchitectureRaj Ningthemcha
 
How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...
How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...
How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...chaturanga ranatunga
 
Complete Architecture and Development Guide To Windows Communication Foundati...
Complete Architecture and Development Guide To Windows Communication Foundati...Complete Architecture and Development Guide To Windows Communication Foundati...
Complete Architecture and Development Guide To Windows Communication Foundati...Abdul Khan
 
Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?Katherine Golovinova
 
A presentation on WCF & REST
A presentation on WCF & RESTA presentation on WCF & REST
A presentation on WCF & RESTSanthu Rao
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - TransactionsDmitry Buzdin
 
Service-Level Objective for Serverless Applications
Service-Level Objective for Serverless ApplicationsService-Level Objective for Serverless Applications
Service-Level Objective for Serverless Applicationsalekn
 
Lecture 1: Introduction to JEE
Lecture 1:  Introduction to JEELecture 1:  Introduction to JEE
Lecture 1: Introduction to JEEFahad Golra
 
vbrownbag dcd6-2.4-merged
vbrownbag dcd6-2.4-mergedvbrownbag dcd6-2.4-merged
vbrownbag dcd6-2.4-mergedVirtualtiers
 
Continuation_alan_20220503.pdf
Continuation_alan_20220503.pdfContinuation_alan_20220503.pdf
Continuation_alan_20220503.pdfShen yifeng
 

Similaire à Transaction design patterns (20)

Grails services
Grails servicesGrails services
Grails services
 
Grails services
Grails servicesGrails services
Grails services
 
Domain Logic Patterns
Domain Logic PatternsDomain Logic Patterns
Domain Logic Patterns
 
Distributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEEDistributed Transaction Management in Spring & JEE
Distributed Transaction Management in Spring & JEE
 
Grails Services
Grails ServicesGrails Services
Grails Services
 
5 application serversforproject
5 application serversforproject5 application serversforproject
5 application serversforproject
 
Wcf Transaction Handling
Wcf Transaction HandlingWcf Transaction Handling
Wcf Transaction Handling
 
Autonomous transaction
Autonomous transactionAutonomous transaction
Autonomous transaction
 
Transaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in JavaTransaction and concurrency pitfalls in Java
Transaction and concurrency pitfalls in Java
 
Informatica powercenter8.x Aarchitecture
Informatica powercenter8.x AarchitectureInformatica powercenter8.x Aarchitecture
Informatica powercenter8.x Aarchitecture
 
How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...
How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...
How to Structure Your Xamarin Solution by Adhering to Common .NET Practices -...
 
Complete Architecture and Development Guide To Windows Communication Foundati...
Complete Architecture and Development Guide To Windows Communication Foundati...Complete Architecture and Development Guide To Windows Communication Foundati...
Complete Architecture and Development Guide To Windows Communication Foundati...
 
Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?
 
A presentation on WCF & REST
A presentation on WCF & RESTA presentation on WCF & REST
A presentation on WCF & REST
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
 
Service-Level Objective for Serverless Applications
Service-Level Objective for Serverless ApplicationsService-Level Objective for Serverless Applications
Service-Level Objective for Serverless Applications
 
Lecture 1: Introduction to JEE
Lecture 1:  Introduction to JEELecture 1:  Introduction to JEE
Lecture 1: Introduction to JEE
 
vbrownbag dcd6-2.4-merged
vbrownbag dcd6-2.4-mergedvbrownbag dcd6-2.4-merged
vbrownbag dcd6-2.4-merged
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
Continuation_alan_20220503.pdf
Continuation_alan_20220503.pdfContinuation_alan_20220503.pdf
Continuation_alan_20220503.pdf
 

Plus de Ben Abdallah Helmi

SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9Ben Abdallah Helmi
 
SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3Ben Abdallah Helmi
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2Ben Abdallah Helmi
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8Ben Abdallah Helmi
 
SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6Ben Abdallah Helmi
 
SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5Ben Abdallah Helmi
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4Ben Abdallah Helmi
 
SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3Ben Abdallah Helmi
 
SCWCD : The servlet model : CHAP : 2
SCWCD  : The servlet model : CHAP : 2SCWCD  : The servlet model : CHAP : 2
SCWCD : The servlet model : CHAP : 2Ben Abdallah Helmi
 
SCWCD : The web client model : CHAP : 1
SCWCD  : The web client model : CHAP : 1SCWCD  : The web client model : CHAP : 1
SCWCD : The web client model : CHAP : 1Ben Abdallah Helmi
 
SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11Ben Abdallah Helmi
 
Ejb3 3-message-driven-beans fr
Ejb3 3-message-driven-beans frEjb3 3-message-driven-beans fr
Ejb3 3-message-driven-beans frBen Abdallah Helmi
 

Plus de Ben Abdallah Helmi (20)

The Data Warehouse .pdf
The Data Warehouse .pdfThe Data Warehouse .pdf
The Data Warehouse .pdf
 
SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9
 
SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2
 
SCWCD : The web client model
SCWCD : The web client modelSCWCD : The web client model
SCWCD : The web client model
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8
 
SCWCD : Secure web : CHAP : 7
SCWCD : Secure web : CHAP : 7SCWCD : Secure web : CHAP : 7
SCWCD : Secure web : CHAP : 7
 
SCWCD : Secure web
SCWCD : Secure webSCWCD : Secure web
SCWCD : Secure web
 
SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6
 
SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
 
SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3
 
SCWCD : The servlet model : CHAP : 2
SCWCD  : The servlet model : CHAP : 2SCWCD  : The servlet model : CHAP : 2
SCWCD : The servlet model : CHAP : 2
 
SCWCD : The web client model : CHAP : 1
SCWCD  : The web client model : CHAP : 1SCWCD  : The web client model : CHAP : 1
SCWCD : The web client model : CHAP : 1
 
SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11
 
Ejb3 3-message-driven-beans fr
Ejb3 3-message-driven-beans frEjb3 3-message-driven-beans fr
Ejb3 3-message-driven-beans fr
 
Ejb3 2-session-beans fr
Ejb3 2-session-beans frEjb3 2-session-beans fr
Ejb3 2-session-beans fr
 
Ejb3 1-server-setup fr
Ejb3 1-server-setup frEjb3 1-server-setup fr
Ejb3 1-server-setup fr
 
Axis2 services fr
Axis2 services frAxis2 services fr
Axis2 services fr
 
Axis2 clients fr
Axis2 clients frAxis2 clients fr
Axis2 clients fr
 

Dernier

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Dernier (20)

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

Transaction design patterns

  • 1. 1 Transaction Design patterns Helmi Ben Abdallah @rchitect J2EE
  • 2. 2 A Brief Introduction to the Patterns • The transaction design patterns that are described in the next are : • Client Owner Transaction Design Pattern • Domain Service Owner Transaction Design Pattern • Server Delegate Owner Transaction Design Pattern
  • 3. 3 CLIENT OWNER TRANSACTION DESIGN PATTERN
  • 4. 4 Context • Although undesirable, in certain situations it is necessary to force transaction management up to the client layer within an enterprise Java application. • Consider the all to common situation where a single request is made from the client, but the client requires multiple (remote) calls to the server to fulfill that request. • This situation occurs most often when domain services are too fine- grained and no aggregate services exist. When this type of situation occurs, we are required to push transaction management up to the presentation layer to maintain ACID properties.
  • 5. 5 public void placeFixedIncomeTrade(TradeData trade) throws Exception { try { ... Placement placement = placementService.placeTrade(trade); executionService.executeTrade(placement); } catch (Exception e) { log.fatal(e); throw e; }
  • 6. 6 Forces • The client requires multiple remote or local calls to do main service objects to fulfill a single business request. • ACID properties must be maintained, meaning that transaction processing is required to maintain data integrity. • Domain service objects are fine-grained and no aggregate services exist to combine business requests. • It is not possible to re-architect or refactor the application to provide a single domain service method invocation for a single client request for all client requests.
  • 8. 8 Consequences • The client object managing the transaction must use programmatic transactions if using EJB, or can use declarative transactions if the client object is managed by Spring. • Server-based domain objects must use declarative transactions since a programmatic transaction cannot be propagated to other programmatic transaction- managed beans. • To maintain ACID properties server-based objects never start or commit a transaction. Also, server-based objects never mark a transaction for rollback. • No transaction logic is necessary in the client or the server for query-related methods. However, server-based domain objects must have a transaction attribute setting of Supports for query methods. • Persistence objects used by the Domain Service components, regardless of the persistence framework, must not contain any transaction or rollback logic.
  • 9. 9 Implementationpublic class ClientModel { public void updateTradeOrder(TradeData trade) throws Exception { InitialContext ctx = new InitialContext(); UserTransaction txn = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); try { txn.begin(); TradingService tradingService = ServiceLocator.getTradingService(); tradingService.updateTradeOrder(trade); txn.commit(); } catch (TradeUpdateException e) { txn.rollback(); EJB
  • 10. 10 @Stateless @TransactionAttribute(TransactionAttributeType.MANDATORY) public class TradingServiceImpl implements TradingService { public void updateTradeOrder(TradeData trade) throws TradeUpdateException { validateUpdate(); TradeOrderDAO dao = new TradeOrderDAO(); dao.update(trade);}} the client is completely responsible for managing the transaction, the Domain Service component must never start a transaction, which is why it is assigned the transaction attribute of Mandatory.
  • 11. 11 <!-- Define the Client Delegate Bean --> <bean id="clientModelTarget" class="com.trading.client.ClientModel"> <property name="tradingService" ref="tradingService"/> </bean> <bean id="clientModel" class="org.springframework.transaction.interceptor. TransactionProxyFactoryBean"> <property name="transactionManager" ref="txnMgr"/> <property name="target" ref="clientModelTarget"/> <property name="transactionAttributes"> <props> <prop key="*"> PROPAGATION_REQUIRED,-Exception </prop> <prop key="get*">PROPAGATION_SUPPORTS</prop> </props> </property> </bean> spring
  • 12. 12 <!-- Define the Domain Service Bean --> <bean id="tradingServiceTarget" class="com.trading.server.TradingServiceImpl"> </bean> <bean id="tradingService" class="org.springframework.transaction.interceptor. TransactionProxyFactoryBean"> <property name="transactionManager" ref="txnMgr"/> <property name="target" ref="tradingServiceTarget"/> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_MANDATORY</prop> <prop key="get*">PROPAGATION_SUPPORTS</prop>
  • 13. 13 public class ClientModel { public void updateTradeOrder(TradeData trade) throws Exception { tradingService.updateTradeOrder(trade); } public TradeData getTradeOrder(TradeKey key) throws Exception { return tradingService.getTrade(key); } } public class TradingServiceImpl implements TradingService { public void updateTradeOrder(TradeData trade) throws TradeUpdateException { validateUpdate(); TradeOrderDAO dao = new TradeOrderDAO(); dao.update(trade); } public TradeData getTrade(TradeKey key) throws Exception { TradeOrderDAO dao = new TradeOrderDAO(); return dao.get(key); } }
  • 14. 14 DOMAIN SERVICE OWNER TRANSACTION DESIGN PATTERN
  • 15. 15 • The Domain Service Owner Transaction Design Pattern is the most commonly used transaction design pattern and applies to most Java-based application architectures you are likely to en-counter. • In EJB the Domain Service component is typically implemented as a Stateless SessionBean and therefore uses the Declarative Transaction Model to manage transactions. • In Spring the Domain Service component is mplemented as a POJO and also uses declarative transaction management.
  • 16. 16 the server layer should be responsible for managing transactions. • First, we may not know the type of client accessing the backend services. For example, a Web Services client cannot feasibly start and propagate a transaction to our domain service. • Second, assigning responsibility for transaction management at the client layer typically places too much of a burden on the client, which should primarily be responsible for presentation logic, not server-based or middleware logic like transaction processing.
  • 17. 17 • For these reasons, enterprise Java applications are usually designed with course-grained domain services that handle a single business request as a single unit of work. This simplifies the overall architecture and increases overall application performance. • It therefore becomes a question of which components should be responsible for starting the transaction, committing the transaction, marking the transaction for rollback, and how the transaction should be propagated to the persistence framework.
  • 18. 18 Using course-grained services : public void placeFixedIncomeTrade(TradeData trade) throws Exception { try { ... tradingService.placeFixedIncomeTrade(trade); } catch (Exception e) { log.fatal(e); throw e; } } the transaction management must reside somewhere in the server-based objects.
  • 19. 19 Forces• The client always makes a single request to domain service objects to fulfill a single client request. • ACID properties must be maintained, meaning that transaction processing is required to maintain data integrity. • Domain service objects are course-grained and/or use inter- service communication to perform aggregate service requests.
  • 21. 21 • This is the most common transaction model used for enterprise Java applications. • It should be used when the services provided by the Domain Service components are course grained and the client only makes one request to the server for a single business request. • Not only does this type of application architecture offer better performance than the multiple remote request, but transaction processing is placed on the server where it really should reside.
  • 22. 22 Consequences • The client (regardless of its type) does not contain any transaction logic and does not manage any aspect of the transaction processing. • Since the Domain Service component is starting and managing the transaction, update methods must invoke the setRollbackOnly()method on application (checked) exceptions. • Persistence objects used by the Domain Service, regardless of the persistence framework, do not contain any transaction or rollback logic. • Server-based domain objects use declarative transactions and use a transaction attribute of Requiredfor update related methods and Supports for read operations.
  • 23. 23 • To maintain ACID properties client-based objects can never start a transaction, commit a transaction, or mark a transaction for rollback. • Persistence objects used by the Domain Service components, regardless of the persistence framework, must not contain any transaction or rollback logic.
  • 24. 24 • If EJB 2.1 Entity Beans are used, the transaction attribute for update operations must be set to Mandatory and no rollback logic is used. • For read operations the Entity Bean method should have a transaction attribute setting of Required for Container-managed Persistence (CMP) Entity Beans or Supports for Bean managed Persistence (BMP) Entity Beans.
  • 25. 25 Implementation@Stateless @TransactionAttribute(TransactionAttributeType.REQUIRED) public class TradingServiceImpl implements TradingService { public void updateTradeOrder(TradeData trade) throws TradeUpdateException { try { validateUpdate(); TradeOrderDAO dao = new TradeOrderDAO(); dao.update(trade); } catch (TradeUpdateException e) { sessionContext.setRollbackOnly(); log.fatal(e); throw e; } }
  • 26. 26 @TransactionAttribute( TransactionAttributeType.SUPPORTS) public TradeData getTrade(TradeKey key) throws Exception { TradeOrderDAO dao = new TradeOrderDAO(); return dao.get(key); } }
  • 27. 27 Spring Framework <!-- Define the Domain Service Bean --> <bean id="tradingServiceTarget" class="com.trading.server.TradingServiceImpl"> </bean> <bean id="tradingService" class="org.springframework.transaction.interceptor. TransactionProxyFactoryBean"> <property name="transactionManager" ref="txnMgr"/> <property name="target" ref="tradingServiceTarget"/> <property name="transactionAttributes"> <props> <prop key="*"> PROPAGATION_REQUIRED,-Exception </prop> <prop key="get*">PROPAGATION_SUPPORTS</prop>
  • 28. 28 public class TradingServiceImpl implements TradingService { public void updateTradeOrder(TradeData trade) throws TradeUpdateException { validateUpdate(); TradeOrderDAO dao = new TradeOrderDAO(); dao.update(trade); } public TradeData getTrade(TradeKey key) throws Exception { TradeOrderDAO dao = new TradeOrderDAO(); return dao.get(key); } }
  • 29. 29 Server Delegate Owner Transaction Design Pattern
  • 30. 30 • The Command Pattern is a useful design pattern that solves many issues with regards to client-based transaction management and EJB in general. • The basic principle behind this pattern is that client functionality is placed in a command and sent to the server for execution. • That command may contain one or more Domain Service method invocations. However, those Domain Service method invocations are all executed on the server via a corresponding Command Implementation object rather than on the client. • Use of the Command Pattern allows you to make single requests to the Domain Services components from the client and also allows transaction processing to be managed by the server rather than the client.
  • 31. 31 Context public class ClientModel { public void placeFixedIncomeTrade(TradeData trade) throws Exception { InitialContext ctx = new InitialContext(); UserTransaction txn = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); try { txn.begin(); ... Placement placement = placementService.placeTrade(trade); executionService.executeTrade(placement); txn.commit(); } catch (TradeUpdateException e) {
  • 32. 32 • In a move towards simplifying this type of situation, we would refactor the Stateless SessionBean Domain Services to POJOs, remove transaction logic from the client, and only make a single call to the server for any given client request. public class ClientModel { public void placeFixedIncomeTrade(TradeData trade) throws Exception { PlaceFITradeCommand command = new PlaceFITradeCommand(); command.setTrade(trade); CommandHandler.execute(command); }}
  • 33. 33 Forces • The Command Pattern or Server Delegate Design Pattern is used in the application architecture for client/server communication. • The client always makes a single request to a Server Delegate to fulfill a single client request. • ACID properties must be maintained, meaning that transaction processing is required to maintain data integrity. • Domain service objects are implemented as POJOs and may be remote or local. • The Command Processor or Server Delegate is the only entry point into the domain services from the client.
  • 35. 35 Consequences • The client (regardless of its type) does not contain any transaction logic and does not manage any aspect of the transaction processing. • Since the Server Delegate component is starting and managing the transaction, update methods must invoke the setRollbackOnly() method on application exceptions. • The transaction established by the Server Delegate is propagated to the POJO-based Domain Services objects as well as the Persistence objects used by the Domain Service (regardless of the persistence framework). Furthermore, none of these components contain any transaction or rollback logic. • Server Delegate objects use declarative transactions and use a transaction attribute of Requiredfor update-related methods and Supportsfor read operations.
  • 36. 36 • To maintain ACID properties client-based objects can never start a transaction, commit a transaction, or mark a transaction for rollback. • Persistence objects and Domain Services do not contain any transaction or rollback logic. • If EJB 2.1 Entity Beans are used, the transaction attribute for update operations must be set to Mandatoryand no rollback logic is used. For read operations the Entity Bean method should have a transaction attribute setting of Requiredfor Container-managed Persistence (CMP) Entity Beans or Supportsfor Bean-managed Persistence (BMP) Entity Beans.
  • 37. 37 Enterprise JavaBeans (EJB) @Stateless public class CommandProcessorImpl implements CommandProcessor { @TransactionAttribute( TransactionAttributeType.SUPPORTS) public BaseCommand executeRead(BaseCommand command) throws Exception { CommandImpl implementationClass = getCommandImpl(command); //introspection return implementationClass.execute(command);
  • 38. 38 @TransactionAttribute( TransactionAttributeType.REQUIRED) public BaseCommand executeUpdate(BaseCommand command) //le nom du classe qui appelle les services throws Exception { try { CommandImpl implementationClass = getCommandImpl(command); return implementationClass.execute(command); //introspection } catch (Exception e) { sessionCtx.setRollbackOnly(); throw e; }}}
  • 39. 39 <!-- Define the Server Delegate Command Processor --> <bean id="commandProcessorTarget" class="com.commandframework.server. commandProcessorImpl"> </bean> <bean id="commandProcessor" class="org.springframework.transaction.interceptor. TransactionProxyFactoryBean"> <property name="transactionManager" ref="txnMgr"/> <property name="target" ref="tradingServiceTarget"/> <property name="transactionAttributes"> <props> <prop key="executeUpdate"> PROPAGATION_REQUIRED,-Exception </prop> <prop key="executeRead"> PROPAGATION_SUPPORTS </prop> </props> </property> </bean>
  • 40. 40 public class CommandProcessorImpl implements CommandProcessor { public BaseCommand executeRead(BaseCommand command) throws Exception { CommandImpl implementationClass = getCommandImpl(command); return implementationClass.execute(command); } public BaseCommand executeUpdate(BaseCommand command) throws Exception { CommandImpl implementationClass = getCommandImpl(command); return implementationClass.execute(command); }}
  • 42. 42 L'interface Commande public abstract class Commande { public abstract void execute(int somme); } The concrete command : TransactionDebit, uses Template pattern : public abstract class TransactionDebit extends Commande{ protected Compte compte; public abstract void beginTransaction(); // Template Method public abstract void endTransaction(); public abstract void rollbackTransaction(); public TransactionDebit(Compte compte){ this.compte = compte; }
  • 43. 43 public void execute(int somme){ // commande atomique try{ beginTransaction(); compte.debit(somme); endTransaction(); }catch(Exception e){ rollbackTransaction(); }
  • 45. 45 Transaction processing is very important and also necessary to maintain data integrity in both your application and database. Transaction management in Java does not have to be complicated using the transaction design patterns described in this chapter makes transaction processing easy to understand, implement, and maintain.

Notes de l'éditeur

  1. This is particularly important when using web services clients or third-party client packages that are not easily modified.