就是要产生的id不重复是吧!这个也不是同步问题呀!如果是id不重复,是要通过事务

解决方案 »

  1.   

    假设有一个保存ID值的表
    简单的,一个字段,一条记录,int型
    (当然,如果最终的ID值是int型,可以设定自增长字段)
    每当取值一次后,字段值+1(update table set no=no+1)
    该取值和更新由一个产生ID的存储过程完成现在的问题是:怎样保证多线程环境下,取值唯一(同步,任何时候只有一个存储过程调用该产生ID的存储过程)
      

  2.   

    这样解释吧
    table1(cid char(15)....)
    table2(ino int)存储过程:cp_gen_id
    产生id值,逻辑大概如下:
    select @ino=ino from table2
    update table2 set ino=ino+1
    return @ino存储过程:cp_table1_add
    完成记录添加, 当然需要调用cp_gen_id 获得一个id值
    然后真正完成table1的记录添加问题是,当多个线程并发调用cp_table1_add时,怎样保证(select @ino=ino from table2)取得的是合适的值,这必然需要同步,或者说,保证
    select @ino=ino from table2
    update table2 set ino=ino+1
    这两个语句一起执行,逻辑上不被中断
      

  3.   

    我的理解是,使用lock
    请教 vivianfdlpw() 
    事务可以保证一个事务中的读写操作一块儿执行吗?特别是“读”操作
    如上例:
    在调用 cp_gen_id时
    不会有另一个线程也调用cp_gen_id,使得
    select @ino=ino from table2
    update table2 set ino=ino+1
    这两个语句逻辑上被中断,这样Update是没有问题的,该加的迟早会加
    但是select 可能读取重复的值,有这种可能吗?
      

  4.   

    TransactionsA transaction is a sequence of operations performed as a single logical unit of work. A logical unit of work must exhibit four properties, called the ACID (Atomicity, Consistency, Isolation, and Durability) properties, to qualify as a transaction: Atomicity 
    A transaction must be an atomic unit of work; either all of its data modifications are performed, or none of them is performed. Consistency 
    When completed, a transaction must leave all data in a consistent state. In a relational database, all rules must be applied to the transaction’s modifications to maintain all data integrity. All internal data structures, such as B-tree indexes or doubly linked lists, must be correct at the end of the transaction. <font color=red>Isolation 
    Modifications made by concurrent transactions must be isolated from the modifications made by any other concurrent transactions. A transaction either sees data in the state it was in before another concurrent transaction modified it, or it sees the data after the second transaction has completed, but it does not see an intermediate state. This is referred to as serializability because it results in the ability to reload the starting data and replay a series of transactions to end up with the data in the same state it was in after the original transactions were performed. </font>Durability 
    After a transaction has completed, its effects are permanently in place in the system. The modifications persist even in the event of a system failure. 从上面的解释,看不出事务对于“读”操作有任何保证呀。
      

  5.   

    SET TRANSACTION ISOLATION LEVEL
    控制由连接发出的所有 Microsoft&reg; SQL Server&#8482; SELECT 语句的默认事务锁定行为。语法
    SET TRANSACTION ISOLATION LEVEL
        { READ COMMITTED
            | READ UNCOMMITTED
            | REPEATABLE READ
            | SERIALIZABLE 
        }参数
    READ COMMITTED指定在读取数据时控制共享锁以避免脏读,但数据可在事务结束前更改,从而产生不可重复读取或幻像数据。该选项是 SQL Server 的默认值。READ UNCOMMITTED执行脏读或 0 级隔离锁定,这表示不发出共享锁,也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。REPEATABLE READ锁定查询中使用的所有数据以防止其他用户更新数据,但是其他用户可以将新的幻像行插入数据集,且幻像行包括在当前事务的后续读取中。因为并发低于默认隔离级别,所以应只在必要时才使用该选项。SERIALIZABLE在数据集上放置一个范围锁,以防止其他用户在事务完成之前更新数据集或将行插入数据集内。这是四个隔离级别中限制最大的级别。因为并发级别较低,所以应只在必要时才使用该选项。该选项的作用与在事务内所有 SELECT 语句中的所有表上设置 HOLDLOCK 相同。
      

  6.   

    不知除了设置事务隔离级别以外
    还有没有别的办法,按照联机手册上的说法:
    A lower isolation level increases concurrency but at the expense of data correctness. Conversely, a higher isolation level ensures that data is correct, but can negatively affect concurrency
    高级别的隔离对于并发性能有较大的副作用
    请教vivianfdlpw() ,把设置事务隔离级别与表锁定相比,哪一种的开销更小呢?我在犹豫:
    因为数据库是一台机器(如果在这上面解决问题,比较好)
    服务程序运行在N台机器上,如果是一台机器,可以在服务程序中解决,无需SQL Server费事,因为 SQL Server本来负担就很重当然,可以写一个小的服务程序,运行在别的机器上,负责同步产生各种ID,但是这样不仅麻烦,也降低了系统的健壮性