这个SQL代码是否安全?

时间:2009-06-09 18:58:36

标签: sql concurrency

我很确定这段代码很好。我想知道你们对insertMediaTags函数(第二个函数)的看法。我担心的是下面的并发安全吗?如果insertMediaTags足够优化?请注意,由于第一个函数它处于事务中,但它也处于一个可能意味着它不好的循环中?

我对你们可能拥有的任何编码习惯,风格或建议持开放态度。 (我知道有人会问,我使用的是sqlite ATM但其原型代码与mysql或ms sql或其他版本一起使用)

    {
        long mediaId;
        //all the credentials should be verified by this point.
        command.CommandText = "SELECT mediaId FROM media " +
            "WHERE userId=@userId AND title=@title;";
        command.Parameters.Add("@userId", DbType.Int64).Value = m.userid;
        command.Parameters.Add("@title", DbType.String).Value = m.title;
        if (command.ExecuteScalar() != null)
            throw new System.InvalidOperationException("Title already exisit");

        using (var dbTrans = connection.BeginTransaction())
        {
            command.CommandText =
                "INSERT INTO " +
                "media ( userid,  catagory,  creation_date,  current_media_date,  current_desc_date,  licence,  title,  desc,  ext) " +
                "VALUES(@userid, @catagory, @creation_date, @current_media_date, @current_desc_date, @licence, @title, @desc, @ext); " +
                "SELECT last_insert_rowid() AS RecordID;";

            DateTime currentDate = m.creation_date;
            command.Parameters.Add("@userid", DbType.Int64).Value = m.userid;
            command.Parameters.Add("@catagory", DbType.Int64).Value = m.catagory;
            command.Parameters.Add("@creation_date", DbType.DateTime).Value = m.creation_date;
            command.Parameters.Add("@current_media_date", DbType.DateTime).Value = currentDate;
            command.Parameters.Add("@current_desc_date", DbType.DateTime).Value = currentDate;
            command.Parameters.Add("@licence", DbType.Int64).Value = m.license;
            command.Parameters.Add("@title", DbType.String).Value = m.title;
            command.Parameters.Add("@desc", DbType.String).Value = m.desc;
            command.Parameters.Add("@ext", DbType.Int64).Value = m.ext;

            mediaId = (long)command.ExecuteScalar();
            //m.collaborateWith
            insertInspired(inspireLinks.external, inspireLinks.internalPair, mediaId);
            insertDerived(deriveLinks.external, deriveLinks.internalPair, mediaId);
            insertMediaTags(m.listTagString, mediaId);
            //command.CommandText = "END TRANSACTION;";            command.ExecuteNonQuery();

            updateMediaForWatchers(m.userid, mediaId, m.catagory, currentDate);
            dbTrans.Commit();
        }
        return mediaId;
    }

    void insertMediaTags(List<string> tags, long mediaId)
    {
        foreach(string tag in tags)
        {
            //assure tag exist
            long tagId;
            command.CommandText = "SELECT tagid FROM tag_name WHERE title=@title;";
            command.Parameters.Add("@title", DbType.String).Value = tag;
            object o = command.ExecuteScalar();
            if (o == null)
            {
                command.CommandText =
                    "INSERT INTO tag_name(title) VALUES(@title); " +
                    "SELECT last_insert_rowid() AS RecordID;";
                command.Parameters.Add("@title", DbType.String).Value = tag;
                tagId = (long)command.ExecuteScalar();
            }
            else
                tagId = (long)o;

            command.CommandText =
                "INSERT INTO media_tags(mediaId, tagid) " +
                "VALUES(@mediaId, @tagid);";
            command.Parameters.Add("@mediaId", DbType.Int64).Value = mediaId;
            command.Parameters.Add("@tagid", DbType.Int64).Value = tagId;
            command.ExecuteNonQuery();

            command.CommandText =
                "UPDATE tag_name SET count = count+1 "+
                "WHERE tagid=@tagid";
            command.Parameters.Add("@tagid", DbType.Int64).Value = tagId;
            command.ExecuteNonQuery();
        }
    }

2 个答案:

答案 0 :(得分:4)

不,它不是并发安全的。 SELECT之间存在潜在的竞争条件以确定标记是否存在,如果不存在则INSERT创建标记。想象一下,线程A执行SELECT并发现它不存在,然后线程B在线程A执行INSERT之前执行相同的操作。线程B也将尝试插入并失败。

答案 1 :(得分:0)

在SQL Server中,最好使用SCOPE_IDENTITY()函数。除此之外我没有看到问题。