请教并发写入数据库,怎么保持唯一性

WBOY
Release: 2016-06-13 11:54:24
Original
1428 people have browsed it

请问并发写入数据库,如何保持唯一性啊

<br />//a,b两个请求并发  注册相同用户名 username='myname'<br />$is_user = "SELECT username FROM users WHERE username='myname'";<br />if(!$is_user)<br />{<br />	echo ("INSERT INTO users (username) VALUES ('myname')");<br />}<br />
Copy after login


假如表中字段未设置唯一索引,程序上如何控制唯一性啊
a,b同时查询表,结果是可以注册的,所以都执行了insert,但用户名相同,这样数据就不唯一了。是会这样吗,如何避免呢?


------解决方案--------------------
数据库加锁操作
------解决方案--------------------
怎么不加个unique key呢。
------解决方案--------------------
最简单的方法数据库加unique key

复杂一点:
单台web机器,使用本地文件锁flock,一个用户名一个本地文件
多台机器 ,使用memcache inc 实现锁机制 http://ju.outofmemory.cn/entry/48924

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!