Aspects Made Explicit for
Safe Transactional Semantics
Kevin Hoffman / Patrick Eugster
-2-Kevin Hoffman and Patrick Eugster
Properties of Transactions
A tomicity – all or nothing
C onsistency – changes respect specifications
I solation – effects of concurrency masked
D urability – committed results never lost
-3-Kevin Hoffman and Patrick Eugster
Implementation Approaches
Library approach
- Safety
- Elegance
+Flexibility
+Interoperability
Language approach
+Safety
+Elegance
- Flexibility
- Interoperability
-4-Kevin Hoffman and Patrick Eugster
The Pursuit of Elegance
Cross-cutting concerns tangle/scatter code
Aspect Oriented Programming (AOP) / AspectJ
Exposes structural points (e.g. method call, member
accesses) of a program’s primary decomposition as
language features
Provides language features (aspects) to superimpose
logic (advice) systematically onto the mainline code
(via pointcuts)
AOP improves elegance but complicates safety
-5-Kevin Hoffman and Patrick Eugster
AOP/AspectJ in 60 seconds
void buy(…){
…}
void cancel(…){
…}
void return(…){
…}
Store class
…DB.buy(order);…
…DB.cancel(o);…
…DB.return(o);…
pointcut tranOp():call(* * Store.*(..));
around(): tranOp() { //adviceT = thisJoinP.getTarget();TransMan.beginTrans(T);try{
Object ret = proceed();TransMan.commit(T);return ret;
} catch (Exception e){TransMan.abort(T);throw e;
}}
JSP Pages Transactionalizer aspect
-6-Kevin Hoffman and Patrick Eugster
AOP/AspectJ Safety Issues
void buy(…){
…}
void cancel(…){
…}
void return(…){
…}
Store class
…DB.buy(order);…
…DB.cancel(o);…
…DB.return(o);…
JSP Pages Transactionalizer aspect
pointcut tranOp():call(* * Store.*(..));
around(): tranOp() { //adviceT = thisJoinP.getTarget();TransMan.beginTrans(T);try{
Object ret = proceed();TransMan.commit(T);return ret;
} catch (Exception e){TransMan.abort(T);throw e;
}}
Aspects can throw
unexpected
exceptions
due to new logic.
But there is no way
to constrain the
base code to catch
these exceptions…
-7-Kevin Hoffman and Patrick Eugster
void buy(…){TA.jpTrans() {
…
}}void cancel(…){
TA.jpTrans() {…
}}void return(…){
TA.jpTrans() {…
}}
Our Idea: Explicit Join PointsStore class
…DB.buy(order);…
…DB.cancel(o);…
…DB.return(o);…
scoped joinpoint jpTrans()throws TranException;
pointcut tranOp():joinpoint(jpTrans);
before (): tranOp() {T = thisJoinP.getTarget();TransMan.beginTrans(T);
}after () throwing: tranOp() {
TransMan.abort(T);}after () returning: tranOp() {
TransMan.commit(T);}
JSP PagesTransactionalizer aspect [TA]
-8-Kevin Hoffman and Patrick Eugster
EJP Larger Example [Aspect]aspect Transactionalizer {
scoped joinpoint jpTrans(Serializable[ ] objects)throws TransactionException; /* EJPs makes this constraint possible */
pointcut pcTrans(Serializable[ ] objects) :joinpoint(jpTrans) && args(objects);
before(Serializable[ ] objs): pcTrans(objs) {TransMan.begin(objs, ...);
}
after() throwing: pcTrans() { // if mainline code fails,
TransMan.abort(...); // abort transaction
}after() returning: pcTrans() { // after normal completion,
TransMan.commit(...); // commit transaction
}}
-9-Kevin Hoffman and Patrick Eugster
EJP Larger Example [Mainline]/* EJPs explicitly require object to be Serializable to be Transactionalized */
class Agent implements Serializable {
void createTrip(Person p, Flight f, Hotel h, RentalCar c){
boolean bCharged = false;
try {
/* EJPs provide bilateral communication with aspects */
Serializable [ ] objects = {this, p, f, h, c};
joinpoint Transactionalizer.jpTrans(objects) {
/* Scope of transaction specified by scope of EJP */
f.ReserveSeat(p); h.ReserveRoom(p);
c.ReserveVehicle(c); m_CC.Debit(p.getCC(), …);
bCharged=true;
}
} catch (TransactionException e) { /* Required by EJP */
if (bCharged){ m_CC.Credit(p.getCC(), …); }
}
}
-10-Kevin Hoffman and Patrick Eugster
Why is an EJP not a method call?
Multiple aspects may advise the EJP
Scoped EJPs provide new code structure
which can be advised, increasing safety
Aspect instance can depend on context of
joinpoint (via pertarget, percflow, …)
Pointcuts may be passed as parameters,
utilizing the full power of AOP features
-11-Kevin Hoffman and Patrick Eugster
How EJP Measures Up
+ Safety: EJP constraints enforced at
compile time
+ Elegance: EJP increases AspectJ power
(bilaterial communication, constraints)
+ Flexibility: Change implementation easily
+ Interoperability: Superset of AspectJ;
works with any other Java program
-12-Kevin Hoffman and Patrick Eugster
The End of the Beginning
Top Related