Sobes.tech
Junior — Middle

How do you implement transaction management in the system?

sobes.tech AI

Answer from AI

In Java, transaction management is usually implemented using transaction APIs, for example, through Spring Framework or JTA. In Spring, you can use the @Transactional annotation for automatic management of transactions: start, commit, or rollback on exceptions.

Example using Spring:

@Service
public class UserService {

    @Transactional
    public void updateUser(User user) {
        userRepository.save(user);
        // if an exception occurs here, the transaction will rollback
    }
}

If you do not use frameworks, you can manage transactions manually via JDBC by calling methods like connection.setAutoCommit(false), commit(), and rollback().

Thus, transaction management ensures data integrity and consistency of operations in the system.

How do you implement transaction management in the… - sobes.tech