MDL-30026 improve session lock - mssql workaround for bug with scalar returned values
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Sun, 13 Nov 2011 18:06:44 +0000 (19:06 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Sun, 13 Nov 2011 18:06:44 +0000 (19:06 +0100)
lib/dml/mssql_native_moodle_database.php

index 90f5a2f..f1d3226 100644 (file)
@@ -1242,7 +1242,20 @@ s only returning name of SQL substring function, it now requires all parameters.
         $timeoutmilli = $timeout * 1000;
 
         $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
-        $sql = "sp_getapplock '$fullname', 'Exclusive', 'Session',  $timeoutmilli";
+        // There is one bug in PHP/freetds (both reproducible with mssql_query()
+        // and its mssql_init()/mssql_bind()/mssql_execute() alternative) for
+        // stored procedures, causing scalar results of the execution
+        // to be cast to boolean (true/fals). Here there is one
+        // workaround that forces the return of one recordset resource.
+        // $sql = "sp_getapplock '$fullname', 'Exclusive', 'Session',  $timeoutmilli";
+        $sql = "BEGIN
+                    DECLARE @result INT
+                    EXECUTE @result = sp_getapplock @Resource='$fullname',
+                                                    @LockMode='Exclusive',
+                                                    @LockOwner='Session',
+                                                    @LockTimeout='$timeoutmilli'
+                    SELECT @result
+                END";
         $this->query_start($sql, null, SQL_QUERY_AUX);
         $result = mssql_query($sql, $this->mssql);
         $this->query_end($result);