crystal reports barcode generator free TransactionLogging.java: an improved version in Java

Encoder QR in Java TransactionLogging.java: an improved version

Listing 11.7 TransactionLogging.java: an improved version
QR Code Encoder In Java
Using Barcode generator for Java Control to generate, create QR Code image in Java applications.
www.OnBarcode.com
Read QR In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
package banking; import java.sql.*; import org.aspectj.lang.*; import logging.*; import transaction.jdbc.*; public aspect TransactionLogging extends IndentedLogging { declare precedence: TransactionLogging, *; public pointcut accountActivities() : call(void Account.credit(..)) || call(void Account.debit(..)) || call(void InterAccountTransferSystem.transfer(..)); public pointcut connectionActivities(Connection conn) : (call(* Connection.commit(..)) || call(* Connection.rollback(..))) && target(conn); public pointcut updateActivities(Statement stmt) : call(* Statement.executeUpdate(..)) && target(stmt); public pointcut aspectInstantiation() : execution(JDBCTransactionAspect+.new(..)); public pointcut loggedOperations() : accountActivities() || connectionActivities(Connection) || updateActivities(Statement) || aspectInstantiation(); before() : accountActivities() { Signature sig = thisJoinPointStaticPart.getSignature(); System.out.println("[" + sig.getName() + "]"); }
Drawing ANSI/AIM Code 39 In Java
Using Barcode generation for Java Control to generate, create Code-39 image in Java applications.
www.OnBarcode.com
EAN / UCC - 13 Generator In Java
Using Barcode creation for Java Control to generate, create UCC - 12 image in Java applications.
www.OnBarcode.com
Transaction management
Make 2D In Java
Using Barcode encoder for Java Control to generate, create Matrix Barcode image in Java applications.
www.OnBarcode.com
Linear Barcode Maker In Java
Using Barcode printer for Java Control to generate, create Linear Barcode image in Java applications.
www.OnBarcode.com
before(Connection conn) : connectionActivities(conn) { Signature sig = thisJoinPointStaticPart.getSignature(); System.out.println("[" + sig.getName() + "] " + conn); } before(Statement stmt) throws SQLException : updateActivities(stmt) { Signature sig = thisJoinPointStaticPart.getSignature(); System.out.println("[" + sig.getName() + "] " + stmt.getConnection()); } before() : aspectInstantiation() { Signature sig = thisJoinPointStaticPart.getSignature(); System.out.println("[" + sig.getName() + "] " + sig.getDeclaringType()); } }
Encode Quick Response Code In Java
Using Barcode creation for Java Control to generate, create QR Code 2d barcode image in Java applications.
www.OnBarcode.com
Print Code11 In Java
Using Barcode maker for Java Control to generate, create USD8 image in Java applications.
www.OnBarcode.com
When we compile all the classes and aspects and run the program, we see output similar to the following:
Encode Quick Response Code In None
Using Barcode encoder for Word Control to generate, create QR Code image in Microsoft Word applications.
www.OnBarcode.com
Recognize QR Code JIS X 0510 In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
> ajc banking\*.java logging\*.java transaction\jdbc\*.java > java banking.Test [credit] [<init>] class transaction.jdbc.JDBCTransactionAspect [<init>] class banking.BankingTransactionAspect [executeUpdate] sun.jdbc.odbc.JdbcOdbcConnection@fa3ac1 [commit] sun.jdbc.odbc.JdbcOdbcConnection@fa3ac1 [debit] [<init>] class transaction.jdbc.JDBCTransactionAspect [<init>] class banking.BankingTransactionAspect [executeUpdate] sun.jdbc.odbc.JdbcOdbcConnection@1833955 [commit] sun.jdbc.odbc.JdbcOdbcConnection@1833955 [transfer] [<init>] class transaction.jdbc.JDBCTransactionAspect [<init>] class banking.BankingTransactionAspect [credit] [executeUpdate] sun.jdbc.odbc.JdbcOdbcConnection@21b6d [debit] [executeUpdate] sun.jdbc.odbc.JdbcOdbcConnection@21b6d [commit] sun.jdbc.odbc.JdbcOdbcConnection@21b6d [transfer] [<init>] class transaction.jdbc.JDBCTransactionAspect [<init>] class banking.BankingTransactionAspect [credit] [executeUpdate] sun.jdbc.odbc.JdbcOdbcConnection@c9ba38 [debit] [rollback] sun.jdbc.odbc.JdbcOdbcConnection@c9ba38
Recognize Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Barcode Creator In None
Using Barcode encoder for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Developing a simple AspectJ-based solution
Barcode Encoder In Objective-C
Using Barcode maker for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
Drawing Code 128 Code Set C In .NET
Using Barcode drawer for Reporting Service Control to generate, create USS Code 128 image in Reporting Service applications.
www.OnBarcode.com
Exception in thread "main" transaction.jdbc.JDBCTransactionAspect $TransactionException: banking.InsufficientBalanceException: Total balance not sufficient ... the rest of call stack
QR Code 2d Barcode Generator In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
www.OnBarcode.com
Make Data Matrix ECC200 In None
Using Barcode generation for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
Let s examine the output:
Code 128 Code Set A Drawer In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Code 128C image in VS .NET applications.
www.OnBarcode.com
Read ECC200 In Visual Basic .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
A new aspect instance is created at the beginning of each top-level operation as a result of associating the aspect with the control flow of each top-level operation. The same connection object is used for all the operations inside each transaction. Specifically, both credit() and debit() operations called during the transfer of money from one account to the other use the same connection (JdbcOdbcConnection@21b6d and JdbcOdbcConnection@c9ba38). The commit call is made only at the end of the transaction. Even though credit() and debit() did call commit() on the connection object, our aspect bypassed it. The connection is rolled back when the last transfer operation throws an exception.
Painting ECC200 In None
Using Barcode creator for Software Control to generate, create Data Matrix 2d barcode image in Software applications.
www.OnBarcode.com
Code-39 Creator In Java
Using Barcode creator for BIRT reports Control to generate, create Code 39 image in Eclipse BIRT applications.
www.OnBarcode.com
Note the exception type, JDBCTransactionAspect.TransactionException, shown in the stack trace. It is wrapping the original InsufficientBalanceException exception. Because the transfer() method declares that it throws a checked InsufficientBalanceException exception, the caller of transfer() expects an InsufficientBalanceException and not a runtime exception. We can take care of this problem by adding an aspect (listing 11.8) that preserves the checked exception, based on the exception introduction pattern presented in chapter 8.
Listing 11.8 An aspect that preserves the checked exception
package banking; import transaction.jdbc.JDBCTransactionAspect; public aspect PreserveCheckedException { after() throwing(JDBCTransactionAspect.TransactionException ex) throws InsufficientBalanceException : call(* banking..*.*(..) throws InsufficientBalanceException) { Throwable cause = ex.getCause(); if (cause instanceof InsufficientBalanceException) { throw (InsufficientBalanceException)cause;
Transaction management
} else { throw ex; } } }
The aspect in listing 11.8 captures the methods throwing InsufficientBalanceException and advises them to check whether the cause of the exception is of the InsufficientBalanceException type; if so, it simply throws the cause instead of the caught exception. Now when we compile and run the test program after including this aspect, the last transaction log looks like this:
[transfer] [<init>] class transaction.jdbc.JDBCTransactionAspect [<init>] class banking.BankingTransactionAspect [credit] [executeUpdate] sun.jdbc.odbc.JdbcOdbcConnection@c9ba38 [debit] [rollback] sun.jdbc.odbc.JdbcOdbcConnection@c9ba38 Exception in thread "main" banking.InsufficientBalanceException: Total balance not sufficient ... the rest of call stack
Now we have our first AspectJ-based solution for transaction management. This solution works fine if you have only one subaspect of JDBCTransactionAspect. However, if you need to use multiple subaspects to separately specify operations that require transaction support, you may have to enhance this solution, as we describe next.
11.4 Improving the solution
In many situations, operations that must be managed within one transaction may belong to different subsystems, and it may be desirable for each subsystem to have its own aspect for specifying the pointcuts for the operations that need transaction support. That way, any changes to a subsystem require changing only the associated subaspect. In this case, we need to ensure that the transaction management for one aspect does not conflict with that of the other. Let s use two of the patterns from chapter 8, the participant pattern and the worker object creation pattern, to create a solution that meets this requirement. In our realization of the participant pattern, we will create a participant subaspect for each subsystem. We will then use the worker object creation pattern to ensure that the solution works correctly in all cases. As we review the example in the next two sections, keep in mind that
Copyright © OnBarcode.com . All rights reserved.