CLR trigger - handling multiple inserts at the same time

Hi
I've developed a CLR trigger which operates on inserts performed on a staging table. The trigger implements some business logic and then inserts or updates a record in a target table. Whether an insert or update is performed depends on whether
a record with the same ID already exists in the target (i.e. a select * from target where ID = 123).
This works fine in most scenarios, but occasionally I am getting duplicates in the target table and have noticed that this seems to occur when inserts on the staging table happen at exactly the same time (i.e. multiple inserts for the same ID at
the same time). In this situation duplicates are created in the target table because at the time of the inserts, no record with that ID exists in the target table (i.e. the select returns no records), therefore a new record is created for each.
Is there a known way to deal with this scenario? For example, would locking the target table on insert result in the subsequent selects against the target table waiting until the target table had been updated, therefore the select would return a record
for the given ID.
I didn't really want to lock the whole target table on insert, because there are potentially other users reading that table (selects) and these would also have to wait for the insert to complete.
I'd appreciate any thoughts on how to deal with this and avoid duplicates in the target table. I'm unable to change the way the data is coming in to the staging table, so my trigger code must deal with the above scenario.
Thanks in advance.
John

First if you do not want any duplicate values in a column (or combination of columns) you should add a constraint to ensure this is never possible. A
unique index
like this should do this trick.
CREATE UNIQUE NONCLUSTERED INDEX [IX_yourIndexName] ON [dbo].[YourTableName]
[yourColumn1] ASC,
-- add more columns that make the unique combination that you don't want repeated
You can then add a try/catch block in your trigger code, if you get an exception based on this index then the record was created by another executing instance of this trigger and in that case you should do an update (or not, not sure what the rest of your
logic is) in your catch block. This is the easiest solution and does not involve table locks. The only drawback is the first one to commit the insert will win and you have no guarantee which process or data set that will be. Also i have no idea how big the
table is, how frequently changes are made, and what the data type is so you should
keep this in mind when creating your index so you don't run into unexpected high index fragmentation which can lead to performance problems when executing updates and inserts.
You could also create a
named transaction with scope serializable around your insert/update block and execute your reads using a
NOLOCK hint
which should allow them to retrieve uncommitted writes and not create a long wait. The downside is is that the data might not be 100% accurate depending on if a transaction fails or not if there happens to be an update at the same time as a select but maybe
this is not a big deal to the calling code.
-Igor

