Search Tutorials


Spring Transaction Management Interview Questions | JavaInUse

Top Spring Transaction Management frequently asked interview questions.

In this post we will look at Spring Transaction Management Interview questions. Examples are provided with explanations.

Q: What are Database Transactions ?
A:
A Database transaction is a single logical unit of work which accesses and possibly modifies the contents of a database. It can be considered as a sequence of Queries that should be run in a sequence performing one logical function.

Database Transactions

Q: What are Application Transactions ?
A:
An application transaction is a sequence of application actions that are considered as a single logical unit by the application. For our application the joinOrganization method will be considered as one complete transaction. joinOrganization consists of two actions-
  • Persist Employee Information
  • Persist HealthInsurance Information

OrganizationService Exit

jsp:include page="newPagesmall.jsp" /> Q: What is Transaction Management ?
A:
A transaction manager is a part of an application that controls the coordination of transactions over one or more resources. The transaction manager is responsible for creating transaction objects and managing their durability and atomicity.

Q: How to implement Transactions Management in Spring Boot ?
A:
In Spring Boot Transaction Management is implemented using Transactional annotation. Transaction is a cross cutting concern and it is implemented using AOP in Spring Boot.
Transaction Management Concern
package com.javainuse.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
public class OrganzationServiceImpl implements OrganizationService {

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	@Transactional
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) {
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) {
			throw new RuntimeException("thowing exception to test transaction rollback");
		}
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	}

	@Override
	@Transactional
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) {
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
	}
}
Spring Boot implicitly creates a proxy for the transaction annotated methods. So for such methods the proxy acts like a wrapper which takes care of creating a transaction at the beginning of the method call and committing the transaction after the method is executed.
Transaction Management Proxy
The component that intercepts the @Transactional annotated method like the EmployeeService. Spring Boot Transaction Management Tutorials

Q: What are the different types of Transaction Propagations?
A:
The different types of Transaction Propagation for Spring Boot are as follows-
  • REQUIRED
  • SUPPORTS
  • NOT_SUPPORTED
  • REQUIRES_NEW
  • NEVER
  • MANDATORY
Spring Boot Transactions - Understanding Transaction Propagation

Q: How to handle Transactions for checked Exceptions ?
A:
We see that the employeeService transaction is not rolled back due to an exception in employeeHealthService.
Spring Boot Microservices Transaction without Rollback
In the Database we see that the insert for employee table has not been rolledback-
Spring Boot Microservices Transaction without Rollback database
But this should not be the case. To achieve roll back for checked exception we will need to specify it using Rollbackfor Annotation
Spring Boot Transactions - Understanding Transaction Rollbacks

Q: What are Transaction Isolation Levels ?
A:
The Transaction Isolation Levels are as follows-
  • READ_UNCOMMITTED
  • READ_COMMITTED
  • REPEATABLE_READ
  • SERIALIZABLE
  • DEFAULT

See Also

Top Java Data Structures and Algorithm Interview Questions
Elasticsearch Tutorial- Download and install Elasticsearch. Perform basic operations with Elasticsearch. Installing the Head Plugin for Elasticsearch. Understanding Elasticsearch Cluster, Node, Index and Document using example. Elasticsearch-Main Menu.