Showing posts with label optimistic. Show all posts
Showing posts with label optimistic. Show all posts

Tuesday, March 20, 2012

optimistic locking

we have an old Win NT4/SQL Server 6.5 server outhere
the size of the database has been increasing over the years(and all the
data is used daily), so we`ve been looking to upgrade the hardware or
move to a virtual mashine
but a consultant we hired on another project thinks that the main reason
for the poor performance is the pessimistic locking used by the DB, and
recomends upgrade to Oracle
as i have no Oracle expirience and SQL Server is easyer to install and
surely easyer to upgrade to in this case i`m looking for a more
optimistic version
does 7.0, 2000 and 2005 support optimistic locking ?
(we have an unused 7.0 1 cpu licence)
"Fred" <fred@.ilovespam.com> wrote in message
news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all the
> data is used daily), so we`ve been looking to upgrade the hardware or move
> to a virtual mashine
>
Don't go to a VM, at least not for your end state. Upgrade SQL Server to
2000 or 2005 (when released), and then move the databse to a new server
running Windows Server 2003.

> but a consultant we hired on another project thinks that the main reason
> for the poor performance is the pessimistic locking used by the DB, and
> recomends upgrade to Oracle
Hmm. Very very unlikely. SQL 2000 or 2005 on Windows 2003 and modern
hardware should fix your problem.

> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more optimistic
> version
>
Migrating the application to Oracle will be a _whole_ lot harder than
upgrading SQL Server and Windows.

> does 7.0, 2000 and 2005 support optimistic locking ?
>
SQL Server 2005 supports a locking model very similar to Oracle's model. So
on SQL Server 2005 you could actually test which concurrency model works
best for your application. But you won't need to. The hardware and
software upgrade should fix you up.
David
|||SQL 7.0 and higher gives you the option to choose a locking model (see
BOL for Isolation Levels), not so sure about 6.5. But before going for
a upgrade make sure it's actually SQL Server using pessimistic locking
and not your application. If had this kind of issues and most of the
times it's things like locking hints in the application code or using
commands like SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. In this
case upgrading the database won't solve anything.
M
|||I would consider this advice complete crap. Any good consultant should be
able to justify such a decision in concrete terms. Was that done? What is
the impact analysis? Where is the cost/benefit analysis? As David
suggested, an upgrade to the current version will likely do wonders for your
system (but it will likely involve re-tuning the application as well as
extensive testing).
Since you're still using 6.5, the system can't be working all that poorly
now, can it?
|||> does 7.0, 2000 and 2005 support optimistic locking ?
All versions of SQL Server support optimistic concurrency control because
this is handled via application code. With optimistic concurrency, the
application holds minimal locks based on the assumption that it is unlikely
that data are modified by other sessions. The application checks to see if
another user modified data before permitting the update. For example:
UPDATE MyTable
SET DataValue = @.NewDataValue
WHERE PK= @.PK AND
DataValue = @.OldDataValue
IF @.@.ORWCOUNT = 0
BEGIN
RAISERROR ('Data was updated or deleted by another user', 16, 1)
END
This is in contrast with pessimistic locking where the application relies on
the database to control concurrency using locking.
In SQL 6.5, a page was the most granular level of locking (ignoring insert
row-level locking for the purposes of discussion). This exacerbated
concurrency problems since unrelated data was also locked. Row-level
locking is provided in later versions so concurrency is improved, even with
pessimistic locking. Consequently, you might get some relief by upgrading
with no application changes but a lot depends on the details of your app. I
suggest you consider upgrading to SQL 7 before downgrading to another DBMS

As David mentioned, SQL Server 2005 introduces the READ_COMMITTED_SNAPSHOT
isolation level, which provides row versioning and statement-level read
consistency. This may further improve concurrency but you need to be aware
that an application that employs pessimistic concurrency might make
concurrency assumptions that are not valid under the READ_COMMITTED_SNAPSHOT
isolation level.
Hope this helps.
Dan Guzman
SQL Server MVP
"Fred" <fred@.ilovespam.com> wrote in message
news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all the
> data is used daily), so we`ve been looking to upgrade the hardware or move
> to a virtual mashine
> but a consultant we hired on another project thinks that the main reason
> for the poor performance is the pessimistic locking used by the DB, and
> recomends upgrade to Oracle
> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more optimistic
> version
> does 7.0, 2000 and 2005 support optimistic locking ?
> (we have an unused 7.0 1 cpu licence)
|||Fred wrote:
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all
> the data is used daily), so we`ve been looking to upgrade the
> hardware or move to a virtual mashine
> but a consultant we hired on another project thinks that the main
> reason for the poor performance is the pessimistic locking used by
> the DB, and recomends upgrade to Oracle
Does he only *think* this is the reason or did he present hard facts to
backup this statement? If not, I'd first start to investigate the
performance of your current setup. Maybe you find an easy solution to
speed up things such as an index that is missing etc.

> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more
> optimistic version
> does 7.0, 2000 and 2005 support optimistic locking ?
> (we have an unused 7.0 1 cpu licence)
Kind regards
robert
|||That is right. SQL Server supports a feature called RCSI. It is a database
option. You can use it to change your concurrency control model without
changing your application. However, as David pointed out here, you may not
even need to.
--
Wei Xiao [MSFT]
SQL Server Storage Engine Development
http://blogs.msdn.com/weix
This posting is provided "AS IS" with no warranties, and confers no rights.
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uHk41UyzFHA.3780@.TK2MSFTNGP12.phx.gbl...
> "Fred" <fred@.ilovespam.com> wrote in message
> news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> Don't go to a VM, at least not for your end state. Upgrade SQL Server to
> 2000 or 2005 (when released), and then move the databse to a new server
> running Windows Server 2003.
>
> Hmm. Very very unlikely. SQL 2000 or 2005 on Windows 2003 and modern
> hardware should fix your problem.
>
> Migrating the application to Oracle will be a _whole_ lot harder than
> upgrading SQL Server and Windows.
>
> SQL Server 2005 supports a locking model very similar to Oracle's model.
> So on SQL Server 2005 you could actually test which concurrency model
> works best for your application. But you won't need to. The hardware and
> software upgrade should fix you up.
> David
>

optimistic locking

I am writing an Application which is connected to a database on MSSQl
Server. For performing updates on a table I chose the way of optimistic
locking using timestamps. I use a stored procedure for updating the
values which returns either 0 or 1 to indicate if there was a conflict
or not. If there was one, the current values in the database are selected.
My stored procedure for updating data looks like this:
create procedure updateSomeTable
@.key int,
@.col1 nvarchar(100),
@.col2 datetime,
@.TS timestamp
AS
update MyDB.dbo.SomeTable
set col1 = @.col1,
col2 = @.col2
where key = @.key and TS = @.TS
if(@.@.rowcount = 0)
begin
select * from SomeTable where key = @.key
return 1
end
return 0
Do I need any transactions within souch a procedure? As far as I can
think of, I don't need any... Is this the typical way to perform this
task, or are there much more common ways to do this?
Thanks
Regards
StephanNot really. The UPDATE command is in an implicit transaction, and it is the
nature of the beast of opimistic locking that a user might change it after
you retrieve it (it is optimistic after all.)
Not sure that I would just conditionally receive the row. The return count
here is not of much value, since you already have to decide whether or not
to fetch a dataset which may or may not be returned. I would just always
return the row, which takes care of any trigger modifications (if there were
any, usually this in not a big deal) but also to handle the new timestamp.
Once you do the update it will change, meaning you will have to go fetch at
least the timestamp after the modification.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Stephan Zaubzer" <stephan.zaubzer@.schendl.at> wrote in message
news:utkvIuQIGHA.2300@.TK2MSFTNGP15.phx.gbl...
>I am writing an Application which is connected to a database on MSSQl
>Server. For performing updates on a table I chose the way of optimistic
>locking using timestamps. I use a stored procedure for updating the values
>which returns either 0 or 1 to indicate if there was a conflict or not. If
>there was one, the current values in the database are selected.
> My stored procedure for updating data looks like this:
> create procedure updateSomeTable
> @.key int,
> @.col1 nvarchar(100),
> @.col2 datetime,
> @.TS timestamp
> AS
> update MyDB.dbo.SomeTable
> set col1 = @.col1,
> col2 = @.col2
> where key = @.key and TS = @.TS
> if(@.@.rowcount = 0)
> begin
> select * from SomeTable where key = @.key
> return 1
> end
> return 0
> Do I need any transactions within souch a procedure? As far as I can think
> of, I don't need any... Is this the typical way to perform this task, or
> are there much more common ways to do this?
> Thanks
> Regards
> Stephan|||Ok, so my thoughts of not to need an explicit transaction was ok...
As you said it is definitely better to fetch the new dataset immediately
after inserting it. But the chances exist, that someone will update this
row between my insert and the fetch. But this probability should rather
be negligible.
Are there any other issues besides these with this procedure?
Louis Davidson schrieb:
> Not really. The UPDATE command is in an implicit transaction, and it is t
he
> nature of the beast of opimistic locking that a user might change it after
> you retrieve it (it is optimistic after all.)
> Not sure that I would just conditionally receive the row. The return coun
t
> here is not of much value, since you already have to decide whether or not
> to fetch a dataset which may or may not be returned. I would just always
> return the row, which takes care of any trigger modifications (if there we
re
> any, usually this in not a big deal) but also to handle the new timestamp.
> Once you do the update it will change, meaning you will have to go fetch a
t
> least the timestamp after the modification.
>|||> after inserting it. But the chances exist, that someone will update this
> row between my insert and the fetch. But this probability should rather be
> negligible.
This really isn't a big deal at all. The idea behind optimistic locking is
that you can get the rows, hold onto them for a long time, and still know
that they are good by the timestamp. You just accept this as the risk
(obviously the risk should be evaluated based on the data integrity
requirements).
I am going to assume that this is for 2000. If it is 2005, we could extend
using TRY...CATCH. You would possibly want transactions for 2005 with
TRY...CATCH because of how triggers behave. In 2000 an error/rollback in a
trigger just halted everything. If you are in a TRY...CATCH block, that
changes.
Anyhow, I wouldn't put the database name as a prefix, unless it is in a
different one from the tables. It makes it harder to use it with a
different named database, like for testing if you don't have the ability to
have multiple servers. I think this will work fine. I added a check to see
if the row had been deleted, though you will also know that because no rows
will be returned :)
create procedure updateSomeTable
@.key int,
@.col1 nvarchar(100),
@.col2 datetime,
@.TS timestamp
AS
declare @.rowcount int
update dbo.SomeTable
set col1 = @.col1,
col2 = @.col2
where key = @.key
and TS = @.TS
set @.rowcount = @.@.rowcount
select col1, col2
from dbo.SomeTable
where key= @.key
--this means that the row has been deleted
if @.rowcount = 1 --this means it has been updated
return 1
else
return 0
go
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Stephan Zaubzer" <stephan.zaubzer@.schendl.at> wrote in message
news:%235J5fSSIGHA.2696@.TK2MSFTNGP14.phx.gbl...
> Ok, so my thoughts of not to need an explicit transaction was ok...
> As you said it is definitely better to fetch the new dataset immediately
> after inserting it. But the chances exist, that someone will update this
> row between my insert and the fetch. But this probability should rather be
> negligible.
> Are there any other issues besides these with this procedure?
> Louis Davidson schrieb:

optimistic locking

we have an old Win NT4/SQL Server 6.5 server outhere
the size of the database has been increasing over the years(and all the
data is used daily), so we`ve been looking to upgrade the hardware or
move to a virtual mashine
but a consultant we hired on another project thinks that the main reason
for the poor performance is the pessimistic locking used by the DB, and
recomends upgrade to Oracle
as i have no Oracle expirience and SQL Server is easyer to install and
surely easyer to upgrade to in this case i`m looking for a more
optimistic version
does 7.0, 2000 and 2005 support optimistic locking ?
(we have an unused 7.0 1 cpu licence)"Fred" <fred@.ilovespam.com> wrote in message
news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all the
> data is used daily), so we`ve been looking to upgrade the hardware or move
> to a virtual mashine
>
Don't go to a VM, at least not for your end state. Upgrade SQL Server to
2000 or 2005 (when released), and then move the databse to a new server
running Windows Server 2003.

