Saturday, 28 September 2019

User login process

User login process


Unix booted.

Program Unix(kernel) is booted into main memory, and 
remains active till the computer is shut down

Program init runs as a background task and remains 
running till shutdown

 User attempts to log in.

 Kernel calls program ‘init’.

 init’ scans file /etc/inittab , which lists the ports with 
terminals and their characteristics and returns an 
active open terminal to ‘init’.

 init calls program ‘getty’, which issues a login 
prompt in the monitor

 User enters login and password

getty’ calls program ‘login which scans file /etc/passwd to match username and password

After validation, control passes to session startup program /bin/sh , session startup program

Program /bin/sh reads file /etc/profile and .profile and sets up system wide and user specific environment.

User gets a shell prompt

Shell

Shell


Human interface point for Unix
Program layer – provides an environment for the user to enter commands to get desired results.
Korn Shell, Bourne Shell, C Shell are various shells used by Unix users



Unix Operating System

Unix Operating System
In UNIX, the operating system is broken into three pieces: the kernel, the shell, and the built-in utilities. The kernel is responsible for low level hardware communication, the shell provides human users with a user-friendly interface, and the built-in utilities provide basic tools for doing work.

Kernel
Heart of The Unix OS.
Collection of C programs directly communicating with hardware
Part of Unix system loaded into memory when Unix is booted

Manages:-
1.System resources
2.Allocates time between user and processes
3.Decides process priorities


Unix basic

Course CONTENTS

üUnix Operating System   
                      
üKernel

üShell

üUser login process

üBasic Unix commands

üFile management

üDirectory management

üPermission

üFile/Text processing commands 

üVI editor

üPattern searching


üSearching and Archiving

üProcess management

üJob Scheduling

üDisk usage commands

üArchitecture of Unix File system

üMailing commands/Utilities

üNetworking commands

üPrinting commands

üArithmetic operations

üSome useful commands

üMisc commands

Spring


Core & spring interview questions



MVC

                                                                         

spring boot Creating Config Server



Spring Boot Interview Questions



Spring and Rest




                

Security



Transaction Management 


compare Programmatic and Declarative Transaction Management:




Effective Spring Transaction Management





Wednesday, 25 September 2019

compare Programmatic and Declarative Transaction Management:

Programmatic Vs Declarative Transaction Management


  • Programmatic transaction management is good only if you have a small number of transactional operations. (Most of the times, this is not the case.)
  • Transaction name can be explicitly set only using Programmatic transaction management.
  • Programmatic transaction management should be used when you want explicit control over managing transactions.
  • On the other hand, if your application has numerous transactional operations, declarative transaction management is worthwhile.
  • Declarative Transaction management keeps transaction management out of businesslogic,and is not difficult to configure.

Sunday, 22 September 2019

Effective Spring Transaction Management

an important aspect of transaction management is defining the right transaction body
when transaction should start, when end, when data to be committed in data base and when transaction should be rolled back (in exception).

how to implement transaction management in application.

we can manage transaction either by :


1. Programmatically by writing our own custom code

2.Use Spring to manage the transaction



1. Programmatically by writing our own custom code


  this is the legacy way for managing transactions.

EntityManagerFactory factory = Persistence.createEntityManagerFactory("name");                                       
EntityManager entityManager = factory.createEntityManager();                   
Transaction transaction = entityManager.getTransaction()                  
try                                       
{  
   transaction.begin();                   
   //write some business logic here                    
   transaction.commit();  
}                  
catch(Exception ex)                   
{                     
   transaction.rollback();  
   throw ex;                  
}


Pros:
  • The scope of the transaction is very clear in the code.
Cons:
  • It's repetitive and could be error-prone.
  • In this transactions any error cause a very high impact.
  • needed to be write a lot of boilerplate code ,you needed to manage your code if you want to write another method.

2. Use Spring to manage the transaction

Spring supports two types of transaction management:


  1. Programmatic transaction management:  manage the transaction with the help of programming. It gives you flexibility, but it is quite difficult to maintain.
  2.  Declarative transaction management: In this you separate transaction management from the business code. You are  only using annotations or XML-based configuration to manage the transactions.

Recommendation : Declarative transaction

1. Programmatic transaction management

The Spring Framework provides two means of programmatic transaction management.

a. Using the TransactionTemplate (Recommended by Spring Team):

Context xml file


<!-- Initialization for data source -->   
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">      
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>      
<property name="url" value="jdbc:mysql://localhost:3306/TEST"/>      
<property name="username" value="root"/>      
<property name="password" value="password"/>   
</bean>
<!-- Initialization for TransactionManager -->   
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
<property name="dataSource"  ref="dataSource" />  
</bean>
<!-- Definition for ServiceImpl bean -->   
<bean id="serviceImpl" class="com.service.ServiceImpl">    
<constructor-arg ref="transactionManager"/>   

</bean>

Service Class:
public class ServiceImpl implements Service
{        
  private final TransactionTemplate transactionTemplate;
  // use constructor-injection to supply the PlatformTransactionManager    
  public ServiceImpl(PlatformTransactionManager transactionManager)
  {     
this.transactionTemplate = new TransactionTemplate(transactionManager);   
  }
  // the transaction settings can be set here explicitly if so desired hence better control
  //This can also be done in xml file                  
  this.transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);                   this.transactionTemplate.setTimeout(30); // 30 seconds       
  ...
  public Object someServiceMethod()
  {        
    return transactionTemplate.execute(new TransactionCallback() 
    {
     // the code in this method executes in a transactional context           
     public Object doInTransaction(TransactionStatus status)
     {                
     updateOperation1();     
        return resultOfUpdateOperation2();    
     }
   });   

}}


