#379 Transaction Rollback When Catching an Exception
What happens when the it() method is called if an error occurs:
-
In
insert()? -
In
update()?
@Service
class Service {
@Autowired
private AccountRepository repository;
public void it(Data data) {
try {
repository.doIt(data);
} catch (Exception e) {
// Log the error
System.err.println("Error in service method: " + e.getMessage());
// Additional error handling if necessary
}
}
}
@Repository
class AccountRepository {
@Transactional
public void doIt(Data data) throws Exception {
try {
insert();
update();
} catch (Exception e) {
// Log the error
System.err.println("Error during transaction: " + e.getMessage());
throw e; // Rethrow the exception
}
}
private void insert() {
// Data insertion logic
}
private void update() {
// Data update logic
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor