Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Table locking causing problems

 
   Database Help (Home) -> Programming RSS
Next:  SQL Server 2005 - filegroup backup/restore proble..  
Author Message
Saga

External


Since: Mar 18, 2009
Posts: 2



(Msg. 1) Posted: Wed Mar 18, 2009 12:28 pm
Post subject: Table locking causing problems
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have to fix a possible locking problem with an existing app.

The overall process consists of getting a base 36 number from a table to use
as a
product id, then increment it by one and save the new number back to the
table.

The table has only one column and one row, containing the next available
product
id. The select statement is trivial:

select CurProId from ProdIdTable

The record set is declared local to the procedure:

dim rsGetProdNum as ADODB.Recordset
dim NextProdId as String

The app is using ADO 2.7 and the code to open the record set is:

Set rsGetProdNum = New ADODB.Recordset
rsGetProdNum.CursorLocation = adUseServer
rsGetProdNum.Open strSQL, dbADOConx, adOpenDynamic, adLockPessimistic

There is a validation for the results (sort of):

With rsGetProdNum
If .EOF = False Then
NewProdNum = Trim(!CurProId)
Else
MsgBox "INVALID PRODUCT NUMBER CALL COMPUTER DEPT!!!", vbOKOnly
Exit Function
End If
End With

The logic then proceeds to calculate the next product id and then updates
and
closes the table:

(logic to calculate next prod id snipped, resulting id in NextProdId
variable)

rsGetProdNum!CurProId = NextProdId

'Update and unlock table
rsGetProdNum.Update
rsGetProdNum.Close

The problem is that some users are getting results where rsGetProdNum.EOF is
True, that is, it does not return anything. This condition is not possible
because it
is garanteed that the Prod Id table will *always* contain valid data. This
might be
happening because of some locking issue.

What is the best way to approach this scenario? Thanks Saga.

 >> Stay informed about: Table locking causing problems 
Back to top
Login to vote
Sylvain Lafontaine

External


Since: Mar 21, 2009
Posts: 6