b. Using a PlatformTransactionManager implementation directly:


public class ServiceImpl implements Service
{    
 private PlatformTransactionManager transactionManager;
 public void setTransactionManager( PlatformTransactionManager transactionManager)
 {    
   this.transactionManager = transactionManager;  
 }
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// explicitly setting the transaction name is something that can only be done programmatically
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try 
{    
// execute your business logic here
}
catch (Exception ex)
{    
txManager.rollback(status);   
throw ex;
}
txManager.commit(status);

}

Choosing between Programmatic and Declarative Transaction Management:


  • Use Programmatic transaction management if you have a small number of transnational operations. (not used mostly.)
  • Transaction name can be explicitly and can be set only by  using Programmatic transaction management.
  • Programmatic transaction management should be used when you want explicit control over managing transactions.
  • if your application has numerous transactional operations, declarative transaction management is worthwhile.
  • Declarative Transaction management keeps transaction management out of businesslogic, it is not difficult to configure.
2. Declarative Transaction (Usually used almost in all scenarios of any web application)
Step 1: Define a transaction manager in your Spring application context XML file.
<bean id="txManager" class="org.springframework.jdbc.
datasource.DataSourceTransactionManager"/>


<tx:annotation-driven transaction-manager="txManager"/>

Step 2: Turn on support for transaction annotations by adding below entry to your Spring application context XML file.
OR add @EnableTransactionManagement   to your configuration class as below:
@Configuration
@EnableTransactionManagement
public class AppConfig
{
 ...
}


Step 3: Add the @Transactional annotation to the Class (or method in a class) or Interface (or method in an interface).
<tx:annotation-driven proxy-target-class="true">
Default configuration: proxy-target-class="false"  
  • The @Transactional   annotation may be placed before an interface definition, a method on an interface, a class definition, or a public method on a class.
  • If you want some methods in the class (annotated with  @Transactional) to have different attributes settings like isolation or propagation level then put annotation at method level which will override class level attribute settings.
  • In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.  
Let us now understand different @Transactional   attributes.
@Transactional (isolation=Isolation.READ_COMMITTED)
  • The default is Isolation.DEFAULT
  • Most of the times, you will use default unless and until you have specific requirements.
  • Informs the transaction (tx) manager that the following isolation level should be used for the current tx. Should be set at the point from where the tx starts because we cannot change the isolation level after starting a tx.
other isolation- 
@Transactional(timeout=60)
Defaults to the default timeout of the underlying transaction system.
Informs the tx manager about the time duration to wait for an idle tx before a decision is taken to rollback non-responsive transactions.
@Transactional(propagation=Propagation.REQUIRED)
If not specified, the default propagational behavior is REQUIRED. 
Other options are  REQUIRES_NEW , MANDATORY  , SUPPORTS  , NOT_SUPPORTED  , NEVER  , and  NESTED .
REQUIRED
  • Indicates that the target method cannot run without an active tx. If a tx      has already been started before the invocation of this method, then it will continue in the same tx or a newtxwould begin soon as this method is called.    
REQUIRES_NEW
  • Indicates that a new tx has to start every time the target method is called.
  •  If already atxis going on, it will be suspended before starting a new one.
MANDATORY
  • Indicates that the target method requires an active tx to be running.
  •  If a tx is not going on, it will fail by throwing an exception.
SUPPORTS
  • Indicates that the target method can execute irrespective of a tx. If a tx is   running, it will participate in the same tx. If executed without a tx it will   still execute if no errors.
  • Methods which fetch data are the best candidates for this option.
NOT_SUPPORTED
  • Indicates that the target method doesn’t require the transaction context to be propagated.
  • Mostly those methods which run in a transaction but perform in-memory operations are the best candidates for this option.
NEVER
  • Indicates that the target method will raise an exception if executed in a transactional process.
  • This option is mostly not used in projects.
@Transactional (rollbackFor=Exception.class)
  • Default is rollbackFor=RunTimeException.class
  • In Spring, all API classes throw RuntimeException, which means if any method fails, the container will always rollback the ongoing transaction.
  • The problem is only with checked exceptions. So this option can be used to declaratively rollback a transaction if Checked Exception occurs.
@Transactional (noRollbackFor=IllegalStateException.class)
  • Indicates that a rollback should not be issued if the target method raises this exception.
Now the last but most important step in transaction management is the placement of @Transactional annotation. Most of the times, there is a confusion where should the annotation be placed: at Service layer or DAO layer?
@Transactional: Service or DAO Layer?
The Service is the best place for putting @Transactional, service layer should hold the detail-level use case behavior for a user interaction that would logically go in a transaction.
There are a lot of CRUD applications that don't have any significant business logic for them having a service layer that just passes data through between the controllers and data access objects is not useful. In these cases we can put transaction annotation on Dao.
So in practice, you can put them in either place, it's up to you.
Also if you put @Transactional   in DAO layer and if your DAO layer is getting reused by different services then it will be difficult to put it on DAO layer as different services may have different requirements.
If your service layer is retrieving objects using Hibernate and let's say you have lazy initializations in your domain object definition then you need to have a transaction open in service layer else you will face LazyInitializationException  thrown by the ORM.
Consider another example where your Service layer may call two different DAO methods to perform DB operations. If your first DAO operation failed, then the other two may be still passed and you will end up inconsistent DB state. Annotating a Service layer can save you from such situations.