#906 Better way to handle the lock in order to prevent duplicates in the numbering of Tickets. Note that the iTopMutex now supports re-entrancy inside the same PHP page.

SVN:trunk[3230]
This commit is contained in:
Denis Flaven
2014-06-27 13:45:48 +00:00
parent ec16c5f86f
commit 61b5b5cc71
7 changed files with 41 additions and 82 deletions

View File

@@ -932,20 +932,6 @@
$this->Set('last_update', time()); $this->Set('last_update', time());
}]]></code> }]]></code>
</method> </method>
<method id="ComputeValues">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<code><![CDATA[ public function ComputeValues()
{
if ($this->IsNew())
{
$iKey = MetaModel::GetNextKey(get_class($this));
$sName = sprintf('C-%06d', $iKey);
$this->Set('ref', $sName);
}
}]]></code>
</method>
<method id="GetIcon"> <method id="GetIcon">
<comment>/**&#13; <comment>/**&#13;
* Get the icon representing this object&#13; * Get the icon representing this object&#13;

View File

@@ -633,20 +633,6 @@
$this->Set('last_update', time()); $this->Set('last_update', time());
}]]></code> }]]></code>
</method> </method>
<method id="ComputeValues">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<code><![CDATA[ public function ComputeValues()
{
if ($this->IsNew())
{
$iKey = MetaModel::GetNextKey(get_class($this));
$sName = sprintf('C-%06d', $iKey);
$this->Set('ref', $sName);
}
}]]></code>
</method>
<method id="GetIcon"> <method id="GetIcon">
<comment>/**&#13; <comment>/**&#13;
* Get the icon representing this object&#13; * Get the icon representing this object&#13;

View File

@@ -1277,13 +1277,6 @@
// Compute the priority of the ticket // Compute the priority of the ticket
$this->Set('priority', $this->ComputePriority()); $this->Set('priority', $this->ComputePriority());
if ($this->IsNew())
{
$iKey = MetaModel::GetNextKey(get_class($this));
$sName = sprintf('I-%06d', $iKey);
$this->Set('ref', $sName);
}
return parent::ComputeValues(); return parent::ComputeValues();
}]]></code> }]]></code>
</method> </method>

View File

@@ -504,13 +504,6 @@
{ {
// Compute the priority of the ticket // Compute the priority of the ticket
$this->Set('priority', $this->ComputePriority()); $this->Set('priority', $this->ComputePriority());
if ($this->IsNew())
{
$iKey = MetaModel::GetNextKey(get_class($this));
$sName = sprintf('P-%06d', $iKey);
$this->Set('ref', $sName);
}
}]]></code> }]]></code>
</method> </method>
<method id="OnInsert"> <method id="OnInsert">

View File

@@ -1667,14 +1667,6 @@
// Compute the priority of the ticket // Compute the priority of the ticket
$this->Set('priority', $this->ComputePriority()); $this->Set('priority', $this->ComputePriority());
if ($this->IsNew())
{
// Object not yet in the Database
$iKey = MetaModel::GetNextKey(get_class($this));
$sName = sprintf('R-%06d', $iKey);
$this->Set('ref', $sName);
}
return parent::ComputeValues(); return parent::ComputeValues();
}]]></code> }]]></code>
</method> </method>

View File

@@ -1653,13 +1653,6 @@
// Compute the priority of the ticket // Compute the priority of the ticket
$this->Set('priority', $this->ComputePriority()); $this->Set('priority', $this->ComputePriority());
if ($this->IsNew())
{
$iKey = MetaModel::GetNextKey(get_class($this));
$sName = sprintf('R-%06d', $iKey);
$this->Set('ref', $sName);
}
return parent::ComputeValues(); return parent::ComputeValues();
}]]></code> }]]></code>
</method> </method>

View File

@@ -48,7 +48,7 @@
<field id="ref" xsi:type="AttributeString"> <field id="ref" xsi:type="AttributeString">
<sql>ref</sql> <sql>ref</sql>
<default_value/> <default_value/>
<is_null_allowed>false</is_null_allowed> <is_null_allowed>true</is_null_allowed>
</field> </field>
<field id="org_id" xsi:type="AttributeExternalKey"> <field id="org_id" xsi:type="AttributeExternalKey">
<sql>org_id</sql> <sql>org_id</sql>
@@ -164,36 +164,52 @@
<static>false</static> <static>false</static>
<access>public</access> <access>public</access>
<type>Overload-DBObject</type> <type>Overload-DBObject</type>
<code><![CDATA[ public function DBInsertNoReload() <code><![CDATA[
public function DBInsertNoReload()
{ {
$oMutex = new iTopMutex('ticket_insert'); $oMutex = new iTopMutex('ticket_insert');
$oMutex->Lock(); $oMutex->Lock();
$iNextId = MetaModel::GetNextKey(get_class($this));
$sRef = $this->MakeTicketRef($iNextId);
$this->Set('ref', $sRef);
$iKey = parent::DBInsertNoReload(); $iKey = parent::DBInsertNoReload();
$oMutex->Unlock(); $oMutex->Unlock();
return $iKey; return $iKey;
} }
]]></code> ]]></code>
</method> </method>
<method id="DBInsertTracked_Internal"> <method id="MakeTicketRef">
<static>false</static> <static>false</static>
<access>protected</access> <access>protected</access>
<type>Overload-DBObject</type> <type>Overload-DBObject</type>
<code><![CDATA[ protected function DBInsertTracked_Internal($bDoNotReload = false) <code><![CDATA[
protected function MakeTicketRef($iNextId)
{ {
// Beware !!! switch(get_class($this))
// Compensate the fact that CMDBObject::DBInsertTracked_Internal does NOT call the derived version of DBInsertNoReload
// when performing an INsert with "no reload" but actually calls it (followed by Reload) when doing an Insert with reload !!
if ($bDoNotReload)
{ {
$oMutex = new iTopMutex('ticket_insert'); case 'UserRequest':
$oMutex->Lock(); $sFormat = 'R-%06d';
break;
case 'Incident':
$sFormat = 'I-%06d';
break;
case 'Change':
case 'RoutineChange':
case 'EmergencyChange':
case 'NormalChange':
$sFormat = 'C-%06d';
break;
case 'Problem':
$sFormat = 'P-%06d';
break;
default:
$sFormat = 'T-%06d';
} }
$ret = parent::DBInsertTracked_Internal($bDoNotReload); return sprintf($sFormat, $iNextId);
if ($bDoNotReload)
{
$oMutex->Unlock();
}
return $ret;
} }
]]></code> ]]></code>
</method> </method>