Showing posts with label child. Show all posts
Showing posts with label child. Show all posts

Friday, March 9, 2012

Help needed for Transaction Support in SQL server 2005

Hi,

I have 2 stored procedure 1st insert the data in parent tables and return the Id. and second insert child table data using that parent table id as paramenter. I have foreign key relationship between these two tables also.

my data layer methods somewhat looks like

public void Save(order value)
{
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
{
int orderId = SaveOrderMaster(value);
value.OrderId = orderid;
int childId = SaveOrderDetails(value);
//complete the transaction
transactionScope.Complete();
}
}

here
1. SaveOrderMaster() calls an stored procedure InserOrderData which insert a new record in order table and return the orderId which is identity column in Order table.

2. SaveOrderDetails() call another sotored procedure which insert order details in to table "orderdetail" using the foreign key "orderid".

My Problem:
Some time the above method works correctly but when i call it repeatledly (in a loop) with data, some time it gives me foreign key error which state that orderid is not existsin table Order. This will happen only randomly. I am not able to figureout the reason. does some one face the same problem. if yes, what could be the reason and/or solution.

The problem may occur if your code calls the second procedure before the first procedure is done (or before the row has been added to the parent table). This can happen, because in your code you have not ensured the second procedure to call after the first procedure is completed. So either you can implement this logic in your code, or you can make use of trigger functionality in SQL. For example you can use an INSERT trigger on the parent table to automatically do the same thing as your second procedure did:

CREATE TRIGGER trg_InsOrdDetails ON Orders FOR INSERT
AS
--do your insert here, you can also call the second procedure

go

For more information about SQL trigger, you can refer to this link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_08_4nxu.asp

|||Jay

But those calls are from Businss Layer Dll which written in .NET. So when SaveOrderMaster(value) call returns it should update the record in database. because SaveOrderMaster(value) calls one stored procedure that returns the id.

so call is flowing in the following order:

UI calls BLL.Save(value) -->
BLL calls DAL.SaveOrderMaster(value) -->
DAL calls Stored Procedure and set the newly created id in value object.
BLL Calls DAL.SaveOrderDetails(values) -->
DAL calls stored procedure with the details and Id (created in SaveOrderMaster call).
and I am getting the foreignKey error while making call to SaveOrderDetails().

But I am not able to find the source of error. as this is not repeatative behaviour, some time it ouccurs after 5000 BLL.Save() calls.

I also cant use triggers here, as order detail data is huge. and i dont want to call my pass so many data to stored procedure in one go. (aka. business and design requirement -:) )

any help in this regard is highly appriciated?|||

When you have the error, what's the orderId in the parent table, and does it look like what you'd expect? Also, are you using identity(), or Scope_identity to get the key? If you're not using scope_identity(), then that could be your problem.

|||I am using the Scope_identity to retrive the value for the Order Table identity column.
and if i remove the Transaction, My SaveOrder method save the Master table entry while some time I am getting error while saving order details.|||

The TransactionScope class performs none atomic transactions which is not legal so pass your code to T-SQL transaction block and your problem will go away. Hope this helps.

http://www.codeproject.com/database/sqlservertransactions.asp

http://msdn2.microsoft.com/en-us/library/ms190295.aspx

Friday, February 24, 2012

Help Me read the data collected from Trace Flag 1204