> but a consultant we hired on another project thinks that the main reason
> for the poor performance is the pessimistic locking used by the DB, and
> recomends upgrade to Oracle
Hmm. Very very unlikely. SQL 2000 or 2005 on Windows 2003 and modern
hardware should fix your problem.

> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more optimistic
> version
>
Migrating the application to Oracle will be a _whole_ lot harder than
upgrading SQL Server and Windows.

> does 7.0, 2000 and 2005 support optimistic locking ?
>
SQL Server 2005 supports a locking model very similar to Oracle's model. So
on SQL Server 2005 you could actually test which concurrency model works
best for your application. But you won't need to. The hardware and
software upgrade should fix you up.
David|||SQL 7.0 and higher gives you the option to choose a locking model (see
BOL for Isolation Levels), not so sure about 6.5. But before going for
a upgrade make sure it's actually SQL Server using pessimistic locking
and not your application. If had this kind of issues and most of the
times it's things like locking hints in the application code or using
commands like SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. In this
case upgrading the database won't solve anything.
M|||I would consider this advice complete crap. Any good consultant should be
able to justify such a decision in concrete terms. Was that done? What is
the impact analysis? Where is the cost/benefit analysis? As David
suggested, an upgrade to the current version will likely do wonders for your
system (but it will likely involve re-tuning the application as well as
extensive testing).
Since you're still using 6.5, the system can't be working all that poorly
now, can it?|||> does 7.0, 2000 and 2005 support optimistic locking ?
All versions of SQL Server support optimistic concurrency control because
this is handled via application code. With optimistic concurrency, the
application holds minimal locks based on the assumption that it is unlikely
that data are modified by other sessions. The application checks to see if
another user modified data before permitting the update. For example:
UPDATE MyTable
SET DataValue = @.NewDataValue
WHERE PK= @.PK AND
DataValue = @.OldDataValue
IF @.@.ORWCOUNT = 0
BEGIN
RAISERROR ('Data was updated or deleted by another user', 16, 1)
END
This is in contrast with pessimistic locking where the application relies on
the database to control concurrency using locking.
In SQL 6.5, a page was the most granular level of locking (ignoring insert
row-level locking for the purposes of discussion). This exacerbated
concurrency problems since unrelated data was also locked. Row-level
locking is provided in later versions so concurrency is improved, even with
pessimistic locking. Consequently, you might get some relief by upgrading
with no application changes but a lot depends on the details of your app. I
suggest you consider upgrading to SQL 7 before downgrading to another DBMS

