I want to actually understand the difference between Propagation.NESTED
VS
Propagation.REQUIRES_NEW
.. My requirement is to save the updateStudent
and updateSchool
and to rollback the updateBus
method where i made runtime exception purposefully. so i have used Propagation.NESTED
and tested, which works fine, but the result is same with or without Propagation.NESTED
in updateBus
method, so what is the purpose of NESTED here.
On other note when i change my code like this without updateStudentInner
method, entire transaction is roll backed. So here i don't understand the difference in keeping the Transactional level in updateStudentInner
vs updateStudentDetail
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStudentDetail(String name, int id) {
updateStudent(name,id);
updateSchool(name, id);
updateBus(name, id);
}
Here is the entire code for this
package com.example.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ExampleDaoImpl {
@Autowired
JdbcTemplate jdbcTemplate;
public void updateStudentDetail(String name, int id) {
updateStudentInner(name, id);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStudentInner(String name, int id) {
updateStudent(name, id);
updateSchool(name, id);
updateBus(name, id);
}
public void updateStudent(String name, int id) {
String query = "UPDATE STUDENT SET NAME=? WHERE ID=?";
name = "Name " + name;
int i = jdbcTemplate.update(query, name, id);
System.out.println("i updateStudent-->" + i);
}
public void updateSchool(String name, int id) {
name = "School " + name;
String query = "UPDATE SCHOOL SET NAME=? WHERE ID=?";
int i = jdbcTemplate.update(query, name, id);
System.out.println("i updateSchool-->" + i);
}
@Transactional(propagation = Propagation.NESTED)
public void updateBus(String name, int id) {
name = "Bus " + name;
String query = "UPDATE BUS SET AME=? WHERE ID=?";
int i = jdbcTemplate.update(query, name, id);
System.out.println("i updateBUs-->" + i);
}
}
Also it would be better is someone explain with realtime example of the difference between REQUIRE_NEW, REQUIRED, NESTED and where these to be used.