Here is the data i got...from trace flag 1204..Could you guys help me read
this..like understand which is the problem child and which one is chosen as
a
victim any thing that helps me to understand this..
Deadlock encountered ... Printing deadlock information
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Wait-for graph
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:1
2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
Mode: IX Flags: 0x2
2006-09-07 03:05:18.92 spid4 Grant List 1::
2006-09-07 03:05:18.92 spid4 Owner:0x292a7440 Mode: IX Flg:0x0 Ref
:
1 Life:02000000 SPID:181 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 181 ECID: 0 Statement Type: UPDATE
Line #: 260
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Upd;1
2006-09-07 03:05:18.92 spid4 Grant List 2::
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: S SPID:6
2
ECID:0 Ec0x2C2D75A0) Value:0x7877bfa0 Cost0/0)
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:2
2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6) CleanCnt:
3
Mode: U Flags: 0x0
2006-09-07 03:05:18.92 spid4 Convert List:
2006-09-07 03:05:18.92 spid4 Owner:0x28d98fa0 Mode: X Flg:0x2 Ref
:
1 Life:02000000 SPID:210 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 210 ECID: 0 Statement Type: UPDATE
Line #: 1
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_upd_Finished;1
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X SPI
D:
181 ECID:0 Ec0x5D6435A0) Value:0x787ad1c0 Cost0/0)
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:3
2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6) CleanCnt:
3
Mode: U Flags: 0x0
2006-09-07 03:05:18.92 spid4 Grant List 1::
2006-09-07 03:05:18.92 spid4 Owner:0x327cc680 Mode: S Flg:0x0
Ref:1 Life:00000000 SPID:81 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 81 ECID: 0 Statement Type: SELECT
Line #: 1
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Sea_Sel;1
2006-09-07 03:05:18.92 spid4 Grant List 2::
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X SPI
D:
210 ECID:0 Ec0x1EF9D5A0) Value:0x28d98fa0 Cost0/1BC)
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:4
2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
Mode: IX Flags: 0x2
2006-09-07 03:05:18.92 spid4 Wait List:
2006-09-07 03:05:18.92 spid4 Owner:0x7877bfa0 Mode: S Flg:0x0
Ref:1 Life:00000000 SPID:62 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 62 ECID: 0 Statement Type: SELECT
Line #: 6
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_sel_ToSam;1
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS
SPID:81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
2006-09-07 03:05:18.92 spid4 Victim Resource Owner:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS SPID
:
81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
2006-09-07 03:05:18.92 spid4
Thanks in ADVANCE!
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200609/1read this article.
http://msdn.microsoft.com/library/d...
tabse_5xrn.asp
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"msqldba via droptable.com" <u11604@.uwe> wrote in message
news:65fced5a1d92d@.uwe...
> Here is the data i got...from trace flag 1204..Could you guys help me read
> this..like understand which is the problem child and which one is chosen
> as a
> victim any thing that helps me to understand this..
>
>
> Deadlock encountered ... Printing deadlock information
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Wait-for graph
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:1
> 2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
> Mode: IX Flags: 0x2
> 2006-09-07 03:05:18.92 spid4 Grant List 1::
> 2006-09-07 03:05:18.92 spid4 Owner:0x292a7440 Mode: IX Flg:0x0
> Ref:
> 1 Life:02000000 SPID:181 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 181 ECID: 0 Statement Type: UPDATE
> Line #: 260
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Upd;1
> 2006-09-07 03:05:18.92 spid4 Grant List 2::
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: S
> SPID:62
> ECID:0 Ec0x2C2D75A0) Value:0x7877bfa0 Cost0/0)
>
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:2
> 2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6)
> CleanCnt:3
> Mode: U Flags: 0x0
> 2006-09-07 03:05:18.92 spid4 Convert List:
> 2006-09-07 03:05:18.92 spid4 Owner:0x28d98fa0 Mode: X Flg:0x2
> Ref:
> 1 Life:02000000 SPID:210 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 210 ECID: 0 Statement Type: UPDATE
> Line #: 1
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_upd_Finished;1
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X
> SPID:
> 181 ECID:0 Ec0x5D6435A0) Value:0x787ad1c0 Cost0/0)
>
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:3
> 2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6)
> CleanCnt:3
> Mode: U Flags: 0x0
> 2006-09-07 03:05:18.92 spid4 Grant List 1::
> 2006-09-07 03:05:18.92 spid4 Owner:0x327cc680 Mode: S
> Flg:0x0
> Ref:1 Life:00000000 SPID:81 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 81 ECID: 0 Statement Type:
> SELECT
> Line #: 1
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Sea_Sel;1
> 2006-09-07 03:05:18.92 spid4 Grant List 2::
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X
> SPID:
> 210 ECID:0 Ec0x1EF9D5A0) Value:0x28d98fa0 Cost0/1BC)
>
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:4
> 2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
> Mode: IX Flags: 0x2
> 2006-09-07 03:05:18.92 spid4 Wait List:
> 2006-09-07 03:05:18.92 spid4 Owner:0x7877bfa0 Mode: S
> Flg:0x0
> Ref:1 Life:00000000 SPID:62 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 62 ECID: 0 Statement Type:
> SELECT
> Line #: 6
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_sel_ToSam;1
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS
> SPID:81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
> 2006-09-07 03:05:18.92 spid4 Victim Resource Owner:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS
> SPID:
> 81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
> 2006-09-07 03:05:18.92 spid4
> Thanks in ADVANCE!
> --
> Message posted via droptable.com
> http://www.droptable.com/Uwe/Forum...server/200609/1
>

Help Me read the data collected from Trace Flag 1204

