The Ultimate Guide to Spring Boot Transaction Management Interview Questions and Answers (2024) | TechGeekNext


The Ultimate Guide to Spring Boot Transaction Management Interview Questions and Answers (2024)

In this post, questions from Spring Boot Transaction Management Interviews will be answered for Experienced and Freshers. We're trying to share our experience and learn how to help you make progress in your career.

Spring Boot Transaction Management:

  1. Transaction Management Example
  2. Transaction Propagation
  3. Transaction Interview Questions

  1. What is database transaction?
  2. What is an Application Transaction?
  3. What is Spring Transaction?
  4. What are types of Spring transaction management?
  5. What is use of @transactional annotation in Spring?
  6. How does transaction management work in Spring?
  7. What is Transaction Propagation?
  8. What are different types of Transaction Propagation?

Q: What is database transaction?
Ans:

A database transaction is a sequence of actions that are treated as a single unit of work. Transaction management is an essential aspect of RDBMS application to ensure data integrity and consistency. The transaction can be defined with ACID properties.

  1. Atomicity - All success or none.
  2. Consistency - Integrity constraints must be maintained in such a way that the database is consistent before and after the transaction.
  3. Isolation - One transaction should not impact on another transaction.
  4. Durability - After completion of the transaction, the changes and modifications to the database will be stored in and written to the disk and will continue even if a system failure occurs.

Q: What is an Application Transaction?
Ans:

An application transaction is a sequence of actions or operations performed by the user or system considered as logical unit of work.

For Example: In below example, adding notes will be called as application transaction which performs different operations like registering user and creating, and which will be considered as one logical unit of work.

Checkout our related posts :

Q: What is Spring Transaction?
Ans:

A database transaction is a sequence of actions that are considered as a single unit of work. These actions will either be completed in full or take no effect at all. Transaction management is a crucial part of the RDBMS based enterprise application to maintain data integrity and consistency.

Q: What are types of Spring transaction management?
Ans:

There are 2 ways to achieve transaction management in Spring:

  1. Spring Programmatic Transaction Management

    With programmatic transactions, transaction management code needs to be explicitly written so as to commit when everything is successful and rolling back if anything goes wrong. The transaction management code is tightly bound to the business logic in this case.
    UserNoteTransaction userNoteTransaction = entityManager.getTransaction();
    try {
       //Begin Transaction
        userNoteTransaction.begin();
    
        /* register user - query 1
    	 create note
    	link note to user - query 2 */
    
        //Commit Transaction
        userNoteTransaction.commit();
    } catch(Exception exception) {
        //Rollback Transaction
        userNoteTransaction.rollback();
        throw exception;
    } 
  2. Spring Declarative Transaction Management

    Using @Transactional, the above code gets reduced to simply this:
    @Transactional
    public void addNoteToSpecificUser() {
        /* register user - query 1
    	 create note
    	 link note to user - query 2 */
    }

Q: What is use of @transactional annotation in Spring?
Ans:

@Transactional annotation will do all the magic we need to do in the programmatic way before to handle transaction management. Any bean's public method you annotate with the @Transactional annotation, will be executed within a transaction management.

package com.techgeeknext.service.impl;

import com.techgeeknext.modal.Notes;
import com.techgeeknext.modal.User;
import com.techgeeknext.service.NotesService;
import com.techgeeknext.service.UserNotesLinkService;
import com.techgeeknext.service.UserService;
import com.techgeeknext.util.ApplicationConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserNotesLinkServiceImpl implements UserNotesLinkService {
    @Autowired
    private UserService userService;

    @Autowired
    private NotesService notesService;

    @Override
    @Transactional
    public String addNoteToSpecificUser(User user, Notes note) throws Exception {
        //create new user
        User createdUser = userService.registerUser(user);
        Notes dbNote = new Notes();
        dbNote.setTitle(note.getTitle());
        dbNote.setMessage(note.getMessage());
        //set created user to note
        dbNote.setUserDetails(createdUser);
        //persist new note
        notesService.addNote(dbNote);
        return ApplicationConstants.ADDED_NOTE_DESC;
    }
}

Q: How does transaction management work in Spring?
Ans:

Spring Boot Transaction Management Props is a configuration class used to define options when creating an actor.

Transaction Management in Spring Boot is a cross cutting concern and it is implemented using AOP (same as Transaction Advice).

As shown below in diagram, Spring internally creates a proxy for the class or method annotated with @transaction annotation. A proxy allows to inject before,after and around methods calls into the object being proxied.

The generated proxy object is provided with a Transaction Interceptor, which is created by Spring. So when the @Transactional method is invoked from the client code, the Transaction Interceptor will first be invoked from the proxy object, which will start the transaction and eventually invoke the method on the target bean (i.e. your bean). When the invocation completed, the TransactionInterceptor commits/rollback the transaction properly.


Q: What is Transaction Propagation?
Ans:

There are many components/services involved in the enterprise application for any given request to get the job done. Some of these components/services mark the transaction boundary (start/end) that will be used in the corresponding component and its sub-components. For this transaction boundary of components/services, Transaction Propagation specifies whether or not the corresponding component will participate in the transaction and what happens if the calling component/service has or does not already have a transaction created/started.

In enterprise application, there are many services or components involved to compeleted the job/request. Transaction Propogation specifies whether or not the current component/service will participate in the transaction, and what if the calling component/service has or does not already have a transaction created/started.

Q: What are different types of Transaction Propagation?
Ans:

There are six types of Transaction Propagation. REQUIRED is Default Transaction Propagation.
To understand more, you can go through Transaction Propagation example

  • REQUIRED
  • SUPPORTS
  • NOT_SUPPORTED
  • REQUIRES_NEW
  • NEVER
  • MANDATORY








Recommendation for Top Popular Post :