As David mentioned, SQL Server 2005 introduces the READ_COMMITTED_SNAPSHOT
isolation level, which provides row versioning and statement-level read
consistency. This may further improve concurrency but you need to be aware
that an application that employs pessimistic concurrency might make
concurrency assumptions that are not valid under the READ_COMMITTED_SNAPSHOT
isolation level.
Hope this helps.
Dan Guzman
SQL Server MVP
"Fred" <fred@.ilovespam.com> wrote in message
news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all the
> data is used daily), so we`ve been looking to upgrade the hardware or move
> to a virtual mashine
> but a consultant we hired on another project thinks that the main reason
> for the poor performance is the pessimistic locking used by the DB, and
> recomends upgrade to Oracle
> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more optimistic
> version
> does 7.0, 2000 and 2005 support optimistic locking ?
> (we have an unused 7.0 1 cpu licence)|||Fred wrote:
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all
> the data is used daily), so we`ve been looking to upgrade the
> hardware or move to a virtual mashine
> but a consultant we hired on another project thinks that the main
> reason for the poor performance is the pessimistic locking used by
> the DB, and recomends upgrade to Oracle
Does he only *think* this is the reason or did he present hard facts to
backup this statement? If not, I'd first start to investigate the
performance of your current setup. Maybe you find an easy solution to
speed up things such as an index that is missing etc.

> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more
> optimistic version
> does 7.0, 2000 and 2005 support optimistic locking ?
> (we have an unused 7.0 1 cpu licence)
Kind regards
robert|||That is right. SQL Server supports a feature called RCSI. It is a database
option. You can use it to change your concurrency control model without
changing your application. However, as David pointed out here, you may not
even need to.
--
Wei Xiao [MSFT]
SQL Server Storage Engine Development
http://blogs.msdn.com/weix
This posting is provided "AS IS" with no warranties, and confers no rights.
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uHk41UyzFHA.3780@.TK2MSFTNGP12.phx.gbl...
> "Fred" <fred@.ilovespam.com> wrote in message
> news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> Don't go to a VM, at least not for your end state. Upgrade SQL Server to
> 2000 or 2005 (when released), and then move the databse to a new server
> running Windows Server 2003.
>
> Hmm. Very very unlikely. SQL 2000 or 2005 on Windows 2003 and modern
> hardware should fix your problem.
>
> Migrating the application to Oracle will be a _whole_ lot harder than
> upgrading SQL Server and Windows.
>
> SQL Server 2005 supports a locking model very similar to Oracle's model.
> So on SQL Server 2005 you could actually test which concurrency model
> works best for your application. But you won't need to. The hardware and
> software upgrade should fix you up.
> David
>

optimistic locking

we have an old Win NT4/SQL Server 6.5 server outhere
the size of the database has been increasing over the years(and all the
data is used daily), so we`ve been looking to upgrade the hardware or
move to a virtual mashine
but a consultant we hired on another project thinks that the main reason
for the poor performance is the pessimistic locking used by the DB, and
recomends upgrade to Oracle
as i have no Oracle expirience and SQL Server is easyer to install and
surely easyer to upgrade to in this case i`m looking for a more
optimistic version :)
does 7.0, 2000 and 2005 support optimistic locking ?
(we have an unused 7.0 1 cpu licence)"Fred" <fred@.ilovespam.com> wrote in message
news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all the
> data is used daily), so we`ve been looking to upgrade the hardware or move
> to a virtual mashine
>
Don't go to a VM, at least not for your end state. Upgrade SQL Server to
2000 or 2005 (when released), and then move the databse to a new server
running Windows Server 2003.
> but a consultant we hired on another project thinks that the main reason
> for the poor performance is the pessimistic locking used by the DB, and
> recomends upgrade to Oracle
Hmm. Very very unlikely. SQL 2000 or 2005 on Windows 2003 and modern
hardware should fix your problem.
> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more optimistic
> version :)
>
Migrating the application to Oracle will be a _whole_ lot harder than
upgrading SQL Server and Windows.
> does 7.0, 2000 and 2005 support optimistic locking ?
>
SQL Server 2005 supports a locking model very similar to Oracle's model. So
on SQL Server 2005 you could actually test which concurrency model works
best for your application. But you won't need to. The hardware and
software upgrade should fix you up.
David|||SQL 7.0 and higher gives you the option to choose a locking model (see
BOL for Isolation Levels), not so sure about 6.5. But before going for
a upgrade make sure it's actually SQL Server using pessimistic locking
and not your application. If had this kind of issues and most of the
times it's things like locking hints in the application code or using
commands like SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. In this
case upgrading the database won't solve anything.
M|||I would consider this advice complete crap. Any good consultant should be
able to justify such a decision in concrete terms. Was that done? What is
the impact analysis? Where is the cost/benefit analysis? As David
suggested, an upgrade to the current version will likely do wonders for your
system (but it will likely involve re-tuning the application as well as
extensive testing).
Since you're still using 6.5, the system can't be working all that poorly
now, can it?|||> does 7.0, 2000 and 2005 support optimistic locking ?
All versions of SQL Server support optimistic concurrency control because
this is handled via application code. With optimistic concurrency, the
application holds minimal locks based on the assumption that it is unlikely
that data are modified by other sessions. The application checks to see if
another user modified data before permitting the update. For example:
UPDATE MyTable
SET DataValue = @.NewDataValue
WHERE PK= @.PK AND
DataValue = @.OldDataValue
IF @.@.ORWCOUNT = 0
BEGIN
RAISERROR ('Data was updated or deleted by another user', 16, 1)
END
This is in contrast with pessimistic locking where the application relies on
the database to control concurrency using locking.
In SQL 6.5, a page was the most granular level of locking (ignoring insert
row-level locking for the purposes of discussion). This exacerbated
concurrency problems since unrelated data was also locked. Row-level
locking is provided in later versions so concurrency is improved, even with
pessimistic locking. Consequently, you might get some relief by upgrading
with no application changes but a lot depends on the details of your app. I
suggest you consider upgrading to SQL 7 before downgrading to another DBMS
:)
As David mentioned, SQL Server 2005 introduces the READ_COMMITTED_SNAPSHOT
isolation level, which provides row versioning and statement-level read
consistency. This may further improve concurrency but you need to be aware
that an application that employs pessimistic concurrency might make
concurrency assumptions that are not valid under the READ_COMMITTED_SNAPSHOT
isolation level.
Hope this helps.
Dan Guzman
SQL Server MVP
"Fred" <fred@.ilovespam.com> wrote in message
news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all the
> data is used daily), so we`ve been looking to upgrade the hardware or move
> to a virtual mashine
> but a consultant we hired on another project thinks that the main reason
> for the poor performance is the pessimistic locking used by the DB, and
> recomends upgrade to Oracle
> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more optimistic
> version :)
> does 7.0, 2000 and 2005 support optimistic locking ?
> (we have an unused 7.0 1 cpu licence)|||Fred wrote:
> we have an old Win NT4/SQL Server 6.5 server outhere
> the size of the database has been increasing over the years(and all
> the data is used daily), so we`ve been looking to upgrade the
> hardware or move to a virtual mashine
> but a consultant we hired on another project thinks that the main
> reason for the poor performance is the pessimistic locking used by
> the DB, and recomends upgrade to Oracle
Does he only *think* this is the reason or did he present hard facts to
backup this statement? If not, I'd first start to investigate the
performance of your current setup. Maybe you find an easy solution to
speed up things such as an index that is missing etc.
> as i have no Oracle expirience and SQL Server is easyer to install and
> surely easyer to upgrade to in this case i`m looking for a more
> optimistic version :)
> does 7.0, 2000 and 2005 support optimistic locking ?
> (we have an unused 7.0 1 cpu licence)
Kind regards
robert|||That is right. SQL Server supports a feature called RCSI. It is a database
option. You can use it to change your concurrency control model without
changing your application. However, as David pointed out here, you may not
even need to.
--
--
Wei Xiao [MSFT]
SQL Server Storage Engine Development
http://blogs.msdn.com/weix
This posting is provided "AS IS" with no warranties, and confers no rights.
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uHk41UyzFHA.3780@.TK2MSFTNGP12.phx.gbl...
> "Fred" <fred@.ilovespam.com> wrote in message
> news:OWAhM$xzFHA.1924@.TK2MSFTNGP14.phx.gbl...
>> we have an old Win NT4/SQL Server 6.5 server outhere
>> the size of the database has been increasing over the years(and all the
>> data is used daily), so we`ve been looking to upgrade the hardware or
>> move to a virtual mashine
> Don't go to a VM, at least not for your end state. Upgrade SQL Server to
> 2000 or 2005 (when released), and then move the databse to a new server
> running Windows Server 2003.
>> but a consultant we hired on another project thinks that the main reason
>> for the poor performance is the pessimistic locking used by the DB, and
>> recomends upgrade to Oracle
> Hmm. Very very unlikely. SQL 2000 or 2005 on Windows 2003 and modern
> hardware should fix your problem.
>> as i have no Oracle expirience and SQL Server is easyer to install and
>> surely easyer to upgrade to in this case i`m looking for a more
>> optimistic version :)
> Migrating the application to Oracle will be a _whole_ lot harder than
> upgrading SQL Server and Windows.
>> does 7.0, 2000 and 2005 support optimistic locking ?
> SQL Server 2005 supports a locking model very similar to Oracle's model.
> So on SQL Server 2005 you could actually test which concurrency model
> works best for your application. But you won't need to. The hardware and
> software upgrade should fix you up.
> David
>

Monday, March 19, 2012

Optimistic Concurrency Help

Hi,

I'm trying to implement Optimistic Concurrency in asp 2 but so far it has caused me nothing but problems.

First, when doing an UPDATE I tried to use the primary key & a timestamp field which I had in SQL Express.. VS 2005 generated the stored procedures fine however when it came to the actual updating I think there was a problem with the conversion of the timestamp field when it was being stored in a text box (in a FormView control). So.. as a result that failed. And also I checked sooo many places online and haven't been able to find any examples of code where a timestamp was used with success in asp2.

Next, I got ride of the timestamp type (in SQL Express database) and used a datetime and then.. I just implemented Optimistic Concurrency by passing in ALL the values (ie all the original values) like is proposed http://www.asp.net/learn/dataaccess/tutorial21vb.aspx?tabid=63 . This... works however I really do not want to have to pass in ALL these values (ie original and new).

Ideally I would like to be able to use the primary key & the datetime field to handle the Optimistic Concurrency checks where only the original values of both those fields are passed back into the stored procedure. Now.. I tried this as well, but I kept getting an error that suggests that (for some reason) the FormView or DataSource is passing ALL the values (original & new) into the dataset as opposed to only the original primary key & datetime fields & the new set of values.

Can ANYONE offer any help? I really would like not to have to pass in all these values.

Thanks in advance!

Why not add a "last_updated" column that is of type uniqueidentiifer (GUID) and use the NEWID() function to create a new guid whenever the table is updated. Obviously you would need to wrap this in a stored proc that all of your client code uses instead of the table direclty. (or you could use a UPDATE trigger).

Then just have the app compare the guid it retrieved on first read to the one on the row when it goes to do the update. If they dont match, you are done. Make sure to wrap the code in a transaction when you first a) check the guid to see if they match, then b) perform the actual update.

-David

|||

Yes, although you will have to set up your stored procedures, and sqldatasource control manually.

First, a timestamp is not a datetime. It's technically a binary object, although it can be converted to/from an integer pretty easily. Also, you can't SET the value of a timestamp field. The whole point of a timestamp column is that the database maintains it. In it's current implementation (logically), every table that has a timestamp column maintains a counter at the table-level. Every time a row is changed, it increments this counter, and uses that value as the timestamp value. So every row that gets changed has a unique number.

As for the error about the datasource/formview passing ALL the values, it's most likely because you have the datasource's conflict detection property set to "CompareAllValues". Change it to "Overwrite". Then only the keys you've specified in the formview/gridview's datakeynames property, and the fields you've bound via a 2-way databind should be passed back.

|||

Thanks for the responses!

dwlovell - Even if I use GUID instead of timestamp or datetime, I still have the prob with the update and getting the parameters passed nicely from the datasource to the stored procedure. Unless... If I put a GUID in the table.. and then in VS 2005 chose optimistic concurrency (when creating the tableadapter and stored procedures).. will VS 2005 recognise the GUID field (like it does with the timestamp field) and therefore use only the table key & that GUID fields to compare for the update??

Motley - I know that timestamp and datetime are different, I only used datetime because I (a) timestamp wasn't working and (b) datetime could be used (when tweeked) to implement the optimistic concurrency.

To avoid the error about passing all the values, I had used OverwriteChanges before but my problem with that is that I don't think the datetime field's original value is being passed to the stored procedure successfully. The only original value being passed was the key. Therefore, I'm getting a "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM." error constantly (I believe that 0/0/0000 00:00:00 - i.e. an invalid value - is being passed from the dataset to the sql stored procedure). This error is linked with the following LOC in the stack trace: "int returnValue = this.Adapter.UpdateCommand.ExecuteNonQuery();".

The strange thing is that when I check the FormView1_ItemUpdating's event, I can see the datatime field's values in both e.NewValues & e.OldValues.. so I can't understand why the value is not being passed to the stored procedure.

A snippet from my <EditItemTemplate> (in the FormView) is below:

 <asp:TextBox ID="RTextBox" runat="server" Text='<%# Bind("R")%>'>
</asp:TextBox><br />
RDescription:
<asp:TextBox ID="RDescriptionTextBox" runat="server" Text='<%# Bind("RDescription")%>'>
</asp:TextBox><br />
Lastupdate:
<asp:TextBox ID="LastupdateTextBox" runat="server" Text='<%# Bind("Lastupdate")%>'>
</asp:TextBox>
...

A snippet from my ObjectDataSource is below:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" ConflictDetection="OverwriteChanges"
DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData" TypeName="DataSet1TableAdapters.RTableAdapter" UpdateMethod="Update">
<UpdateParameters>
<asp:Parameter Name="R" Type="Byte" />
<asp:Parameter Name="RDescription" Type="String" />
<asp:Parameter Name="Lastupdate" Type="DateTime" />
<asp:Parameter Name="original_R" Type="Byte" />
<asp:Parameter Name="original_Lastupdate" Type="DateTime" />
</UpdateParameters>
...

Here's a bit from the <UpdateCommand> in the DataSet (not sure it's relevant but.. doesn't hurt and maybe one of you gurus will find an answer to my prob there..):

 <Parameters>
<Parameter AllowDbNull="False" AutogeneratedName="" DataSourceName="" DbType="Object" Direction="ReturnValue" ParameterName="@.RETURN_VALUE" Precision="0" Scale="0" Size="0" SourceColumnNullMapping="False" SourceVersion="Current">
</Parameter>
<Parameter AllowDbNull="False" AutogeneratedName="" DataSourceName="" DataTypeServer="tinyint" DbType="Byte" Direction="Input" ParameterName="@.R" Precision="0" ProviderType="TinyInt" Scale="0" Size="0" SourceColumn="R" SourceColumnNullMapping="False" SourceVersion="Current">
</Parameter>
<Parameter AllowDbNull="False" AutogeneratedName="" DataSourceName="" DataTypeServer="varchar(40)" DbType="AnsiString" Direction="Input" ParameterName="@.RDescription" Precision="0" ProviderType="VarChar" Scale="0" Size="0" SourceColumn="RDescription" SourceColumnNullMapping="False" SourceVersion="Current">
</Parameter>
<Parameter AllowDbNull="False" AutogeneratedName="" DataSourceName="" DataTypeServer="datetime" DbType="DateTime" Direction="Input" ParameterName="@.Lastupdate" Precision="0" ProviderType="DateTime" Scale="0" Size="0" SourceColumn="Lastupdate" SourceColumnNullMapping="False" SourceVersion="Current">
</Parameter>
<Parameter AllowDbNull="False" AutogeneratedName="" DataSourceName="" DataTypeServer="tinyint" DbType="Byte" Direction="Input" ParameterName="@.Original_R" Precision="0" ProviderType="TinyInt" Scale="0" Size="0" SourceColumn="R" SourceColumnNullMapping="False" SourceVersion="Original">
</Parameter>
<Parameter AllowDbNull="False" AutogeneratedName="" DataSourceName="" DataTypeServer="datetime" DbType="DateTime" Direction="Input" ParameterName="@.Original_Lastupdate" Precision="0" ProviderType="DateTime" Scale="0" Size="0" SourceColumn="Lastupdate" SourceColumnNullMapping="False" SourceVersion="Original">
</Parameter>

So... any more thoughts??|||

You might try posting this in the ObjectDatasource forum.

You might also try adding a parameter at the top of your list in the ObjectDataSource for the return value. It's possible the parameters are being shifted by one which would obviously try and force a different value into your datetime column and give the error you see (I could be completely wrong on that, I'm not a ObjectDataSource expert).

