我的想法:
用戶1:打開某個記錄, 把一特定字段設為一個唯一的值,如session_id,另一個字段為當前timestamp.
用戶2:可讀取這個記錄,
      若要寫這個記錄,則session_id是自己的話,可以寫,
    或timestamp己10分鍾,可以寫,並把session_id寫入。
    因為我們不可以確定用戶1任時走了沒有.
用戶1:session_id為自己的話,可以寫,否則,重讀並設定session_id及timestamp.

解决方案 »

  1.   

    flock
    (PHP 3>= 3.0.7, PHP 4 )flock -- Portable advisory file locking
    Description
    bool flock ( int fp, int operation [, int &wouldblock])PHP supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work). flock() operates on fp which must be an open file pointer. operation is one of the following values: To acquire a shared lock (reader), set operation to LOCK_SH (set to 1 prior to PHP 4.0.1). To acquire an exclusive lock (writer), set operation to LOCK_EX (set to 2 prior to PHP 4.0.1). To release a lock (shared or exclusive), set operation to LOCK_UN (set to 3 prior to PHP 4.0.1). If you don't want flock() to block while locking, add LOCK_NB (4 prior to PHP 4.0.1) to operation. flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows). The optional third argument is set to TRUE if the lock would block (EWOULDBLOCK errno condition) flock() returns TRUE on success and FALSE on error (e.g. when a lock could not be acquired). 注: Because flock() requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen()). 警告 
    flock() will not work on NFS and many other networked file systems. Check your operating system documentation for more details. On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance! flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users). --------------------------------
    来自PHP手册, flock函数. 我自己对它的几种方式也不是很理解. 但应该就是用这个了.
      

  2.   

    SELECT 语句中“加锁选项”的功能说明
    SQL Server提供了强大而完备的锁机制来帮助实现数据库系统的并发性和高性能。用户既能使用SQL Server的缺省设置也可以在select 语句中使用“加锁选项”来实现预期的效果。 本文介绍了SELECT语句中的各项“加锁选项”以及相应的功能说明。
    功能说明:  
    NOLOCK(不加锁) 此选项被选中时,SQL Server 在读取或修改数据时不加任何锁。 在这种情况下,用户有可能读取到未完成事务(Uncommited Transaction)或回滚(Roll Back)中的数据, 即所谓的“脏数据”。
    HOLDLOCK(保持锁) 此选项被选中时,SQL Server 会将此共享锁保持至整个事务结束,而不会在途中释放。
    UPDLOCK(修改锁) 此选项被选中时,SQL Server 在读取数据时使用修改锁来代替共享锁,并将此锁保持至整个事务或命令结束。使用此选项能够保证多个进程能同时读取数据但只有该进程能修改数据。
    TABLOCK(表锁) 此选项被选中时,SQL Server 将在整个表上置共享锁直至该命令结束。 这个选项保证其他进程只能读取而不能修改数据。
    PAGLOCK(页锁) 此选项为默认选项, 当被选中时,SQL Server 使用共享页锁。
    TABLOCKX(排它表锁) 此选项被选中时,SQL Server 将在整个表上置排它锁直至该命令或事务结束。这将防止其他进程读取或修改表中的数据。使用这些选项将使系统忽略原先在SET语句设定的事务隔离级别(Transaction Isolation Level)。 请查阅SQL Server 联机手册获取更多信息。
      

  3.   

    我要的是“UPDLOCK(修改锁) 此选项被选中时,SQL Server 在读取数据时使用修改锁来代替共享锁,并将此锁保持至整个事务或命令结束。使用此选项能够保证多个进程能同时读取数据但只有该进程能修改数据。”功能,能否把语句写给我
      

  4.   

    USE pubs
    GO
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    GO
    BEGIN TRANSACTION
    SELECT au_lname FROM authors WITH (NOLOCK)
    GO