Showing posts with label transactions. Show all posts
Showing posts with label transactions. Show all posts

Friday, March 30, 2012

manage transaction to avoid locks

Hi,
I am quite puzzled how SQLServer manages transactions.
Whatever the isolation level I set when performing an insertion, other
connections do not have access to the table in select mode.

Example in SQL Analyzer:
create table foo (
id numeric(10),
data varchar(100)
)

On Connection 1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
GO
BEGIN TRANSACTION
insert into foo(id,data) values (1,'data');

On Connection 2
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
select * from foo
-> QUERY HANGS

On Connection 1
COMMIT

On Connection 2
Get the result

Using READ COMMITTED level, I was expecting not to lock the table when
performing the select.

Thanks in advance for your help,
Cedric(extmb@.yahoo.fr) writes:
> I am quite puzzled how SQLServer manages transactions.
> Whatever the isolation level I set when performing an insertion, other
> connections do not have access to the table in select mode.
> Example in SQL Analyzer:
> create table foo (
> id numeric(10),
> data varchar(100)
> )
> On Connection 1
> SET TRANSACTION ISOLATION LEVEL READ COMMITTED
> GO
> BEGIN TRANSACTION
> insert into foo(id,data) values (1,'data');
> On Connection 2
> SET TRANSACTION ISOLATION LEVEL READ COMMITTED
> select * from foo
> -> QUERY HANGS
> On Connection 1
> COMMIT
> On Connection 2
> Get the result
> Using READ COMMITTED level, I was expecting not to lock the table when
> performing the select.

Why not? READ COMMITTED means just that, read committed data, and there
is uncommitted data in the table.

You can access the uncommitted data if you change the isolation level
for connection 2 to READ UNCOMMITTED.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Ok thanks,
However the question is how can I avoid dirty reads i.e. uncommitted
data without being locked ?
This corresponds to the default behaviour in Oracle.
Thanky you|||Concurrency control in SQL Server 2000 is done using locking. SQL Server
2005 introduces a new feature, snapshot isolation, that operates more like
the Oracle default you mentioned. There is a link to a whitepaper describing
the snapshot isolation feature as it is in SQL Server 2005 Beta 2 here:

http://msdn.microsoft.com/SQL/2005/...es/default.aspx

--
Alan Brewer [MSFT]
Content Architect
SQL Server Documentation Team

This posting is provided "AS IS" with no warranties, and confers no rights|||The scan from the second connection has to wait on the lock on the newly
insert row from the uncommitted transaction in connection1 because under
READ COMMITTED isolation it can't see dirty data.

The next SQL Server release will provide a new isolation level named
SNAPSHOT that will allow the second connection not to block on the
uncommited insert from the first connection, much like Oracle's scan
behavior.

--
Gang He
Software Design Engineer
Microsoft SQL Server Storage Engine

This posting is provided "AS IS" with no warranties, and confers no rights.
<extmb@.yahoo.fr> wrote in message
news:1111565523.911633.41290@.o13g2000cwo.googlegro ups.com...
> Hi,
> I am quite puzzled how SQLServer manages transactions.
> Whatever the isolation level I set when performing an insertion, other
> connections do not have access to the table in select mode.
> Example in SQL Analyzer:
> create table foo (
> id numeric(10),
> data varchar(100)
> )
> On Connection 1
> SET TRANSACTION ISOLATION LEVEL READ COMMITTED
> GO
> BEGIN TRANSACTION
> insert into foo(id,data) values (1,'data');
> On Connection 2
> SET TRANSACTION ISOLATION LEVEL READ COMMITTED
> select * from foo
> -> QUERY HANGS
> On Connection 1
> COMMIT
> On Connection 2
> Get the result
> Using READ COMMITTED level, I was expecting not to lock the table when
> performing the select.
> Thanks in advance for your help,
> Cedricsql

Wednesday, March 28, 2012

Making Transactions Work - Without Blocking

I'm having a very difficult time getting any type of transactions to work without encountering blocking.

Here's what I have. I created a package using the Import data option on a database to start with. That gave me the following:

On the control flow tab there is a Preparation SQL task which truncates a table followed by a Data Flow Task that then copies from an oracle database into that same table.

The two are connected by the green success arrow.

For obvious reasons, if the import from oracle fails, I would like the truncate of the table to be rolled back so that the table in question is returned to its previous state.

