1. 程式人生 > 實用技巧 >mybatis整合資料庫鎖表

mybatis整合資料庫鎖表

直接上程式碼

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註解的使用@Transactional(isolation = Isolation.READ_COMMITTED)

選擇READ_COMMITTED註解的話,無論條件列上是否有索引,都不會鎖表,只鎖行

Read Uncommited和Read Committed不存在間隙鎖所以不會存在鎖錶行為


具體詳情可參考:
https://blog.csdn.net/zc_ad/article/details/83582614
https://blog.51cto.com/14230003/2427994