Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Locking A Query?

    Greetings. I need some feedback on using cflock with a query. I essentially
    want to single thread just one piece of code (a query) on a page.

    The query is a SQL stored procedure that does this:
    1. Select an id from the next available record in the table.
    2. Update that record (to make it no longer available).
    3. Return the results of that record.

    This happens pretty quick, but if two users hit the page at exactly the same
    time, they can both get the same id. The update doesn't happen before the next
    user runs executes the first select statement. Obviously, that's no good.

    My questions: Would wrapping the query in cflock single-thread that code and
    eliminate this issue (by forcing the query to complete for one user before a
    second user can execute that query)?

    Are there better solutions? Should I instead be looking at the stored proc by
    changing the transaction isloation level?

    Oh, yeah ... we're running CFMX, SQL 8, IIS on Windows Server 2003.

    TIA.
    joe

    JoeD Guest

  2. Similar Questions and Discussions

    1. SQL Server Locking
      I have an application that allows users to select the next person in a table based on a callback time and an InUse bit field. It is critical that...
    2. Need Help with Locking
      I need help with understanding about locking. I have my DSN created as Application Variable . I want to lock a transaction in one of the module to...
    3. Query of Queries on query New type query
      In CF5 we have a page that creates a query, using queryNew and querySetCell and the like, we then used dbtype="query" and gave it's name so we could...
    4. File locking in XP
      The issue i'm experiencing may be more related to XP Pro than with Illustrator itself, but it only seems to be affecting Illustrator EPS files... ...
    5. DB2 locking UDF dll
      Meg wrote: Which version of db2 are you using? For v8, try set KEEP_FENCED to no (db2 update dbm cfg using KEEP_FENCED NO). This way, the fenced...
  3. #2

    Default Re: Locking A Query?

    Always is better put the DB operations in a stored procedure, the reasons are
    well known. Using CFLOCK is a solution but you are not really locking the DB,
    only a portion or CF code.

    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

    BEGIN TRANSACTION

    SELECT ..... (Select an id from the next available record in the table.)
    UPDATE ..... (Update that record (to make it no longer available).
    SELECT ...... (Return the results of that record.)

    COMMIT TRANSACTION

    Regards

    Sojovi Guest

  4. #3

    Default Re: Locking A Query?

    Thanks Sojovi -

    I was thinking this must be so. SQL warns of using serializable isloation
    because of performance hit, but it's most important to get it correct, even if
    performance has to suffer a bit.

    Is it safe to assume that modifying the isolation level in the stored proc is
    still more efficient than using cflock?

    jd

    JoeD Guest

  5. #4

    Default Re: Locking A Query?

    The point is : With CFLOCK you are locking the portion of CF code beetwen begin
    and end tag, but you are not really locking the DB, because if at the same time
    CF is processing the CFLOCK somebody using the SQL query analizer modifies that
    records involved, you'll have a consistency problem.

    But if you want to lock more code than only the SQL statements you'll need to
    use CFLOCK (besides SET ISOLATION LEVEL).

    With you SET ISOLATION LEVEL SERIALIZABLE you are sure that nobody can access
    the records involved in the opened transaction.

    Regards

    Regards

    Sojovi Guest

  6. #5

    Default Re: Locking A Query?

    Gotcha. That makes sense, because if I just set the isolation level to
    serializable without using cflock, I get deadlocks. I'm hoping that setting
    the isolation level to serializable in SP and locking the CF code with cflock,
    I'll get the results I hope for.

    Thanks for your help.

    best,
    joe


    JoeD Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139