Similar Messages

  • How to support multiple users at the same time?

    Hi all,
    I have a Labview software which controls a analyzer via GPIB connector. When user A is using the software, user B has to wait until A is done. But actually, the analyzer is not very busy, when user A is using the software.
    I want user A, B, C and D can use the software at the same time. just like we can open Multiple websites at the same time.
    How can I program the software, so that the software can be open multiple times? Do you have any example?
    Thanks a lot.
    Steven
    Solved!
    Go to Solution.

    Are all the users going to be measuring the exact same thing from the same instrument?
    It would be possible to write a separate instrument server application that would handle all the direct interactions with the instrument. When one of the users needed to access the instrument, they would gain access to it through this server.
    The big, Big, BIG caveat is that each interaction with the instrument would need to be atomic. In other words each access would need to configure the instrument, initiate the reading, report the results, and then reset the instrument back to some known default value.
    As Dennis said, not a trivial process, but probably do-able...
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • How does iCal, or Calendar, order the display of multiple events for the same time?

    I can't for the life of me figure out how iCal orders the display of multiple items at the same time.  Does anyone have a clue?  It's not alphabetical.  It's not according the the order of the calendars in the left pane.  Is it just random?  Any help would be greatly appreciated.

    In the focus lost eventhandling start a thread which does
    the actual handling.
    the new thread waits for a little time (0.1 seconds)
    the click event of the button checks for a thread like the one above and tells it not to execute
    This way you execute the for the lost focus event only if
    the ok button isn't clicked in the same process ... not
    realy good, because it kind of relies on the ordering of threads ... but maybe it still works for you?

  • Does af:commandButton submit multiple requests at the same time?

    Hi experts,
    I have a question about af:commandButton behavior.
    I have two commandButtons in a page. They are button1 and button2.
    Button1 takes a few seconds to complete its request process.
    So when I click button1, I can click button2 although button1 request is still being processed.
    I checked how ADF faces handled this situation with servlet filter.
    And I saw that button2 request was always submitted after button1 request was completed.
    Due to this behavior, I would assume that commandButton is designed not to submit multiple requests at the same time and guarantees click order.
    However I couldn't found any documents specifying this feature of commandButton.
    If anyone knows it, could you share?
    I know ADF Faces has busyStateListener to prevent user inputs during request round trip.
    But I'd like to make it sure that I don't need to use busyStateListener if I just want to keep processing order.
    Regards,
    Atsushi

    Hi,
    Does anyone know the document specifying the behavior of af:commandButton in case I click two buttons in a page almost simultaneously?
    Any help will be much appreciated.
    Thanks in advance,
    Atsushi

  • Can you use sync on multiple computers at the same time? ie have someone else logged into your firefox account for you whilst you are also logged in

    I am trying to sign up to something but there is only a short window so was going to ask a friend to sign in at the same time using my account to try to sign up (with my password being saved into my firefox account)

    Hi. Yes, you can use Sync on multiple devices at the same time. I use it to keep my bookmarks in sync across my desktop and Android versions of Firefox.

  • Is there a way to delete multiple pictures at the same time from the iphone4s?

    Is there a way to deleter multiple pictures at the same time, from my iphone4s? I know how to delete one at a time. Thanks

    Open your Photos App > Camera Roll > At the top right corner you will see a rectangle with a right arrow, select that. Now you can select as many photos as you want and you can hit the red Delete button on the bottom right.

  • CRVS2010 - open multiple rpts at the same time, like sheets in a xls file ?

    Hi all,
    is that possible to open multiple reports at the same time, like sheets in a excell file ?
    Is that possible create tabs beside the MainReport tab ? or include reports in the Group Tree ? how can I do it ?
    The application is in asp.net with VS2010 and CR2010.
    The reports used to be in excell with three sheets and now we are migrating them to crystal reports, how can I create the report with those three sheets ?
    thanks !
    Subject modified as per the sticky post at the top of this forum; [Crystal Reports for Visual Studio 2010 Beta - read before posting|Crystal Reports for Visual Studio 2010 Beta - read before posting;
    Edited by: Ludek Uher on Aug 31, 2010 7:33 AM

    I'll make the suggestion to the Program Management group.
    You may want to do a post to the [Crystal Reports Design|SAP Crystal Reports; forum and ask them how they's fake that sort of a requirement. E.g.; there may be some way to make this report look the way you want, but it will be best to ask the experts. Don't mention CRVS2010 as they don't support it and most likely you will not get an answer.
    Ludek

  • Can you opening multiple albums at the same time in Photo (not iPhoto)?

    When converting the iPhoto library to Photo most of the albums that were in place in iPhoto converted to albums in Photo . . . .but not all. Some were retained as events mainly those that were folders within an album. In order to move them to the right album I would like to have multiple albums open at the same time (side by side) so that I can make sure not to duplicate photos or move them to the wrong spot.
    I cant see how to do this, it seems that I can need to open one album and check phots and then close to look within and other and rely on memory. There must be a more efficient and accurate way to do this?

    All of the events in iPhoto were converted to albums and are located in the Folder titled iPhoto Event in the sidebar of Photos. 
    You can't open multiple albums at the same time.  You can create a smart album that will do essentially the same thing with the following criteria:
    Album is "A"
    Album is "B".
    The smart album will display all of the photos in those two albums.

  • In Elements 7.0, how can I delete multiple photos at the same time?

    In Adobe Photoshop Elements 7.0, how can I delete multiple photos at the same time?   Please be a little specific so I can follow your instructions as I'm not real smart at computers; I'm an intermediate user.  Thank you.

    Shift-click the photos or Ctrl-click them to select them, then right-click one of them and choose Delete from Catalog. They will all go together.

  • How do you apply a master page to multiple documents at the same time?

    How do you apply a master page to multiple documents at the same time?

    Hi friends,
    Thank you for trying to help me out.
    Let me explain it a bit to remove the ambiguity.
    I have 10 documents nested under a book. Each of these documents have 'n' number of pages. I want to apply my custom made master page "First" to the first page of all these 10 documents in one go. The remaining pages of the documents have to be in default "Right" master page format. How will I do it?
    I tried selecting all the documents and importing the formats from another document with the custom made "First" master page. The master page format is getting imported but the first page of all the documents still remain with the default "Right" master page format.
    I think now my question is more clear...

  • WDS 2012 R2 - Cannot PXE multiple clients at the same time

    Hello All,
    This is my first post on here so I apologize if this is the wrong place.  I work for a school district and we are implementing WDS 2012 R2.  We've been extremely satisfied with the speeds and ease of use through unattend files.  However, for
    the past month I've been looking for a possible answer to a problem that has plagued us from day one of implementation.
    So here's the problem:
    I have a stand alone WDS server which is not a domain controller and is not our DHCP server.  I have IP helpers and broadcast forwarders setup on the network.  As well as option 66 and 67 in DHCP.  So far so good right!
    Well that's partially right.  When we boot one client at a time to the WDS server.  Everything works as intended.  We can TFTP the necessary files from the WDS server.  Everything boots up and we're off and running.
    However, if we boot up two or more clients at the same time.  The WDS server never responds to the traffic.  The clients get their DHCP information.  They start the referral and download from the WDS server, but get no response.  I'm
    really hoping that someone on here would have some insight of something I can try.  I've about exhausted my list of peers and contacts.  They're all stumped as well and were smart enough to stay with 2008 WDS.
    I would prefer to stick with 2012 R2 since it's setup and working for the most part.  With only this one hiccup.
    Thanks in advance for any guidance!

    Hello Daniel,
    I appreciate the reply and apologize for taking so long to get back to this.   Things have been a little hectic over here.
    I have tried everything on this forum and I am still unsuccessful in PXE booting multiple clients at the same time.
    Multicast is enabled on the server, and it works for the clients.  However, as stated in the original post.  I cannot boot multiple machines at the same time.  I can start them from the image selection screen around the same time though.  So,
    that appears to be working fine.

  • Lightroom CC issues with multiple export at the same time

    Back before Lightroom updated to the CC version, I was able to use export presets to send 4 different versions of the photos I'm working on at the same time (A Full Size for client at certain size, full size for myself, web-sized with watermark and a thumbnail). Since updating to the CC version, I'm only able to start the export process on two of these versions before the system simply becomes crippled and I cannot even navigate through Lightroom without it chugging along.
    For reference, I'm running a 2014 rMBP with 16GB of RAM, LR is the only app open at the time and it's showing that I have roughly 6GB of RAM free when attempting this export.
    TL;DR: LR5 worked wiht multiple exports at the same time, but LRCC now chugs after 2 exports are started

    Re: "Selecting video in Messages, now starts Face time for video. Face time is one to one only."
    Apple "Messages" supports two different types of video conferencing. It's become more than a little confusing. If you (or the person on the other end) ONLY have accounts set up for Apple's "iMessage" service (associated with your Apple ID and "iCloud") you may see the video symbol, but when you click on it you will be connected via FaceTime which is limited a 1-to-1 video conference.
    However, if you have set up an AIM account in Messages (you may already have an AIM account ID if you once used iChat for video, audio and screensharing) and the person at the other end also has an AIM account you will be connected via AIM and will be able to do screen sharing and multi-person video conferences just like the old iChat, i.e. you'll have iChat Theater features, up to 4-way conferencing, etc.
    It can get very confusing if, for example, you've signed up for AIM using a .me or .cloud email address. But once you have it set up and learn to recognise the way Messages switches between AIM and iMessage all the old iChat features work just fine in Messages.

  • Run invoke-command on multiple machines at the same time

    Hey all so I read that if I store my New-pssession in a variable then used that in my invoke-command it would run all computers at the same time.
    $a = Get-Content "C:\Users\cody-horton\Desktop\list.txt"
    $session
    for($i=0;$i -lt $a.Length;$i++){
    if(!(Test-Connection -Cn $a[$i] -BufferSize 16 -Count 1 -ea 0 -quiet)){
    Write-Host $a[$i] -foregroundcolor red
    else{
    $session = New-PSSession $a[$i]
    Invoke-Command -Session $session -FilePath "\\My computer\C`$\Users\public\Documents\zip folder.ps1"
    What exactly am I doing wrong I just need to run this script on multiple machines at the same time.
    Thanks.
    Edit: Also what would be the best way to close all the sessions thanks.

    Hi there,
    So what I think you are doing wrong here is that you are overwriting the value in $Session everytime you executed code inside for loop. try the below:
    $a = Get-Content "C:\Users\cody-horton\Desktop\list.txt"
    $session = @() #define this as an array
    for($i=0;$i -lt $a.Length;$i++){
    if(!(Test-Connection -Cn $a[$i] -BufferSize 16 -Count 1 -ea 0 -quiet)){
    Write-Host $a[$i] -foregroundcolor red
    else{
    $session += New-PSSession $a[$i] #add the new session to the array, at the end it will be a collection of sessions
    Invoke-Command -Session $session -FilePath "\\My computer\C`$\Users\public\Documents\zip folder.ps1" #I think the above one won't work..first you need to copy the script locally on the machine and then execute it#Why this won't work because of Second-Hop Authentication
    Have put comments where I edited your code.
    Hope this helps
    Knowledge is Power{Shell}. http://dexterposh.blogspot.com/

  • Accessing multiple portals at the same time?

    Is it possible to access multiple portals at the same time?
    For example, what I want to achieve is different properties (layout,
    portlets, look & feel) for different groups of users accessing the same
    portal. The Associated Groups part on the Portal admin page is not
    fulfilling our requirements. So we decided to have different portals for
    different groups of users, all working through one portal, and accessing
    their custom portals. Is this achievable?
    What we are thinking is: put the common functionality in the repository
    portal directory, and the custom portlets/jsps in the group-specific portal
    directories. This way we can customize portal behavior for different groups
    of users. Is this achievable?
    Thanks.
    Amit

    You have to user respective DRILL commands present in WAD to configure the drill operations on multiple characteristics...

  • Can I run the currency translation for multiple years at the same time?

    Dear Friends,
    I set up currency translation on our BPC NW 7.0 system and it is working fine ONLY if I run it for any one year at a time. But if I select multiple years in the data package (FX Restatment) selection it only calculates for the first year in that selection.
    say if I run the data package for years: 2009, 2010, 2011, 2012, it only performs currency translation for the year 2009.
    Can I run the currency translation for multiple years at the same time ?
    Note: The year is determined from the YEAR property of the "Time" dimension
    appreciate any inputs,
    thanks

    You can run currency translation for multiple years.
    I think the problem it is actually related to how you are sending the parameter to currency translation script.
    Just review the script logic log and you will see with what parameters was called currency translation FXTrans.
    My expectation is that actually the script is called just with first year and that's the problem.
    Log of script logic you can find it into:
    C:\BPC|DATA\Webfolder\YourAppset\YourApplication\Private Publications\Users used to run logic\Temp
    Regards
    Sorin Radulescu

Maybe you are looking for

  • Need help to start a prject

    i am fairly new to JSP. I need to create a form(html,jsp) that interacts with oracle database (help desk ticketing system to input problems). I am using jdeveloper as a software to develop it. Are there any tutorial or some kind of documentation avai

  • How can this encoding be saved in SQL Server 2008

    hi all, We are using Sql Server 2008, it has this collation set "SQL_Latin1_General_CP1_CI_AS". Please have a look at this file: https://drive.google.com/file/d/0BxWAyvJA9ZjCdkFNN3BXZUw3dEE/view?pli=1 its a text file in text format (ascii test data f

  • Add multiple scans to the same pdf file scan at time of scanning

    Hello, HP Officejet Pro 6830 How do I get it to add multiple scans to the same pdf file? I need to scan multiple documents and have them all end up in one pdf. Some may be double-sided and it is fine if I have to scan them individually. At present it

  • New tabs created with + not useable or closing. Google toolbar interaction!

    Using FF25.0 If I create a new tab by middle clicking on a link on a displayed page it works fine. but if I click on the + at the end of the tab bar then select it and type an URL in: Nothing! And the tab then sometimes has no closing cross, even if

  • Preserving Chapter Cross References when going to Structured Framemaker

    Hello, I am trying to preserve all of my cross references that link from chapter to chapter, within the same book. If I convert files one at a time, then FrameMaker has trouble finding the link in the destination chapter - because that chapter has no