Here is the data i got...from trace flag 1204..Could you guys help me read
this..like understand which is the problem child and which one is chosen as a
victim any thing that helps me to understand this..
Deadlock encountered ... Printing deadlock information
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Wait-for graph
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:1
2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
Mode: IX Flags: 0x2
2006-09-07 03:05:18.92 spid4 Grant List 1::
2006-09-07 03:05:18.92 spid4 Owner:0x292a7440 Mode: IX Flg:0x0 Ref:
1 Life:02000000 SPID:181 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 181 ECID: 0 Statement Type: UPDATE
Line #: 260
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Upd;1
2006-09-07 03:05:18.92 spid4 Grant List 2::
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: S SPID:62
ECID:0 Ec0x2C2D75A0) Value:0x7877bfa0 Cost0/0)
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:2
2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6) CleanCnt:3
Mode: U Flags: 0x0
2006-09-07 03:05:18.92 spid4 Convert List:
2006-09-07 03:05:18.92 spid4 Owner:0x28d98fa0 Mode: X Flg:0x2 Ref:
1 Life:02000000 SPID:210 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 210 ECID: 0 Statement Type: UPDATE
Line #: 1
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_upd_Finished;1
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X SPID:
181 ECID:0 Ec0x5D6435A0) Value:0x787ad1c0 Cost0/0)
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:3
2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6) CleanCnt:3
Mode: U Flags: 0x0
2006-09-07 03:05:18.92 spid4 Grant List 1::
2006-09-07 03:05:18.92 spid4 Owner:0x327cc680 Mode: S Flg:0x0
Ref:1 Life:00000000 SPID:81 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 81 ECID: 0 Statement Type: SELECT
Line #: 1
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Sea_Sel;1
2006-09-07 03:05:18.92 spid4 Grant List 2::
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X SPID:
210 ECID:0 Ec0x1EF9D5A0) Value:0x28d98fa0 Cost0/1BC)
2006-09-07 03:05:18.92 spid4
2006-09-07 03:05:18.92 spid4 Node:4
2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
Mode: IX Flags: 0x2
2006-09-07 03:05:18.92 spid4 Wait List:
2006-09-07 03:05:18.92 spid4 Owner:0x7877bfa0 Mode: S Flg:0x0
Ref:1 Life:00000000 SPID:62 ECID:0
2006-09-07 03:05:18.92 spid4 SPID: 62 ECID: 0 Statement Type: SELECT
Line #: 6
2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_sel_ToSam;1
2006-09-07 03:05:18.92 spid4 Requested By:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS
SPID:81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
2006-09-07 03:05:18.92 spid4 Victim Resource Owner:
2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS SPID:
81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
2006-09-07 03:05:18.92 spid4
Thanks in ADVANCE!
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200609/1read this article.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/trblsql/tr_servdatabse_5xrn.asp
--
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"msqldba via SQLMonster.com" <u11604@.uwe> wrote in message
news:65fced5a1d92d@.uwe...
> Here is the data i got...from trace flag 1204..Could you guys help me read
> this..like understand which is the problem child and which one is chosen
> as a
> victim any thing that helps me to understand this..
>
>
> Deadlock encountered ... Printing deadlock information
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Wait-for graph
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:1
> 2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
> Mode: IX Flags: 0x2
> 2006-09-07 03:05:18.92 spid4 Grant List 1::
> 2006-09-07 03:05:18.92 spid4 Owner:0x292a7440 Mode: IX Flg:0x0
> Ref:
> 1 Life:02000000 SPID:181 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 181 ECID: 0 Statement Type: UPDATE
> Line #: 260
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Upd;1
> 2006-09-07 03:05:18.92 spid4 Grant List 2::
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: S
> SPID:62
> ECID:0 Ec0x2C2D75A0) Value:0x7877bfa0 Cost0/0)
>
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:2
> 2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6)
> CleanCnt:3
> Mode: U Flags: 0x0
> 2006-09-07 03:05:18.92 spid4 Convert List:
> 2006-09-07 03:05:18.92 spid4 Owner:0x28d98fa0 Mode: X Flg:0x2
> Ref:
> 1 Life:02000000 SPID:210 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 210 ECID: 0 Statement Type: UPDATE
> Line #: 1
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_upd_Finished;1
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X
> SPID:
> 181 ECID:0 Ec0x5D6435A0) Value:0x787ad1c0 Cost0/0)
>
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:3
> 2006-09-07 03:05:18.92 spid4 KEY: 9:453576654:1 (6100f6c31ca6)
> CleanCnt:3
> Mode: U Flags: 0x0
> 2006-09-07 03:05:18.92 spid4 Grant List 1::
> 2006-09-07 03:05:18.92 spid4 Owner:0x327cc680 Mode: S
> Flg:0x0
> Ref:1 Life:00000000 SPID:81 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 81 ECID: 0 Statement Type:
> SELECT
> Line #: 1
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_Sea_Sel;1
> 2006-09-07 03:05:18.92 spid4 Grant List 2::
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: X
> SPID:
> 210 ECID:0 Ec0x1EF9D5A0) Value:0x28d98fa0 Cost0/1BC)
>
> 2006-09-07 03:05:18.92 spid4
> 2006-09-07 03:05:18.92 spid4 Node:4
> 2006-09-07 03:05:18.92 spid4 PAG: 9:1:5151886 CleanCnt:3
> Mode: IX Flags: 0x2
> 2006-09-07 03:05:18.92 spid4 Wait List:
> 2006-09-07 03:05:18.92 spid4 Owner:0x7877bfa0 Mode: S
> Flg:0x0
> Ref:1 Life:00000000 SPID:62 ECID:0
> 2006-09-07 03:05:18.92 spid4 SPID: 62 ECID: 0 Statement Type:
> SELECT
> Line #: 6
> 2006-09-07 03:05:18.92 spid4 Input Buf: RPC Event: sp_sel_ToSam;1
> 2006-09-07 03:05:18.92 spid4 Requested By:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS
> SPID:81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
> 2006-09-07 03:05:18.92 spid4 Victim Resource Owner:
> 2006-09-07 03:05:18.92 spid4 ResType:LockOwner Stype:'OR' Mode: IS
> SPID:
> 81 ECID:0 Ec0x403995A0) Value:0x1e480020 Cost0/0)
> 2006-09-07 03:05:18.92 spid4
> Thanks in ADVANCE!
> --
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200609/1
>