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.
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.
- Atomicity – Either everything happens or none.
- Consistency – All the application state changes should be exactly same as what is desired following all the rules and constraints.
- Isolation – Within a transaction, different tasks can execute concurrently or in parallel, but to others, they are visible as a single unit.
- 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?
- 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.
- 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.
- Declarative transaction management: Spring support declarative transaction management
- 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?
- First of all, their exception handling model is cumbersome.
- And secondly, it is coupled with other things like JNDI. to start with.
8. What are different components of Spring transaction management?
- First thing to know is PlatformTransactionManager
- TransactionDefinition is other which defines:
- Isolation level
- Propagation method
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?
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 Level||Dirty Read?||Phantom Read?||Non-Repeatable Read?|
|DEFAULT||Same as datasource||Same as datasource||Same as datasource|
12. What are different transaction propagation levels?
- PROPAGATION_MANDATORY: If current transaction exists, supports it otherwise throws exception.
- PROPAGATION_NESTED: If current transaction exists, executes within that.
- PROPAGATION_NEVER: Throws exception if current transaction exists.
- PROPAGATION_NOT_SUPPORTED: Does not support transaction whether it exists or not.
- PROPAGATION_REQUIRED: Supports transaction, if current transaction doesn’t exists it creates a new one.
- PROPAGATION_REQUIRED_NEW: Supports transaction. Always creates a new transaction even if there is already a current transaction in existence.
- 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.