Senior
What interface is used for managing transactions in Spring?
sobes.tech AI
Answer from AI
In Spring, the PlatformTransactionManager interface is used for transaction management. It provides methods for beginning, committing, and rolling back transactions.
Example of usage:
@Autowired
private PlatformTransactionManager transactionManager;
public void executeInTransaction() {
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
// business logic
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
throw e;
}
}
Additionally, transaction management in Spring is most often implemented through the @Transactional annotation, which simplifies working with transactions by hiding the details of using PlatformTransactionManager.