Spring Transactions Flashcards

1
Q

On which layer should transaction be applied? Dao or data layer vs serivce or business logic layer?

A

Transactions should be applied on Service layer or Business logic layer.

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

Which annotation is used for specifying transactional?

A

@Transactional

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

How are transactions managed in spring technically?

A

ThreadLocal and AOP are used together to manage transactions in Spring.

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

If you have two databases is it possible to have two different transaction managers and provide detail in annotation as to which should be injected in Bean or Repository?

A

Yes it is possible to do that.
First we need to define two transaction managers with different qualifiers.

bean id=”tm1” class=”…JpaTransactionmanager”
property name=”entityManagerFactory” ref=”entityManagerFactory1”
qualifier value=”account”

bean id=”transactionManager2” class=”…JpaTransactionManager”
property name=”entityManagerFactory” ref=”entityManagerFactory2”
qualifier value=”businessData”

Then this qualifier can be used in annotation to specify which transaction manager should be used.

public class TransactionalService {

@Transactional("account")
public void doSomethingToAccount() {}

@Transactional(“businessData”)
public void doSomethingInBusinessData() {…}

}

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

Transaction management can be done in how many ways with Spring?

A

In two ways, declarative transaction management which is high level one, using annotation. Or Programmatic transaction management, which is more advanced and flexible.

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

By default Spring transaction management is single threaded or multi-threaded?

A

Spring transactions work well with single thread, as they heavily use ThreadLocal for handling transactions. Problem arises when need to manage transactions across multiple threads.

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

How does spring declarative transaction management work?

A

It uses AOP proxies and the transactional advice is driven by metadata. The combination of AOP with transactional metadata yields an AOP proxy that uses a TransactionInterceptor in conjunction with an appropriate PlatformTransactionManager implementation to drive transactions around method invocations.

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

Flow of declarative transaction management

A

Caller => AOP Proxy => Transaction Advisor (transaction created on way in and committed or rolled back on way out) => Custom Advisor (Custom interceptors may run before or after the transaction advisor) => Target method (Business logic) => Custom Advisor => Transaction Advisor => AOP Proxy => Caller

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