Tuesday, December 21, 2021

Distributed Transactions

What is a distributed transaction? 

Transactions that span over multiple physical systems or computers over the network, are simply termed Distributed Transactions. In the world of microservices a transaction is now distributed to multiple services that are called in a sequence to complete the entire transaction. 

Here is a monolithic e-commerce system using transactions:




Here is the e-commerce system decomposed as microservices:



Two approaches
  • Two-Phase Commit
  • Eventual Consistency and Compensation / SAGA

1. Two-Phase Commit

Handling transactions in two stages, a prepare phase and a commit phase. One important participant is the Transaction Coordinator which maintains the lifecycle of the transaction.



Successful Two Phase commit on Microservices

Failed Two Phase commit on Microservices



2. Eventual Consistency and Compensation / SAGA

Each service publishes an event whenever it updates its data. Other service subscribe to events. When an event is received, a service updates its data.


Eventual Consistency / SAGA, success scenario



Eventual Consistency / SAGA, failure scenario







Thursday, July 21, 2016

Java version history from 1.4 to 1.8




1) Java 4 Features :
The important feature of J2SE 4 is assertions. It is used for testing.


  • Assertion (Java 4)




2) Java 5 Features :
The important features of J2SE 5 are generics and assertions. Others are autoboxing, enum, var-args, static import, for-each loop (enhanced for loop etc.


  • For-each loop (Java 5)
  • Varargs (Java 5)
  • Static Import (Java 5)
  • Autoboxing and Unboxing (Java 5)
  • Enum (Java 5)
  • Covariant Return Type (Java 5)
  • Annotation (Java 5)
  • Generics (Java 5)



Wednesday, October 29, 2014

Making Executable Jar File


Creating an executable JAR file. Here is the general procedure for creating an executable JAR:

1. Compile your java code (for ex: Hello.java), generating all of the program's class files.

2. Create a manifest file containing the following 2 lines: (named as Hello.mf)
Manifest-Version: 1.0
Main-Class: Hello  (name of the main class)

Note: The name of the file should end with the .mf suffix. It is important that the file ends with a blank line.

3. To create the JAR, open command prompt and type the following command:
jar cmf manifest-file jar-file input-files
Ex: jar cmf Hello.mf Hello.jar *.java *.class

Note: The input-files must include any class files, images, sounds, etc. that your program uses. Optionally, you can include the program's .java files in the JAR. See below for adding directories to  the JAR.

4. To view the contents of the JAR, type:
jar tf jar-file
Ex: jar tf Hello.jar

5. Execute the application from the command line by typing:
java -jar jar-file

Ex: java -jar Hello.jar

Monday, October 13, 2014

Try-with-resources in Java 7

Try-with-resources in Java 7

Try-with-resources in Java 7 is a new exception handling mechanism that makes it easier to correctly close resources that are used within a try-catch block.

Resource Management With Try-Catch-Finally, Old School Style 

Managing resources that need to be explicitly closed is somewhat tedious before Java 7.
private static void printFile() throws IOException {
    InputStream input = null;

    try {
        input = new FileInputStream("file.txt");

        int data = input.read();
        while(data != -1){
            System.out.print((char) data);
            data = input.read();
        }
    } finally {
        if(input != null){
            input.close();
        }
    }
}
The code marked in bold is where the code can throw an Exception. As you can see, that can happen in 3 places inside the try-block, and 1 place inside the finally-block.

The finally block is always executed no matter if an exception is thrown from the try block or not. That means, that the InputStream is closed no matter what happens in the try block. Or, attempted closed that is. TheInputStream's close() method may throw an exception too, if closing it fails.

Thursday, September 11, 2014

Method Overriding with Exception Handling

Method Overriding with Exception Handling

There are many rules if we talk about method overriding with exception handling. The Rules are as follows:
  • If the super class method does not declare an exception
    • If the super class method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
  • If the super class method declares an exception
    • If the super class method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception. 

Monday, June 09, 2014

What's New in Java 8 - Method References

Method references provide easy-to-read lambda expressions for methods that already have a name.
We have seen lambda expressions to crate anonymous methods in my post What's New in Java 8 - Lambda Expressions. Sometimes a lambda expression does nothing but call an existing method (will see example in detail). In those cases, we use method references instead lambda expressions. Method references are compact and easy to read lambda expressions for methods that already have a name.

Example  :

To better understand, consider the same car example discussed in my previous post, but here a extra compareByEngineCC static method.

public class Car{
    public enum Color {
        BLACK, WHITE, RED
    }
    public String modelName;
    public Color color;
    public int engineCC;
    public void printModelName() {
        // ...
    }
    
    public static int compareByEngineCC(Car c1, Car c2) {
        if( c1.engineCC  == c2.engineCC) return 0;
        return c1.engineCC > c2.engineCC?1:-1;
    }
}

The requirement is sort the array of cars by engineCC, so that I added a static method to  compare the cars.
We can use sort method which is static in Arrays class to meat this requirement, but in order to use this static method we need to provide Comparator, So we implemented a CarEngineCCComparator class as shown below

Thursday, May 01, 2014

What's New in Java 8 - Lambda Expressions

Introduction :

Lambda Expressions enable us to treat functionality as a method argument or code as data . Well, What is functionality as a method argument? Did we already use this before? Yes, the answer is yes, we already used this concept before with anonymous classes. In anonymous classes case, we are usually trying to pass functionality as an argument to another method. Lambda expressions are also to do this. Although  anonymous classe is often more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome.

Usage :

First let us  discuss the usage of  the Lambda Expressions then will go through the syntax of Lambda Expressions.Suppose that you are creating an application for car sellers. You are asked to create a feature that search cars by certain criteria.
Car class can be represented as follows.
public class Car{
    public enum Color {
        BLACK, WHITE, RED
    }
    public String modelName;
    public Color color;
    public int engineCC;
    public void printModelName() {
        // ...
    }
}
Now you need to implement a feature that search for cars that match one characteristic such as engine cc.One simplistic approach is  create a method that  searches for cars that match one characteristic, such as engine cc. The following method prints Model name that are greater than the specified engine CC.

Distributed Transactions

What is a distributed transaction?  Transactions that span over multiple physical systems or computers over the network, are simply termed D...