(Msg. 2) Posted: Sat Mar 21, 2009 4:33 am
Post subject: Re: Table locking causing problems [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

It's impossible to remember (or understand) all the intricacies of the
various parameters for opening an ADO recordset. For example, it's possible
that by replacing adOpenDynamic with adOpenKeyset will make your code work
or not but I cannot tell you anything on this.

However, in your case, the solution would be to not compute the new ID value
on the client side using ADO. This type of calculation should always be done
directly on the server inside a transaction using a stored procedure (SP).
Search Google for "updlock holdlock incrementing" and you should find many
examples. This is not only a question of performance and reliability but
also a question of not blocking the other users if - for any reason - the
client application freezes in mid-process.

You must also take into account the fact that even if the SQL-Server will
always correctly return a new ID, it's possible that the client will freeze
right after - for whatever reason - and make this new ID orphan; ie.,
without any associated record. In order to eliminate this possibility, the
SP should not only return the new ID but also take in charge the process of
creating the new empty record. Later, you can check the database for empty
records or you can chose to set a flag inside each record when creating it
saying that this record is new but still unused. This way, you will ensure
that there is no hole in your sequence of numbers and that there is always
an associated record - possibly unused - for each number; something that is
mandatory for financial records.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain2009 sylvainlafontaine com (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)


"Saga" wrote in message

>I have to fix a possible locking problem with an existing app.
>
> The overall process consists of getting a base 36 number from a table to
> use as a
> product id, then increment it by one and save the new number back to the
> table.
>
> The table has only one column and one row, containing the next available
> product
> id. The select statement is trivial:
>
> select CurProId from ProdIdTable
>
> The record set is declared local to the procedure:
>
> dim rsGetProdNum as ADODB.Recordset
> dim NextProdId as String
>
> The app is using ADO 2.7 and the code to open the record set is:
>
> Set rsGetProdNum = New ADODB.Recordset
> rsGetProdNum.CursorLocation = adUseServer
> rsGetProdNum.Open strSQL, dbADOConx, adOpenDynamic, adLockPessimistic
>
> There is a validation for the results (sort of):
>
> With rsGetProdNum
> If .EOF = False Then
> NewProdNum = Trim(!CurProId)
> Else
> MsgBox "INVALID PRODUCT NUMBER CALL COMPUTER DEPT!!!", vbOKOnly
> Exit Function
> End If
> End With
>
> The logic then proceeds to calculate the next product id and then updates
> and
> closes the table:
>
> (logic to calculate next prod id snipped, resulting id in NextProdId
> variable)
>
> rsGetProdNum!CurProId = NextProdId
>
> 'Update and unlock table
> rsGetProdNum.Update
> rsGetProdNum.Close
>
> The problem is that some users are getting results where rsGetProdNum.EOF
> is
> True, that is, it does not return anything. This condition is not possible
> because it
> is garanteed that the Prod Id table will *always* contain valid data. This
> might be
> happening because of some locking issue.
>
> What is the best way to approach this scenario? Thanks Saga.
>
>
>

 >> Stay informed about: Table locking causing problems 
Back to top
Login to vote
Saga

External


Since: Mar 18, 2009
Posts: 2



(Msg. 3) Posted: Mon Mar 23, 2009 9:47 am
Post subject: Re: Table locking causing problems [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thank you for you reply. I will look up "updlock holdlock incrementing". I
had
thought of using a stored procedure to get the product id and insert a new
record,
but I hesitated because the insert process consists of 4 inserts into tables
that have
anywhere from 10 to 50 fields and I did not know how to effectively pass all
these
fields to the SP. I did not want an SP with almost 100 input parameters Smile
Saga

"Sylvain Lafontaine" wrote in message

> It's impossible to remember (or understand) all the intricacies of the
> various parameters for opening an ADO recordset. For example, it's
> possible that by replacing adOpenDynamic with adOpenKeyset will make your
> code work or not but I cannot tell you anything on this.
>
> However, in your case, the solution would be to not compute the new ID
> value on the client side using ADO. This type of calculation should always
> be done directly on the server inside a transaction using a stored
> procedure (SP). Search Google for "updlock holdlock incrementing" and you
> should find many examples. This is not only a question of performance and
> reliability but also a question of not blocking the other users if - for
> any reason - the client application freezes in mid-process.
>
> You must also take into account the fact that even if the SQL-Server will
> always correctly return a new ID, it's possible that the client will
> freeze right after - for whatever reason - and make this new ID orphan;
> ie., without any associated record. In order to eliminate this
> possibility, the SP should not only return the new ID but also take in
> charge the process of creating the new empty record. Later, you can check
> the database for empty records or you can chose to set a flag inside each
> record when creating it saying that this record is new but still unused.
> This way, you will ensure that there is no hole in your sequence of
> numbers and that there is always an associated record - possibly unused -
> for each number; something that is mandatory for financial records.
>
> --
> Sylvain Lafontaine, ing.
> MVP - Windows Live Platform
> Email: sylvain2009 sylvainlafontaine com (fill the blanks, no spam please)
> Independent consultant and remote programming for Access and SQL-Server
> (French)
>
>
> "Saga" wrote in message
>
>>I have to fix a possible locking problem with an existing app.
>>
>> The overall process consists of getting a base 36 number from a table to
>> use as a
>> product id, then increment it by one and save the new number back to the
>> table.
>>
>> The table has only one column and one row, containing the next available
>> product
>> id. The select statement is trivial:
>>
>> select CurProId from ProdIdTable
>>
>> The record set is declared local to the procedure:
>>
>> dim rsGetProdNum as ADODB.Recordset
>> dim NextProdId as String
>>
>> The app is using ADO 2.7 and the code to open the record set is:
>>
>> Set rsGetProdNum = New ADODB.Recordset
>> rsGetProdNum.CursorLocation = adUseServer
>> rsGetProdNum.Open strSQL, dbADOConx, adOpenDynamic, adLockPessimistic
>>
>> There is a validation for the results (sort of):
>>
>> With rsGetProdNum
>> If .EOF = False Then
>> NewProdNum = Trim(!CurProId)
>> Else
>> MsgBox "INVALID PRODUCT NUMBER CALL COMPUTER DEPT!!!", vbOKOnly
>> Exit Function
>> End If
>> End With
>>
>> The logic then proceeds to calculate the next product id and then updates
>> and
>> closes the table:
>>
>> (logic to calculate next prod id snipped, resulting id in NextProdId
>> variable)
>>
>> rsGetProdNum!CurProId = NextProdId
>>
>> 'Update and unlock table
>> rsGetProdNum.Update
>> rsGetProdNum.Close
>>
>> The problem is that some users are getting results where rsGetProdNum.EOF
>> is
>> True, that is, it does not return anything. This condition is not
>> possible because it
>> is garanteed that the Prod Id table will *always* contain valid data.
>> This might be
>> happening because of some locking issue.
>>
>> What is the best way to approach this scenario? Thanks Saga.
>>
>>
>>
>
>
 >> Stay informed about: Table locking causing problems 
Back to top
Login to vote
Sylvain Lafontaine

External


Since: Mar 21, 2009
Posts: 6



(Msg. 4) Posted: Mon Mar 23, 2009 10:34 pm
Post subject: Re: Table locking causing problems [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Oh, you're not obligated to fill all these records in the SP (or even to use
a SP to fill them). All that's important is to create the new product ID
and - if it's absolutely necessary that you never end up with any orphan -
the creation of one record to associate this newly created product ID with
at least one record of a well known initial state.

It's not even necessary for this record to contains any useful value other
than to say that it has just been created (and/or initialised to a zeroed
state).

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain2009 sylvainlafontaine com (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)


"Saga" wrote in message

> Thank you for you reply. I will look up "updlock holdlock incrementing". I
> had
> thought of using a stored procedure to get the product id and insert a new
> record,
> but I hesitated because the insert process consists of 4 inserts into
> tables that have
> anywhere from 10 to 50 fields and I did not know how to effectively pass
> all these
> fields to the SP. I did not want an SP with almost 100 input parameters
> Smile
> Saga
>
> "Sylvain Lafontaine" wrote in message
>
>> It's impossible to remember (or understand) all the intricacies of the
>> various parameters for opening an ADO recordset. For example, it's
>> possible that by replacing adOpenDynamic with adOpenKeyset will make your
>> code work or not but I cannot tell you anything on this.
>>
>> However, in your case, the solution would be to not compute the new ID
>> value on the client side using ADO. This type of calculation should
>> always be done directly on the server inside a transaction using a stored
>> procedure (SP). Search Google for "updlock holdlock incrementing" and you
>> should find many examples. This is not only a question of performance
>> and reliability but also a question of not blocking the other users if -
>> for any reason - the client application freezes in mid-process.
>>
>> You must also take into account the fact that even if the SQL-Server will
>> always correctly return a new ID, it's possible that the client will
>> freeze right after - for whatever reason - and make this new ID orphan;
>> ie., without any associated record. In order to eliminate this
>> possibility, the SP should not only return the new ID but also take in
>> charge the process of creating the new empty record. Later, you can
>> check the database for empty records or you can chose to set a flag
>> inside each record when creating it saying that this record is new but
>> still unused. This way, you will ensure that there is no hole in your
>> sequence of numbers and that there is always an associated record -
>> possibly unused - for each number; something that is mandatory for
>> financial records.
>>
>> --
>> Sylvain Lafontaine, ing.
>> MVP - Windows Live Platform
>> Email: sylvain2009 sylvainlafontaine com (fill the blanks, no spam
>> please)
>> Independent consultant and remote programming for Access and SQL-Server
>> (French)
>>
>>
>> "Saga" wrote in message
>>
>>>I have to fix a possible locking problem with an existing app.
>>>
>>> The overall process consists of getting a base 36 number from a table to
>>> use as a
>>> product id, then increment it by one and save the new number back to the
>>> table.
>>>
>>> The table has only one column and one row, containing the next available
>>> product
>>> id. The select statement is trivial:
>>>
>>> select CurProId from ProdIdTable
>>>
>>> The record set is declared local to the procedure:
>>>
>>> dim rsGetProdNum as ADODB.Recordset
>>> dim NextProdId as String
>>>
>>> The app is using ADO 2.7 and the code to open the record set is:
>>>
>>> Set rsGetProdNum = New ADODB.Recordset
>>> rsGetProdNum.CursorLocation = adUseServer
>>> rsGetProdNum.Open strSQL, dbADOConx, adOpenDynamic, adLockPessimistic
>>>
>>> There is a validation for the results (sort of):
>>>
>>> With rsGetProdNum
>>> If .EOF = False Then
>>> NewProdNum = Trim(!CurProId)
>>> Else
>>> MsgBox "INVALID PRODUCT NUMBER CALL COMPUTER DEPT!!!", vbOKOnly
>>> Exit Function
>>> End If
>>> End With
>>>
>>> The logic then proceeds to calculate the next product id and then
>>> updates and
>>> closes the table:
>>>
>>> (logic to calculate next prod id snipped, resulting id in NextProdId
>>> variable)
>>>
>>> rsGetProdNum!CurProId = NextProdId
>>>
>>> 'Update and unlock table
>>> rsGetProdNum.Update
>>> rsGetProdNum.Close
>>>
>>> The problem is that some users are getting results where
>>> rsGetProdNum.EOF is
>>> True, that is, it does not return anything. This condition is not
>>> possible because it
>>> is garanteed that the Prod Id table will *always* contain valid data.
>>> This might be
>>> happening because of some locking issue.
>>>
>>> What is the best way to approach this scenario? Thanks Saga.
>>>
>>>
>>>
>>
>>
>
>
 >> Stay informed about: Table locking causing problems 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
using ignore duplicate index causing problems for Ole db - I have a stored procedure that builds a temp table to filter duplicates before selecting the records I want. The filter is to get the first maximum value in any year. The duplicate key ignored message is returned along with the rows. Ole db sees a retur...

sql CLR trigger causing havoc in Access linked table - Have been running an Access front end (mdb) on a SQL Server back end for several years. Will soon be migrating to SQL2005. Have detected a showstopper of a problem. Some background: One reason to move to SQL05 is the use of CLR for a generic audit...

REQ: Best practices locking, truncating, processing data d.. - Hello, We have a high traffic web server farm where each server captures data and using c# bulk insert stores the data in tbDataDump (SQL Server 2005) almost every second throughout the day. I've been using a INSTEAD OF INSERT trigger to apply..

possible problems altering column order in a table via code? - Right off the bat, I'm not interested in anyone giving me grief regarding this issue. I have (IMHO) valid reasons for this. And I also will refer you to Erland Sommarskog's response to a similar post a couple years ago:..

batch insert into a Huge table performance problems - We have a table (TableB) which we use to archive records accumulated for the day from (TableA). All live applications are using TableA (1000 queries a minute) When we archive the data from TableA to TableB we use this: Insert into TableB select *..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]