I have tried the following:

Setting the transactions setting on the data flow panel to "supported" and changing it on the Control Flow tab to required.

Setting both to required.

Setting the data flow setting to "supported", then adding a sequence container onto the control flow panel, moved both the preparation and data flow tasks into it and then set the sequence container to "required".

In all cases I have installed the package onto the server and then scheduled a job in SQL Server agent to run the package.

No matter what way I do it, the preparation sql task apparently opens a transaction and then the following data flow task starts a different one and is blocked by the first one. If you check a log file I set it to generate it clearly gets stuck after the sql preparation job when trying to start the data flow task. Checking sql server itself (Activity monitor), the job is stuck because it is blocked by process "-2".

So I'm lost as to how to make one single transaction be used for the entire package and get the behavior i need....

Thx.

R-

Robert,

I've had EXACTLY the same problem and to say I was annoyed is an understatement.

You won't like the answer - I don't think this can be solved. The -2 SPID is MSDTC (documented here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_wa-wz_3v8v.asp and here http://msdn2.microsoft.com/ms173730.aspx). It happens because the 2 tasks run under differrent connections. If they were the same connection it wouldn't be a problem.

I've mentioned a workaround here: http://blogs.conchango.com/jamiethomson/archive/2005/08/20/2048.aspx but it can only really be used in specific situations.

-Jamie

|||

Thanks for the information. Glad to know someone else has run into this also.

This really sucks. Deleting or turncating a table and then refreshing it with new data is a very common action. And it hink 100$ of the time you would want to be able to roll back the complete thing in the event of an error.

Maybe MS can explain to us how they anticpate you are supposed to perform this type of function?

|||

Thanks.

I did try that actually but received the error:

INCOMPATIBLE TRANSACTION CONTEXT

when it tried to do the dataflow task (step 2).

Monday, March 26, 2012

making sense of deadlock

Example,
1) Two transactions, A and B have a Shared Lock on a resource
2) Both intend to update the resource
3) Transaction A comes in an places an Update lock on the resource
4) Transaction A then tries to do a Exclusive Lock on the resource.
Shouldnt step 4 cause a deadlock since Transaction B is waiting on A to
release its locks, while Transaction A is waiting on B to release the Shared
lock before it can place the Exclusive Lock on the same resource?
Im a little ... thanks for any insight.
GNo, it won't cause a deadlock. Obtaining the update lock ensures that only
Transaction A can transition to an exclusive lock on the resource in order t
o
perform the update. Obtaining the update lock ensures that Transaction A
doesn't have to wait for Transaction B to release its shared lock before it
can transition to an exclusive lock to perform the modification.
READ: "Understanding Locking in SQL Server" in books online.
"Girish" wrote:

> Example,
> 1) Two transactions, A and B have a Shared Lock on a resource
> 2) Both intend to update the resource
> 3) Transaction A comes in an places an Update lock on the resource
> 4) Transaction A then tries to do a Exclusive Lock on the resource.
> Shouldnt step 4 cause a deadlock since Transaction B is waiting on A to
> release its locks, while Transaction A is waiting on B to release the Shar
ed
> lock before it can place the Exclusive Lock on the same resource?
> Im a little ... thanks for any insight.
> G
>
>

Wednesday, March 21, 2012

Make XACT_ABORT persist?

Hi,
I want my transactions to abort when a runtime error (i.e. foreign key const
raint violation) occurs. I currently accomplish this by prefixing all of my
queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continual
ly set this option on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
..which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent VegaJust turning that on will eventually help you with your present problems, bu
t will make a lot of trouble when leaving this as a global setting:
"Specifies whether Microsoft SQL ServerT automatically rolls back the curre
nt transaction if a Transact-SQL statement raises a run-time error."
I would rather do some error handling than just pushing in all data with ign
oring the error about that.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Vincent Vega" <Pulp@.Fiction.com> schrieb im Newsbeitrag news:aJadna7pIuwCfh
3fRVn-3w@.giganews.com...
Hi,
I want my transactions to abort when a runtime error (i.e. foreign key const
raint violation) occurs. I currently accomplish this by prefixing all of my
queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continual
ly set this option on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
..which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega|||I fully agree with Jens. If you still want to turn this on, you can at the i
nstance level using
sp_configure. Note that many tools etc are not designed to use this setting,
so be aware.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> wrote in
message
news:eDuF9kYVFHA.3312@.TK2MSFTNGP10.phx.gbl...
Just turning that on will eventually help you with your present problems, bu
t will make a lot of
trouble when leaving this as a global setting:
"Specifies whether Microsoft SQL ServerT automatically rolls back the curre
nt transaction if a
Transact-SQL statement raises a run-time error."
I would rather do some error handling than just pushing in all data with ign
oring the error about
that.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Vincent Vega" <Pulp@.Fiction.com> schrieb im Newsbeitrag
news:aJadna7pIuwCfh3fRVn-3w@.giganews.com...
Hi,
I want my transactions to abort when a runtime error (i.e. foreign key const
raint violation)
occurs. I currently accomplish this by prefixing all of my queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continual
ly set this option
on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
..which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega

Make XACT_ABORT persist?

Hi,
I want my transactions to abort when a runtime error (i.e. foreign key constraint violation) occurs. I currently accomplish this by prefixing all of my queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continually set this option on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
...which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega
Just turning that on will eventually help you with your present problems, but will make a lot of trouble when leaving this as a global setting:
"Specifies whether Microsoft SQL ServerT automatically rolls back the current transaction if a Transact-SQL statement raises a run-time error."
I would rather do some error handling than just pushing in all data with ignoring the error about that.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
"Vincent Vega" <Pulp@.Fiction.com> schrieb im Newsbeitrag news:aJadna7pIuwCfh3fRVn-3w@.giganews.com...
Hi,
I want my transactions to abort when a runtime error (i.e. foreign key constraint violation) occurs. I currently accomplish this by prefixing all of my queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continually set this option on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
...which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega
|||I fully agree with Jens. If you still want to turn this on, you can at the instance level using
sp_configure. Note that many tools etc are not designed to use this setting, so be aware.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> wrote in message
news:eDuF9kYVFHA.3312@.TK2MSFTNGP10.phx.gbl...
Just turning that on will eventually help you with your present problems, but will make a lot of
trouble when leaving this as a global setting:
"Specifies whether Microsoft SQL ServerT automatically rolls back the current transaction if a
Transact-SQL statement raises a run-time error."
I would rather do some error handling than just pushing in all data with ignoring the error about
that.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
"Vincent Vega" <Pulp@.Fiction.com> schrieb im Newsbeitrag
news:aJadna7pIuwCfh3fRVn-3w@.giganews.com...
Hi,
I want my transactions to abort when a runtime error (i.e. foreign key constraint violation)
occurs. I currently accomplish this by prefixing all of my queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continually set this option
on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
...which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega

Make XACT_ABORT persist?

Hi,
I want my transactions to abort when a runtime error (i.e. foreign key const
raint violation) occurs. I currently accomplish this by prefixing all of my
queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continual
ly set this option on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
..which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent VegaJust turning that on will eventually help you with your present problems, bu
t will make a lot of trouble when leaving this as a global setting:
"Specifies whether Microsoft SQL ServerT automatically rolls back the curre
nt transaction if a Transact-SQL statement raises a run-time error."
I would rather do some error handling than just pushing in all data with ign
oring the error about that.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Vincent Vega" <Pulp@.Fiction.com> schrieb im Newsbeitrag news:aJadna7pIuwCfh
3fRVn-3w@.giganews.com...
Hi,
I want my transactions to abort when a runtime error (i.e. foreign key const
raint violation) occurs. I currently accomplish this by prefixing all of my
queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continual
ly set this option on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
..which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega|||I fully agree with Jens. If you still want to turn this on, you can at the i
nstance level using
sp_configure. Note that many tools etc are not designed to use this setting,
so be aware.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> wrote in
message
news:eDuF9kYVFHA.3312@.TK2MSFTNGP10.phx.gbl...
Just turning that on will eventually help you with your present problems, bu
t will make a lot of
trouble when leaving this as a global setting:
"Specifies whether Microsoft SQL ServerT automatically rolls back the curre
nt transaction if a
Transact-SQL statement raises a run-time error."
I would rather do some error handling than just pushing in all data with ign
oring the error about
that.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Vincent Vega" <Pulp@.Fiction.com> schrieb im Newsbeitrag
news:aJadna7pIuwCfh3fRVn-3w@.giganews.com...
Hi,
I want my transactions to abort when a runtime error (i.e. foreign key const
raint violation)
occurs. I currently accomplish this by prefixing all of my queries with:
SET XACT_ABORT ON
Is there a way to set this option globally so that I don't have to continual
ly set this option
on each query? My first attempt to do this was:
EXEC sp_dboption 'MyDatabaseName', 'xact_abort', 'true'
..which returned the following error message:
Database option 'xact_abort' does not exist.
Any help would be appreciated. Thanks.
Sincerely,
Vincent Vega

