Home Navigation

Monday 20 July 2020

Postgres docker volume backup and restore

They way we are going to backup and restore postgres database docker volume we will use docker exec to get into the container and will use pg_dump utility to achieve our goal.

Postgres volume backup:

start your postgres container using docker or docker-compose.
execute the below command to get the container id
$docker ps

Backup database: 
    $docker exec -u postgres <containerId> pg_dump -Fc -d <databaseName> > dabase-backup.dump

Restore database:
    $docker exec -u <postgresUser> <containerId > psql -c 'DROP DATABASE <databaseName>'

    $docker exec -i -u < postgresUser > <containerId> pg_restore --clean -C -d postgres < dabase-backup.dump


Backup Schema:
    $docker exec -u postgres <containerId> pg_dump -Fc -d <databaseName> -n <schemaName> > schema-db-backup.dump
Restore Schema:
    $docker exec -it -u postgres <containerId> psql 
you will be in interactive mode, in the prompt terminal execute the below commands
    postgres=# \connect bdd
    # drop schema <schemaName> cascade
    # create schema <schemaName>
    \q to quit the interactive mode postgres
    Then execute the below command to reload db
    $docker exec -i -u postgres <containerId> pg_restore --clean -C -d <databaseName> -n <schemaName> < schema-db-backup.dump
Last step, validate your data using a database browser or you can use the below command to get into the database terminal and use sql query to validate your data.

    $docker exec -it -u postgres <containerId> psql 

Friday 10 July 2020

Spring bean life cycle overview

Spring beans are components which are handled and managed by spring IoC container. When the beans get created it is required to perform some initialization to get the beans usable. And when beans are no longer required and gets removed from IoC needs some clean up.

We define beans in three ways
  • XML Config (Load xml definitions ) -> instantiation & constr injection -> property injection
  • Annotation config ( @Component scanning) -> instantiation & @Autowired on constructor -> injection of @Autowired methods & fields
  • Java Config ( Read @Bean method signature ) -> call @Bean method implementations

We can divide the life cycle of a bean as below

Callback Interfaces
  • InitializingBean.afterPropertiesSet() called after properties are set
  • DisposableBean.destroy() called during bean destruction in shutdown
Life Cycle Annotations
  • @PostConstruct annotated methods will be called after the bean has been constructed, but before its returned to the requesting object   
  • @PreDestroy is called just before the bean is destroyed by the container
Bean Post Processors
  • Gives you a means to tap into the Spring context life cycle and interact with beans as they are processed
  • postProcessBeforeInitialization - Called before bean initialization method
  • postProcessAfterInitialization - Called after bean initialization
‘Aware’ Interfaces
  • Spring has over 14 ‘Aware’ interfaces.
  • These are used to access the Spring Framework infrastructure
  • These are largely used within the framework
  • Rarely used by Spring developers


Note: BeanFactoryPostProcessor implementations are "called" during startup of the Spring context after all bean definitions will have been loaded while BeanPostProcessor are "called" when the Spring IoC container instantiates a bean.

BeanFactoryPostProcessor kicks in at the phase of the container life-cycle, when no bean has been created but the bean definition has already been parsed, while BeanPostProcessor comes into play after bean creation.

Thursday 9 July 2020

Spring mvc life cycle summary



  • First request lands to the different sort of filters and it applies to all request. eg, application provided filters, authentication filters etc
  • The DispatcherServlet consults with the HandlerMapping 
  • Handler Mapping resolves controller to invoke. Usually org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping is used. This class reads @RequestMapping annotation from the Controller and uses the method of Controller that matches with URL as Handler class.
  • Handler Adapter calls the appropriate controller. org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter is used. RequestMappingHandlerAdapter class calls the method of handler class (Controller) selected by HandlerMapping.
  • HandlerInterceptor to perform actions before handling, after handling or after completion (when the view is rendered) of a request.
  • Controller executes the business logic
  • Controller resolves the model and view to send to the user
  • The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke. ViewResolvers resolves the view according to the implementation and configuration of the view, normally JSP org.springframework.web.servlet.view.InternalResourceViewResolver is used, Some other view resolvers are FreeMarkerViewResolver, TilesViewResolver, ThymeleafViewResolver, BeanNameViewResolver etc
  • Now the DispatcherServlet will pass the model object to the View to render the result.