WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Advanced · question 46 of 100

What is the difference between programmatic and declarative transaction management in Spring?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

In Java Spring Framework, transaction management can be managed programmatically or declaratively.

Programmatic transaction management involves manually controlling the transaction behavior with the help of Spring’s transaction manager API. This approach gives developers more control over the transaction’s code flow and can facilitate cases where different transactions need to be managed in different ways. Programmatic transaction management often involves the use of try-catch-finally blocks to correctly handle transaction outcomes (i.e., committing transaction, rolling back transaction or leaving it open).

Here’s an example of programmatic transaction management in Spring:

@Autowired
private PlatformTransactionManager transactionManager;

@Transactional
public void performFoo() {
    TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
    try {
        //perform database operations
        transactionManager.commit(status);
    } catch (Exception e) {
        transactionManager.rollback(status);
    }
}

Declarative transaction management in Spring allows transaction management to be specified using XML or annotations, which describes the configuration of the transaction behavior. This approach offers more clarity and less clutter in the code, as the developer only needs to specify rules for transaction management instead of implementing it in code.

Here’s an example of declarative transaction management in Spring using annotations:

@Transactional
public void performFoo() {
    //perform database operations
}

In this example, any method marked with the ‘@Transactional‘ annotation is wrapped in a transaction that is started just before the method is invoked and either committed or rolled back when the method ends.

In conclusion, the difference between programmatic and declarative transaction management in Spring is that programmatic transaction management involves manually controlling the transaction behavior with code, while declarative transaction management uses annotations or XML configurations to specify the rules for transaction management. Both approaches have their merits and limitations, and the choice between the two depends on the specific requirements and constraints of the project at hand.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics