直接上代碼
package course.service.impl; import course.entity.Course; import course.entity.CourseDetail; import course.mapper.CourseDetailMapper; import course.mapper.CourseMapper; import course.mapper.StudentMapper; import course.service.CourseService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import java.util.Objects;
@Service public class CourseServiceImpl implements CourseService { private Logger logger = LoggerFactory.getLogger(CourseServiceImpl.class); @Autowired StudentMapper studentMapper; @Autowired CourseMapper courseMapper; @Autowired CourseDetailMapper courseDetailMapper; /**使用for update一定要加上這個事務 * 當事務處理完后,for update才會將行級鎖解除*/ @Transactional(isolation = Isolation.READ_COMMITTED) // @Transactional(value = "testTransactionManager") //如果是多數據源,需要制定數據源 @Override public Object chooseCourse(String studentCode, Integer courseId) { /** courseMapper.queryAllById(courseId)會對所選中的那條記錄加行級鎖,其他線程會在此排隊,當事務提交后,才會進行解鎖*/ Course course = courseMapper.queryAllById(courseId); int electiveNum = course.getElectiveNum(); int totalNum = course.getElectiveTotal(); logger.info("After Lock Step 1, Thread: {},courseId{}, studentId: {}, electiveNum: {}, total: {}", Thread.currentThread(),courseId,studentCode, electiveNum, totalNum); if (Objects.isNull(course)){ return "課程不存在"; } if (electiveNum >= totalNum) { return "此課程已被選完"; } /**將此此學生的選課信息保存到選課詳情里面*/ CourseDetail courseDetail = new CourseDetail(); courseDetail.setCourseId(courseId); courseDetail.setStudentCode(studentCode); courseDetailMapper.insert(courseDetail); /**將course表中的electiveNum進行加1操作 * 使用sql進行累加更加安全,因為使用方法開始查詢的course中的electiveNum,並不一定是數據庫存儲的值*/ courseMapper.addElectiveNumByCourseId(courseId); return "選課成功"; } }
主要是Transactional注解的使用
READ_COMMITTED注解的話,無論條件列上是否有索引,都不會鎖表,只鎖行
Read Uncommited和Read Committed不存在間隙鎖所以不會存在鎖表行為
具體詳情可參考:
https://blog.csdn.net/zc_ad/article/details/83582614
https://blog.51cto.com/14230003/2427994