Monday, March 19, 2012

Make Page in Report Invisible

Hi all,
I have a report which lists all transactions in a system. This report has
the parameters called From Date and To Date. Each transaction has a amount
value.
What I need is... if the transactions between the selected dates amounts sum
up to 0, then dont generate a page in the report..
What I receive at the moment is a whole heap of pages with 0 totals..
Any help would be greatly appreciated.
I have tried the hidden attribute... but as I am using the page footer this
does not get hidden.Can't you do this in the dataset by saying where total <> 0? You may want
to add a calculated field to the dataset then you can also apply a filter on
it.
"Clint" <ccpatriot12@.yahoo.com> wrote in message
news:uPDrQToNGHA.3832@.tk2msftngp13.phx.gbl...
> Hi all,
> I have a report which lists all transactions in a system. This report has
> the parameters called From Date and To Date. Each transaction has a
> amount value.
> What I need is... if the transactions between the selected dates amounts
> sum up to 0, then dont generate a page in the report..
> What I receive at the moment is a whole heap of pages with 0 totals..
> Any help would be greatly appreciated.
> I have tried the hidden attribute... but as I am using the page footer
> this does not get hidden.
>|||As craig said, do it in query itself. If its not possible within query go to
the filters for the dataset and specify the condition there.
Bye
Sumit Pilankar
"Craig" <craigm_richardson@.hotmail.com> wrote in message
news:%23XC4DapNGHA.3924@.TK2MSFTNGP14.phx.gbl...
> Can't you do this in the dataset by saying where total <> 0? You may want
> to add a calculated field to the dataset then you can also apply a filter
> on it.
> "Clint" <ccpatriot12@.yahoo.com> wrote in message
> news:uPDrQToNGHA.3832@.tk2msftngp13.phx.gbl...
>> Hi all,
>> I have a report which lists all transactions in a system. This report
>> has the parameters called From Date and To Date. Each transaction has a
>> amount value.
>> What I need is... if the transactions between the selected dates amounts
>> sum up to 0, then dont generate a page in the report..
>> What I receive at the moment is a whole heap of pages with 0 totals..
>> Any help would be greatly appreciated.
>> I have tried the hidden attribute... but as I am using the page footer
>> this does not get hidden.
>|||I cant do it in the query as that would involve grouping the dataset... as
the total is a summation of all the transactions. I have many case
statements as well..
I will look at the calculated field option.
Thanks
"Sumit Pilankar" <sumit.pilankar@.gmail.com> wrote in message
news:uJTxH0pNGHA.208@.tk2msftngp13.phx.gbl...
> As craig said, do it in query itself. If its not possible within query go
> to the filters for the dataset and specify the condition there.
> Bye
> Sumit Pilankar
>
> "Craig" <craigm_richardson@.hotmail.com> wrote in message
> news:%23XC4DapNGHA.3924@.TK2MSFTNGP14.phx.gbl...
>> Can't you do this in the dataset by saying where total <> 0? You may
>> want to add a calculated field to the dataset then you can also apply a
>> filter on it.
>> "Clint" <ccpatriot12@.yahoo.com> wrote in message
>> news:uPDrQToNGHA.3832@.tk2msftngp13.phx.gbl...
>> Hi all,
>> I have a report which lists all transactions in a system. This report
>> has the parameters called From Date and To Date. Each transaction has a
>> amount value.
>> What I need is... if the transactions between the selected dates amounts
>> sum up to 0, then dont generate a page in the report..
>> What I receive at the moment is a whole heap of pages with 0 totals..
>> Any help would be greatly appreciated.
>> I have tried the hidden attribute... but as I am using the page footer
>> this does not get hidden.
>>
>