---
title: "利用数据库锁实现简单的防并发编程"
author: "liyebing"
date: "2011-06-22T23:27:13Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2011-06-22T23:27:13Z"
id: 1101719
url: "https://liyebing.iteye.com/blog/1101719"
---

大约有两类情况：

 

1、一个程序代码块同一时刻只允许一个线程或者用户进程访问。

  解决方案：单独创建一个用来锁表的表table1，利用

  select * from table1 where lockType=? for update 

  或者

  select * from table1 where lockType=? for update nowait

 

 

2、表table2中的数据行在一个代码块执行期间不允许其他代码对其进行修改

    select * from table2 where id=? for update

 

当然，这行锁表语句与该代码块在一个事务内

 

 

讲得不是太详细，大概意思表达了。