If this was a SqlDataSource, I would suggest catching the SqlDataSource_Updating event, and check the command object (and it's parameters), to make sure they all look correct at that stage.

|||

Well you definitely have shed some light on this! And I thank you greatly for it! (See, even though you're not an ObjectDataSource expert, you still helped!!Smile) I checked the ObjectDataSource_Updating event and the original parameter for the Lastupdate was 'Nothing'.

This clearly indicates that the parameter is not being passed to the stored procedure and instead, is somehow being lost between the FormView and the ObjectDataSource. I really don't have a clue why this is happening though! To me, it's like (for optimistic concurrency) the ObjectDataSource control is to work when either ALL the original values are being passed in (i.e. ConflictDetection="CompareAllValues") or just the Key (i.e. ConflictDetection="OverwriteChanges") for the update. There seems to be no "mid-point" where one can send only a specific subset of original values to the ObjectDataSourceautomatically.. which really is a bummer!

Now what I'll have to do is get the original Lastupdate value from the form and set Lastupdate input parameter to that value in the ObjectDataSource_Updating event.

I've also taken your suggestion and posted this question in the ObjectDatasource forum.Here's the link for those of you interested.

Thanks again Motley!

|||

I've written a tutorial to address this exact issue... after spending days figuring it all out.

http://www.primedigit.com/implementing-optimistic-concurrency-with-sql-timestamps/

Optimistic Concurrency Control Error

Hi,

I have a table X:
ID (PK, int, not null)
cstID(FK, int, not null)
Name( nvarchar(100),not null)
Desc( ntext, null)

I am using the table view in Enterprise manager, if I manually type in a new row, then I edit that row, setting "Desc" = NULL, then I delete that row (from within the table view) I get the error:

Data has changed since the results pane was last retrieved. Do you want to save your changes now? (Optimistic Concurrency Control Error)

Things to note:
There was a FTI on this table, I deleted it, didn't help.
No other process or users are editing/viewing this table
The error doesn't occur if edit any other column, just setting the "Desc" to NULL creates this error.

Some other tables in my DB exhibit this same behavior, but not all......I can't figure out what the heck is going on...can you?

Can't anyone take a stab at this?

Some more information, I using SQL server 2005 so I am using MS not EM.

I created a duplicate table and that dup table doesn't have the same issue. So, I scripted both the bad table and the dup table, both scripts look identical sans the table names. I ran a trace and it doesn't look like anything different is happening between the original table and the dup table.

The table doesn't have any triggers.

Please help!

|||I get a similar message when I try to update any tables that contain fields of type bit, ntext, text, or image. But I only get it occasionally and cannot find a reason. I changed my ntext field to varchar and that eliminated the problem.|||

I can help you out here.

The fundamental cause of the problem is that Management Studio is rubbish.

It breaks down like this:

Management Studio can't handle edits on rows with char/varchar/text fields with more than 4000 characters of data (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=266305&SiteID=1)

One side -effect of this is that in optimistic mode Management Studio thinks that the textual data has changed in between loading it and saving your changes to other fields so it warns you. Fortunately it doesn't go so far as to actually destroy your text data.

Optimistic Concurrency Control Error

Hi,

I have a table X:
ID (PK, int, not null)
cstID(FK, int, not null)
Name( nvarchar(100),not null)
Desc( ntext, null)

I am using the table view in Enterprise manager, if I manually type in a new row, then I edit that row, setting "Desc" = NULL, then I delete that row (from within the table view) I get the error:

Data has changed since the results pane was last retrieved. Do you want to save your changes now? (Optimistic Concurrency Control Error)

Things to note:
There was a FTI on this table, I deleted it, didn't help.
No other process or users are editing/viewing this table
The error doesn't occur if edit any other column, just setting the "Desc" to NULL creates this error.

Some other tables in my DB exhibit this same behavior, but not all......I can't figure out what the heck is going on...can you?

Can't anyone take a stab at this?

Some more information, I using SQL server 2005 so I am using MS not EM.

I created a duplicate table and that dup table doesn't have the same issue. So, I scripted both the bad table and the dup table, both scripts look identical sans the table names. I ran a trace and it doesn't look like anything different is happening between the original table and the dup table.

The table doesn't have any triggers.

Please help!

|||I get a similar message when I try to update any tables that contain fields of type bit, ntext, text, or image. But I only get it occasionally and cannot find a reason. I changed my ntext field to varchar and that eliminated the problem.|||

I can help you out here.

The fundamental cause of the problem is that Management Studio is rubbish.

It breaks down like this:

Management Studio can't handle edits on rows with char/varchar/text fields with more than 4000 characters of data (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=266305&SiteID=1)

One side -effect of this is that in optimistic mode Management Studio thinks that the textual data has changed in between loading it and saving your changes to other fields so it warns you. Fortunately it doesn't go so far as to actually destroy your text data.

Optimistic and Pessimistic Concurrency

Can any body explain about the Optimistic and Pessimistic Concurrency with
practically good example.
Thanks
Noor
Noor wrote:
> Can any body explain about the Optimistic and Pessimistic Concurrency
> with practically good example.
> Thanks
> Noor
Optimistic concurrency really means that the last user to update a row
in a table is probably updating with the most accurate values. Meaning,
no forced row locking is going on to keep the record off-limits while
one user edits a row (as you could do in the old dBase world) or no
check is done at update time to check whether another user updated the
row in the interim (the RDBMS way).
Pessimistic concurrency would either keep one user at a time editing a
row or perform some checking at update time to see if the row was
changed in the interim.
There is no effective way to manually lock rows in SQL Server like you
could in the dBase days. I only say "effective" because SQL Server, like
all RDBMSs, is designed to use a more concurrent locking paradigm to
keep data available. If you keep an extended exclusive lock on data, you
may inadvertently lock an entire data page and you would certainly keep
all users from reading that information, which is bad for concurrency.
To implement pessimistic concurrency in a table, add a TIMESTAMP column
to the table. A Timestamp column automatically changes value every time
a user updates a row.
As an example:
1- User A retrieves information from a table (the column list includes
the timestamp value)
2- User A edits the information from the client application
3- User B retrieves the same row from the table (and the same timestamp
value)
4- User B edit the information from the client application
5- User B saves the information before user A. In the stored procedure,
for example, the timestamp is checked to make sure it has not been
changed. Because the timestamp has not been changed while User B edited
the information, the update succeeds and a new timestamp value is
automatically generated by SQL Server.
An example could be:
Update Tablename
Set Col1 = @.NewVal
Where Col2 = @.PK_ID
and TS = @.RetrievedTimestampValue
If @.@.rowcount = 0
Print 'Another user updated the row'
Else
Print 'Updated!'
6- User A then attempts to save the information. The update fails
because the timestamp has changed. The application could then inform the
user of this and retrieve the current values again and begin a new edit
session.
Many applications do not require timestamps and can operate sufficiently
using optimistic concurrency. That just means if User A saves the
information last (as in the example above), it is assumed that User A
has the most up to date information.
David G.
|||On Wed, 25 Aug 2004 22:00:12 -0700, Noor wrote:

>Can any body explain about the Optimistic and Pessimistic Concurrency with
>practically good example.
>Thanks
>Noor
>
Hi Noor,
I believe David's answer is wrong.
Pessimistic concurrency: when a row is read with the intent of (possible)
update, it is locked. This lock will be held until the transaction is
completed (by ROLLBACK or COMMIT). This will prevent any updates to the
row from any other connection for the entire duration of the transaction;
there's no doubt that the row will be unchanged upon update.
Optimistic concurrency: no locks are held after reading the row. If the
row is updated later, a check is made to find out if another connection
has changed the row after the row was read. If that is the case, the
update is not carried out and the user gets an error message.
In transactions that run as one batch, pessimistic concurrency is fairly
standard. But if you have an application where the current data is shown
on screen, the user changes the data to reflect changes and saves it back
to the DB, then you'll have to decide which form of concurrency to use.
Pessimistic concurrency will prevent two users from changing the same data
at the same time, but at the price of also preventing two users from
seeing the same data at the same time (unless you duplicate all screens in
order to have "browse" versions and "edit" versions); optimistic
concurrency will reduce locking problems, but at the price of the
possibility that two people try to change the same row at once (and the
one to click "save" last will get an error and will have to enter his/her
changes again).
Final note: it is also possible to not use optimistic or pessimistic
concurrency at all, but this gives a great risk. Imagine two employees
reading the data of Customer 11765 at the same time. Employee #1 changes
the address and saves the changed data back to the database, but at the
same time, employee #2 changes the name of the primary contact and clicks
save. If employee #2 saves after #1, the old address will be put bacak
into the database (and if #1 saves last, the old primary contact will be
back).
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hugo Kornelis wrote:
> On Wed, 25 Aug 2004 22:00:12 -0700, Noor wrote:
>
> Hi Noor,
> I believe David's answer is wrong.
> Pessimistic concurrency: when a row is read with the intent of
> (possible) update, it is locked. This lock will be held until the
> transaction is completed (by ROLLBACK or COMMIT). This will prevent
> any updates to the row from any other connection for the entire
> duration of the transaction; there's no doubt that the row will be
> unchanged upon update.
> Optimistic concurrency: no locks are held after reading the row. If
> the row is updated later, a check is made to find out if another
> connection has changed the row after the row was read. If that is the
> case, the update is not carried out and the user gets an error
> message.
> In transactions that run as one batch, pessimistic concurrency is
> fairly standard. But if you have an application where the current
> data is shown on screen, the user changes the data to reflect changes
> and saves it back to the DB, then you'll have to decide which form of
> concurrency to use. Pessimistic concurrency will prevent two users
> from changing the same data at the same time, but at the price of
> also preventing two users from seeing the same data at the same time
> (unless you duplicate all screens in order to have "browse" versions
> and "edit" versions); optimistic concurrency will reduce locking
> problems, but at the price of the possibility that two people try to
> change the same row at once (and the one to click "save" last will
> get an error and will have to enter his/her changes again).
> Final note: it is also possible to not use optimistic or pessimistic
> concurrency at all, but this gives a great risk. Imagine two employees
> reading the data of Customer 11765 at the same time. Employee #1
> changes the address and saves the changed data back to the database,
> but at the same time, employee #2 changes the name of the primary
> contact and clicks save. If employee #2 saves after #1, the old
> address will be put bacak into the database (and if #1 saves last,
> the old primary contact will be back).
> Best, Hugo
Your final example is the same example I gave. It is optimistic
concurrency. And I really don't see hwo what you said is any different
than what I said.
David G.
|||On Thu, 26 Aug 2004 11:34:02 -0400, David G. wrote:

>Your final example is the same example I gave. It is optimistic
>concurrency. And I really don't see hwo what you said is any different
>than what I said.
Hi David,
What I understood from your post (but I may of course have misinterpreted
it), is that you say that:
* pessimistic concurrency uses ROWVERSION or TIMESTAMP to check for
updates by other users, and
* optimistic concurrency doesn't check for updates by others at all.
In my post, I write that:
* pessimistic concurrency completely locks rows to prevent updates by
others,
* optimistic concurrency uses ROWVERSION or TIMESTAMP to check for updates
by other users (called pessimistic concurrency in your post), and
* not checking for updates by others at all (called optimistic concurrency
at all) has no name, as this provides no concurrency control at all.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hugo Kornelis wrote:
> On Thu, 26 Aug 2004 11:34:02 -0400, David G. wrote:
>
> Hi David,
> What I understood from your post (but I may of course have
> misinterpreted it), is that you say that:
> * pessimistic concurrency uses ROWVERSION or TIMESTAMP to check for
> updates by other users, and
> * optimistic concurrency doesn't check for updates by others at all.
> In my post, I write that:
> * pessimistic concurrency completely locks rows to prevent updates by
> others,
> * optimistic concurrency uses ROWVERSION or TIMESTAMP to check for
> updates by other users (called pessimistic concurrency in your post),
> and * not checking for updates by others at all (called optimistic
> concurrency at all) has no name, as this provides no concurrency
> control at all.
> Best, Hugo
I would disagree with your definition of concurrency, which just means
simultaneous access to the database. I would argue that optimistic
concurrency just means that the last user to save data is the correct
one. It's optimistic because it makes the assumption the last person has
the correct data. Pessimistic means that you can never assume the last
person saving has correct information and to protect yourself from this
you either lock the row (bad with RDBMSs) or check a timestamp. In
either case, you are being pessimistic because you can't make the case
the last user has correct data.
But this is a matter of sematics really. The definitions are less
important that the reason for the implementation chosen for a given
system. If the OPs system supports a no lock, no timestamp
implementation, that is the best for performance. If not, they should
implement timestamps because the alternative of extended locking will
kill performance.
David G.
|||On Thu, 26 Aug 2004 13:06:25 -0400, David G. wrote:
(snip)
>But this is a matter of sematics really. The definitions are less
>important that the reason for the implementation chosen for a given
>system. If the OPs system supports a no lock, no timestamp
>implementation, that is the best for performance. If not, they should
>implement timestamps because the alternative of extended locking will
>kill performance.
Hi David,
I agree with that. Unfortunately (for her), the OP is not asking questions
about an implementation - she is doing a study and often asks questions to
clarify things she encountered in her books but didn't understand, or
asking self-test questions she could'n find the answer to.
So I guess that Noor is stuck with two answers of people who agree about
the different ways of concurrency control but disagree about the official
names.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||My understanding is that Hugo's definitions are correct and very well
stated! Here is what BOL says in the article entitled "Optimistic and
Pessimistic Concurrency":
Microsoft SQL ServerT 2000 offers both optimistic and pessimistic
concurrency control. Optimistic concurrency control uses cursors.
Pessimistic concurrency control is the default for SQL Server.
Optimistic Concurrency
Optimistic concurrency control works on the assumption that resource
conflicts between multiple users are unlikely (but not impossible), and
allows transactions to execute without locking any resources. Only when
attempting to change data are resources checked to determine if any
conflicts have occurred. If a conflict occurs, the application must read the
data and attempt the change again.
Pessimistic Concurrency
Pessimistic concurrency control locks resources as they are required, for
the duration of a transaction. Unless deadlocks occur, a transaction is
assured of successful completion.
David is right that definitions are 'less' important that the reason for
implementing a certain behavior, but it doesn't mean the definitions (or the
names) are not important at all. For example, when specifying attributes
when declaring a cursor through ADO, the lock type is specified as one of:
adLockReadOnly, adLockPessimistic, adLockOptimistic, or
adLockBatchOptimistic.
So knowing how ADO defines optimistic vs pessimistic is important.
HTH
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:u1dsi0p51lt4f1c97n67792qc4be4qftit@.4ax.com...
> On Thu, 26 Aug 2004 13:06:25 -0400, David G. wrote:
> (snip)
> Hi David,
> I agree with that. Unfortunately (for her), the OP is not asking questions
> about an implementation - she is doing a study and often asks questions to
> clarify things she encountered in her books but didn't understand, or
> asking self-test questions she could'n find the answer to.
> So I guess that Noor is stuck with two answers of people who agree about
> the different ways of concurrency control but disagree about the official
> names.
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
|||Thanks alot David
Noor
"David G." <david_nospam@.nospam.com> wrote in message
news:eZ5hAYziEHA.3632@.TK2MSFTNGP09.phx.gbl...
> Noor wrote:
> Optimistic concurrency really means that the last user to update a row
> in a table is probably updating with the most accurate values. Meaning,
> no forced row locking is going on to keep the record off-limits while
> one user edits a row (as you could do in the old dBase world) or no
> check is done at update time to check whether another user updated the
> row in the interim (the RDBMS way).
> Pessimistic concurrency would either keep one user at a time editing a
> row or perform some checking at update time to see if the row was
> changed in the interim.
> There is no effective way to manually lock rows in SQL Server like you
> could in the dBase days. I only say "effective" because SQL Server, like
> all RDBMSs, is designed to use a more concurrent locking paradigm to
> keep data available. If you keep an extended exclusive lock on data, you
> may inadvertently lock an entire data page and you would certainly keep
> all users from reading that information, which is bad for concurrency.
> To implement pessimistic concurrency in a table, add a TIMESTAMP column
> to the table. A Timestamp column automatically changes value every time
> a user updates a row.
> As an example:
> 1- User A retrieves information from a table (the column list includes
> the timestamp value)
> 2- User A edits the information from the client application
> 3- User B retrieves the same row from the table (and the same timestamp
> value)
> 4- User B edit the information from the client application
> 5- User B saves the information before user A. In the stored procedure,
> for example, the timestamp is checked to make sure it has not been
> changed. Because the timestamp has not been changed while User B edited
> the information, the update succeeds and a new timestamp value is
> automatically generated by SQL Server.
> An example could be:
> Update Tablename
> Set Col1 = @.NewVal
> Where Col2 = @.PK_ID
> and TS = @.RetrievedTimestampValue
> If @.@.rowcount = 0
> Print 'Another user updated the row'
> Else
> Print 'Updated!'
> 6- User A then attempts to save the information. The update fails
> because the timestamp has changed. The application could then inform the
> user of this and retrieve the current values again and begin a new edit
> session.
> Many applications do not require timestamps and can operate sufficiently
> using optimistic concurrency. That just means if User A saves the
> information last (as in the example above), it is assumed that User A
> has the most up to date information.
>
> --
> David G.
>
|||I really appreciated to all of you guys David, Hugo, Kalen
Thanks
NOOR
"Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
news:OQte737iEHA.2544@.TK2MSFTNGP10.phx.gbl...
> My understanding is that Hugo's definitions are correct and very well
> stated! Here is what BOL says in the article entitled "Optimistic and
> Pessimistic Concurrency":
> Microsoft SQL ServerT 2000 offers both optimistic and pessimistic
> concurrency control. Optimistic concurrency control uses cursors.
> Pessimistic concurrency control is the default for SQL Server.
> Optimistic Concurrency
> Optimistic concurrency control works on the assumption that resource
> conflicts between multiple users are unlikely (but not impossible), and
> allows transactions to execute without locking any resources. Only when
> attempting to change data are resources checked to determine if any
> conflicts have occurred. If a conflict occurs, the application must read
the
> data and attempt the change again.
> Pessimistic Concurrency
> Pessimistic concurrency control locks resources as they are required, for
> the duration of a transaction. Unless deadlocks occur, a transaction is
> assured of successful completion.
> David is right that definitions are 'less' important that the reason for
> implementing a certain behavior, but it doesn't mean the definitions (or
the
> names) are not important at all. For example, when specifying attributes
> when declaring a cursor through ADO, the lock type is specified as one
of:[vbcol=seagreen]
> adLockReadOnly, adLockPessimistic, adLockOptimistic, or
> adLockBatchOptimistic.
> So knowing how ADO defines optimistic vs pessimistic is important.
> --
> HTH
> --
> Kalen Delaney
> SQL Server MVP
> www.SolidQualityLearning.com
>
> "Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
> news:u1dsi0p51lt4f1c97n67792qc4be4qftit@.4ax.com...
questions[vbcol=seagreen]
to[vbcol=seagreen]
official
>