专业做汽车零部件平台的网站,wordpress子网页,广西 网站开发,模板app1、select for update 的基本语法
SELECT * FROM table_name WHERE condition FOR UPDATE;2、select for update 的定义及作用
2.1 、select for update的含义是在查询数据的同时对所选的数据行进行锁定#xff0c;以保证数据的一致性和并发控制。在并发环境下#xff0c;多…1、select for update 的基本语法
SELECT * FROM table_name WHERE condition FOR UPDATE;2、select for update 的定义及作用
2.1 、select for update的含义是在查询数据的同时对所选的数据行进行锁定以保证数据的一致性和并发控制。在并发环境下多个事务可能同时对同一数据进行读取和修改。如果不加任何锁机制就会出现脏数据的情况即一个事务读取了另一个事务尚未提交的数据导致数据不一致。
2.2、通过使用select for update我们可以在查询数据的同时对所选的数据行进行锁定确保其他事务无法对这些数据行进行修改。这种加锁机制可以有效地避免脏数据的问题保证数据的一致性。
3、基于select for update 实现数据库分布式锁
注意事项
select for update 必须在事务中才会生效。该语法适用于mysql的innodb 数据库引擎。
java实现
package com.monika.main.system.lock.impl;import com.monika.main.system.lock.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.beans.factory.InitializingBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;/*** author:whh* date: 2024-05-11 18:47* p/p*/Component
public class JdbcRowLock implements Lock,InitializingBean {private static final Logger LOG LoggerFactory.getLogger(JdbcRowLock.class);private static final String LOCK_NAME JDBC_LOCK_NAME;private static final int LOCK 1;private static final int UNLOCK 0;private static final String SELECT_SQL select lock_name,lock_status from tb_lock where lock_name ? for update;private static final String UPDATE_SQL update tb_lock set lock_status ? where lock_name ?;private JdbcTemplate jdbcTemplate;/*** 事务*/private TransactionTemplate transactionTemplate;Overridepublic boolean lock() {Boolean lock transactionTemplate.execute(action - {MapString, Object result jdbcTemplate.queryForMap(SELECT_SQL, LOCK_NAME);int lockStatus (int) result.get(lock_status);if (LOCK lockStatus) {LOG.info(线程{}获取锁[{}]失败当前锁已被占用, Thread.currentThread().getName(), LOCK_NAME);return false;}jdbcTemplate.update(UPDATE_SQL, ps - {ps.setInt(1, LOCK);ps.setString(2, LOCK_NAME);});LOG.info(线程{}获取锁[{}]成功, Thread.currentThread().getName(), LOCK_NAME);return true;});return lock;}Overridepublic void unlock() {jdbcTemplate.update(UPDATE_SQL, ps - {ps.setInt(1, UNLOCK);ps.setString(2, LOCK_NAME);});LOG.info(线程{}释放锁[{}]成功, Thread.currentThread().getName(), LOCK_NAME);}Overridepublic void interrupt() {jdbcTemplate.update(UPDATE_SQL, ps - {ps.setInt(1, UNLOCK);ps.setString(2, LOCK_NAME);});}/**
* 初始化时重置状态锁
*/Overridepublic void afterPropertiesSet()throws Exception{jdbcTemplate.update(UPDATE_SQL, ps - {ps.setInt(1, UNLOCK);ps.setString(2, LOCK_NAME);});}Autowiredpublic JdbcRowLock(JdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {this.jdbcTemplate jdbcTemplate;this.transactionTemplate transactionTemplate;}
} 文章转载自: http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.kxltf.cn.gov.cn.kxltf.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.swwpl.cn.gov.cn.swwpl.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.zyslyq.cn.gov.cn.zyslyq.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.spkw.cn.gov.cn.spkw.cn