我有两个 mysql(AWS Aurora) 数据库表:
--------------------------------
| Table:deparment |
--------------------------------
--------------------------------
| id | dept_name |
--------------------------------
| d1 | dept1 |
--------------------------------
| d2 | dept2 |
--------------------------------
--------------------------------
-------------------------------------------------
| Table:employee |
-------------------------------------------------
-------------------------------------------------
| id | emp_name | dept_id |
-------------------------------------------------
| e1 | Emp 1 | d1 |
-------------------------------------------------
| e2 | Emp 2 | d1 |
-------------------------------------------------
| e3 | Emp 3 | d1 |
-------------------------------------------------
| e4 | Emp 4 | d2 |
-------------------------------------------------
业务逻辑:多线程可以对Depatment和Employee表进行创建/修改或删除操作。对员工表的任何修改都将导致更新员工 ID
------
Java Code with Spring Transaction:
/**
* Modify Employee
*/
@Transactional(isolation = REPEATABLE_READ)
public void modifyEmployeeByDeptarment(int deptId, int empId Update upd) {
Department dept = deptDB.getDeparmentForUpdate(deptId); // returns SELECT * FROM department where id = :deptId FOR UPDATE; Row level Lock on dept
Employee employee = empDB.getEmployee(deptId, employee.id); // returns SELECT * from employee where dept_id = :deptId;
employee.setId(newEmpId);
addUpdate(employee, upd); // This will update in DB
}
/**
* Delete department with employees
*/
@Transactional(isolation = REPEATABLE_READ)
public void deleteDepartment(int deptId) {
Department dept = deptDB.getDeparmentForUpdate(deptId); // returns SELECT * FROM department where id = :deptId FOR UPDATE; Row level Lock on dept
List<Employee> employees = empDB.getEmployeesByDept(deptId); // returns SELECT * from employee where dept_id = :depltId;
deleteDepartment(dept); // Deletes dept from DB
deleteEmployees(employees); // Delete all employee for deptId
}
------
正如您在此处看到的,对于这两个操作,我在对其相应的员工进行任何更改之前都锁定了部门行。但在少数情况下,我们观察到员工记录存在于我们的数据库中,但没有相应的部门。
有人可以解释这种行为吗?
Spring 框架版本:5.2.0.RELEASE Aurora MySQL 版本:5.7.12
* Note: I changed the isolation level to READ_COMMITED, and everything was working as expected.