@Transactional Annotation in Spring Flashcards

1
Q
  1. What Is @Transactional?
  2. What does the annotation allow us to set?
A
  1. We can use @Transactional to wrap a method in a database transaction.
  2. It allows us to set propagation, isolation, timeout, read-only, and rollback conditions for our transaction. We can also specify the transaction manager.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the @Transactional annotation work, how is it implemented?

A

Spring creates a proxy or manipulates the class byte-code to manage the creation, commit and rollback of the transaction.

Spring will wrap some transaction management code around the invocation @Transactional method called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Transaction Propagation?

A

Propagation defines our business logic’s transaction boundary. Spring manages to start and pause a transaction according to our propagation setting.

Spring calls TransactionManager::getTransaction to get or create a transaction according to the propagation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the different types of Propagation?

A

REQUIRED Propagation
SUPPORTS Propagation
MANDATORY Propagation
NEVER Propagation
NOT_SUPPORTED Propagation
REQUIRES_NEW Propagation
NESTED Propagation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define REQUIRED Propagation for @Transactional

A

REQUIRED is the default propagation. Spring checks if there is an active transaction, and if nothing exists, it creates a new one. Otherwise, the business logic appends to the currently active transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define SUPPORTS Propagation for @Transactional

A

ForSUPPORTS, Spring first checks if an active transaction exists. If a transaction exists, then the existing transaction will be used. If there isn’t a transaction, it is executed non-transactional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define MANDATORY Propagation for @Transactional

A

When the propagation isMANDATORY, if there is an active transaction, then it will be used. If there isn’t an active transaction, then Spring throws an exception:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define NEVER Propagation for @Transactional

A

For transactional logic withNEVER propagation, Spring throws an exception if there’s an active transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define NOT_SUPPORTED Propagation for @Transactional

A

If a current transaction exists, first Spring suspends it, and then the business logic is executed without a transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define REQUIRES_NEW Propagation for @Transactional

A

When the propagation is REQUIRES_NEW, Spring suspends the current transaction if it exists, and then creates a new one:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define NESTED Propagation for @Transactional

A

For NESTED propagation, Spring checks if a transaction exists, and if so, it marks a save point. This means that if our business logic execution throws an exception, then the transaction rollbacks to this save point. If there’s no active transaction, it works like REQUIRED.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Transaction Isolation

A

Transaction Isolation describes how changes applied by concurrent transactions are visible to each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the different types of Transaction Isolation

A
  • DEFAULT
  • READ_UNCOMMITTED
  • READ_COMMITTED
  • REPEATABLE_READ
  • SERIALIZABLE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What could the concurrency side effects of a transaction be?

A
  • Dirty read:read the uncommitted change of a concurrent transaction
  • Nonrepeatable read: get different value on re-read of a row if a concurrent transaction updates the same row and commits
  • Phantom read:get different rows after re-execution of a range query if another transaction adds or removes some rows in the range and commits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

@Transactional DEFAULT Isolation

A

The default isolation level isDEFAULT. As a result, when Spring creates a new transaction, the isolation level will be the default isolation of our RDBMS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

@Transactional READ_UNCOMMITTED Isolation

A

READ_UNCOMMITTEDis the lowest isolation level and allows for the most concurrent access.

As a result, it suffers from all three mentioned concurrency side effects. A transaction with this isolation reads uncommitted data of other concurrent transactions. Also, both non-repeatable and phantom reads can happen. Thus we can get a different result on re-read of a row or re-execution of a range query.

17
Q

@Transactional READ_COMMITTED Isolation

A

READ_COMMITTED prevents dirty reads.

The rest of the concurrency side effects could still happen. So uncommitted changes in concurrent transactions have no impact on us, but if a transaction commits its changes, our result could change by re-querying.

READ_COMMITTEDis the default level with Postgres, SQL Server, and Oracle.

18
Q

@Transactional READ_COMMITTED Isolation

A

REPEATABLE_READ,prevents dirty, and non-repeatable reads. So we are not affected by uncommitted changes in concurrent transactions.
Moreover, it is the lowest required level to prevent the lost update.

19
Q

What does the “lost update” mean?

A

The lost update occurs when two or more concurrent transactions read and update the same row.