Thursday 25 March 2021

@Transaction management internals (Proxy pattern)

How exactly @Transaction works:

Earlier we used to start, close, rollback the transactions programmatically. But with the introduction of @Transaction annotation life became easy. All the underlying transaction management will he handled by the framework.

@Service

public class EmployeeService {

        @Transactional

    public void saveEmployee (Employee employee){

        dao.saveEmployee (employee);

    }

}

The underlying  transaction managers handle the transaction, whichever is used, for example: HibernateTransactionManager or JpaTransactionManager


Internally Spring creates a Proxy for the EmloyeeService class. This proxy class is the one which handles the transaction management related activates, including exception handling and rollback of the transaction








Java interface - Diamond Problem

 public interface InterfaceEx {


default void name(){} //default method
static void name6(){}
}

interface InterfaceEx1{
default void name(){} // default method
static void name6(){}
}

//If a class is implementing 2 or more interfaces, with same method signature, compiler enforces class to Override that method.
//This is how diamond problem is resolved.
class ClassEx implements InterfaceEx, InterfaceEx1{ //

@Override
public void name() {

}
}