Spring Transaction Management Interview Questions Answers 2020

Today in this article, we will go through our compiled list of top Spring transaction management interview questions for your Java interview.

Current article is part of our series on Spring Interview Questions.

Few starting questions are generic in nature but are very important. Later on we have Spring specific questions.

So, let’s start.

spring transaction management interview questions and answers

1. Have you worked on transaction management in your project?

YES. I have worked on application level transactions and by large understand how transaction works.

2. What is a transaction?

Transaction is a logical unit of work which either must be executed completely or must be rolled back completely.

Basically, whatever should be done as part of a transaction, should not be done partially.

3. Could you give me an example of a transaction?

Sure. The most common example of transaction is “Fund Transfer”.

If we are transferring money from our account to someone else’s, then debit from our account and credit of other person’s account, BOTH must happen as part of one transaction.

If not, there will be a mismatch in the account books and worst possible user experience.

Either, both tasks should get completed or none.

4. What transaction did you do in your project?

If you have done some work on transaction management, then it should not be an issue, BUT, if you have not:

Just prepare an example in advance on similar lines as above example BUT in the terminology of your project.

5. What are different characteristics of a transaction?

Note: Remember the acronym ACID.

  1. Atomicity – Either everything happens or none.
  2. Consistency – All the application state changes should be exactly same as what is desired following all the rules and constraints.
  3. Isolation – Within a transaction, different tasks can execute concurrently or in parallel, but to others, they are visible as a single unit.
  4. Durable – Once application changes are done in the database, it should survive any crashes or failure.

6. Why should I use Spring transaction management? What are its benefits?

  1. Consistent: We might be using different technologies in our application like JDBC or Hibernate, JPA etc. Each of these provide their ways to support transaction management. BUT code of Hibernate transaction is different than JPA. Here, we can use Spring as a global transaction manager for all these technologies without any issue of code portability.
  2. Easier and Simpler: Typically, global transactions are handled by Java Transaction API (JTA), but that is very difficult to use. Spring API is simpler and easy to use comparatively.
  3. Declarative transaction management: Spring support declarative transaction management
  4. Full integration with Spring’s data access: If you are already using Spring data access like JPA, then Spring transaction management works seamlessly with it.

7. Why do you say Java transaction API is difficult to use?

  1. First of all, their exception handling model is cumbersome.
  2. And secondly, it is coupled with other things like JNDI. to start with.

8. What are different components of Spring transaction management?

  1. First thing to know is PlatformTransactionManager
  2. TransactionDefinition is other which defines:
    1. Isolation level
    2. Propagation method
    3. Time-out
    4. Read-only
  3. TransactionException

9. What does Isolation means in terms of Spring?

Defining an Isolation in Spring means: to what degree a transaction in-progress is visible to another transactions in the system.

Will other transaction be able to see uncommitted state or only after transaction is done.

10. What are different isolation levels in Spring transaction?

  1. DEFAULT
  2. READ_COMMITTED
  3. READ_UNCOMMITTED
  4. REPEATABLE_READ
  5. SERIALIZABLE

11. What is the difference between different Spring transaction isolation levels?

Isolation levels differs in terms of: Dirty Read, Phantom Read and non-repeatable Read.

Isolation LevelDirty Read?Phantom Read?Non-Repeatable Read?
DEFAULTSame as datasource Same as datasource Same as datasource
READ_COMMITTED NoMightMight
READ_UNCOMMITTEDMightMightMight
REPEATABLE_READNoMightNo
SERIALIZABLENoNoNo

12. What are different transaction propagation levels?

  1. PROPAGATION_MANDATORY: If current transaction exists, supports it otherwise throws exception.
  2. PROPAGATION_NESTED: If current transaction exists, executes within that.
  3. PROPAGATION_NEVER: Throws exception if current transaction exists.
  4. PROPAGATION_NOT_SUPPORTED: Does not support transaction whether it exists or not.
  5. PROPAGATION_REQUIRED: Supports transaction, if current transaction doesn’t exists it creates a new one.
  6. PROPAGATION_REQUIRED_NEW: Supports transaction. Always creates a new transaction even if there is already a current transaction in existence.
  7. PROPAGATION_SUPPORTS: If transaction exists, supports it. If not, doesn’t becomes part of the transaction.

With this we have finished with our Spring interview questions on transaction management.

Leave a Comment