SQL Server - How to make SELECT block with TRANSACTION_SERIALIZABLE

I'm in the process of switching an application to use SQL Server. Is there anyway of making a SELECT in SQL Server implement a lock (and therefore also get blocked) similar to an INSERT or UPDATE?
Essentially, I have code that tracks a transaction ID for a particular customer; given there can be concurrent access from the same customer, I am using TRANSACTION_SERIALIZABLE:
long id = 0L; // Customer ID stmt = Conn.prepareStatement("SELECT TxID FROM TxIDTable WHERE (ID = ?)"); stmt.setLong(1, id); result = stmt.executeQuery(); result.next(); int tx_id = result.getInt("TxID"); result.close(); stmt.close(); stmt = Conn.prepareStatement("UPDATE TxID SET TxID = ? WHERE (ID = ?)"); stmt.setInt(1, tx_id + 1); stmt.setLong(2, id); stmt.executeUpdate(); stmt.close(); Conn.commit();
This code is working, however on SQL Server I have to wrap a loop around it, and if I catch a deadlock exception, rerun the transaction. This is costly and I would much rather have the SELECT create a lock and therefore block any concurrent SELECTs. One workaround is to force a dummy UPDATE up front:
long id = 0L; // Customer ID stmt = Conn.prepareStatement("UPDATE TxIDTable SET ID = ? WHERE (ID = ?)"); stmt.setLong(1, id); // THIS UPDATE DOES NOT REALLY UPDATE ANYTHING stmt.setLong(2, id); // (NOTE SET AND WHERE CLAUSE ARE IDENTICAL) stmt.executeUpdate(); // HOWEVER, IT DOES EFFECTLY PREVENT DEADLOCK // BY CREATING A TABLE LOCK BEFORE THE SELECT stmt.close(); // stmt = Conn.prepareStatement("SELECT TxID FROM TxIDTable WHERE (ID = ?)"); stmt.setLong(1, id); result = stmt.executeQuery(); result.next(); int tx_id = result.getInt("TxID"); result.close(); stmt.close(); stmt = Conn.prepareStatement("UPDATE TxIDTable SET TxID = ? WHERE (ID = ?)"); stmt.setInt(1, tx_id + 1); stmt.setLong(2, id); stmt.executeUpdate(); stmt.close(); Conn.commit();
This seems really like a waste -- anyway to make the SELECT lock on SQL Server??
Thanks,
Kevin

You want a table hint (the equivalent of SELECT ... FOR UPDATE in other dialects):
[http://msdn2.microsoft.com/en-us/library/ms187373.aspx|http://msdn2.microsoft.com/en-us/library/ms187373.aspx]

Similar Messages

  • Ssis sql command how to make it working with temp table #check , does not recognize though i created separate above data flow task a temp table through execute sql task.

    CREATE TABLE #check(
    reseller_account_id bigint NOT NULL PRIMARY KEY
    ,customer_id int NOT NULL
    INSERT INTO #check(reseller_account_id, customer_id)
    SELECT reseller_account_id, customer_id
    FROM ods.derived.postal_accounts pa
    JOIN ods.derived.bridge_accounts ba
    ON pa.bridge_id = ba.bridge_id
    AND pa.primary_multi_user_account_flag = 1
    WHERE promotion_code LIKE 'England-[01234]'
    AND reseller_account_id > 0;
    SELECT [reseller_id]
    ,c.reseller_account_id
    ,t.[postage_id]
    ,t.[customer_id]
    ,t.[mail_class_id]
    ,[mail_class_name]
    ,[mail_type_id]
    ,[mail_type_description]
    ,[org_zip_code]
    ,[dest_zip_code]
    ,[zone]
    ,t.[confirmation_number]
    ,[weight_category]
    ,[usps_postage_amount]
    ,[reseller_postage_amount]
    ,[cubic_pricing_flag]
    ,[destination_address]
    ,pe.recipient_address
    FROM ods.derived.v_reseller_prints_for_export t(NOLOCK)
    JOIN check_test c
    ON c.customer_id=t.customer_id
    JOIN ods.swsim.prints s(NOLOCK)
    ON t.postage_id=s.postage_id
    JOIN ods.postal.prints_extension pe(NOLOCK)
    on pe.postage_id=s.postage_id
    where t.customer_id IN (Select customer_id from #check)

    Hi Mark Subedi,
    In addtion to the above posts.
    The fragment referenced from the BOL
    You can create local and global temporary tables. Local temporary tables are visible only in the current session; global temporary tables are visible to all sessions.
    All local temporary tables are dropped automatically at the end of the current session.
    You can read more from the below link.
    https://technet.microsoft.com/en-us/library/aa258255(v=sql.80).aspx
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • How to Connect SQL Server enterprises Manager Version 8.0 with Form 6i

    Dear experts
    I want to know that How can I connect SQL server Enterprise Manager Version 8.0 with Forms/reprots 6i.
    currently I have oracle 9i database at server windows 2003 and I also connected with client (my PC) I work on server.
    I also have SQL server in that server 2003 machine and this SQL Server also work with other clients that have VB program.
    I want to know that can I connect with this SQL server with Form 6i?

    You can't. The closest thing would be to create links in an oracle database to the SQL Server database using Heteregeneous Services and connect forms to the that.
    This forum is for the SQLDeveloper tool. You will get more complete answers in the "Database General", or "Heterogeneous Services" forums

  • SQL Server 2012 PowerView & PoverPivot in combination with SharePoint 2013

    Hi,
    I have been reading articles on the internet about this, and I am little bit confused. I understand this is also changed since in SP2013. Could someone with experience clarify.
    1. It looks like Power View has become a part of Reporting Services and can not be installed without Reporting Services, is this correct? If this is true, then what is this
    http://www.microsoft.com/en-us/download/details.aspx?id=35577 If not, why do I see a lot of links referring to PowerView as a part of Reporting Services? for example
    http://www.codeproject.com/Articles/615462/Configure-Power-View-Reporting-Services-Features-o
    2. Do I need Analysis Services in order to use PowerView, is other words is Analysis Services a requirement?
    3. Where do I run the SQL Server 2012 Setup? I was assuming that I need to run it on SharePoint in order to add and register dll's on the SP servers, they are needed in order to create Service Applications (PowerView & PoverPivot). However in pictures
    on the
    http://www.codeproject.com/Articles/615462/Configure-Power-View-Reporting-Services-Features-o link I see they are adding features to SQL instance. So do we run it on SharePoint or SQL server? If we run it on SharePoint server, what to do with instance name?
    If on SQL server, how will SharePoint server get dll's in order to create Service Applications?
    4. Can SQL 2008 be used for PowerView & PoverPivot configuration databases? I mean a scenario when SharePoint has 2012 versions of PowerView & PoverPivot DLLs (i.e. PowerView & PoverPivot Service Applications created
    with SQL2012 version), but  configuration databases itself for these applications are in SQL 2008? I know that such a scenario was supported with SSRS on SP2010(you could simply run the SQL2012 setup on SP server and add SQL2012 SSRS, later when you configure
    SSRS in SharePoint mode SQL 2008 database could be used as a SSRS configuration database)...
    Thanks in advance!
    Tofig

    1. Part of the confusion comes from the fact that there are several versions of powerview: powerview for SharePoint and PowerView for Excel. But yes, for SharePoint integration it's part of SSRS. (If you're not talking about SharePoint, then you can create
    and use PowerView in Excel. No SSRS or SharePoint required. But, the link you provided is for PowerPivot for SharePoint. This provides additional functionality for folks that want to surface PowerPivot workbooks in SharePoint. (which is different than PowerView).
    (PowerPivot = tabular data mode in excel = the ability to create pivottables/charts with millions of rows of data in Excel with jaw dropping performance (sorry for the advertising speak, but in my experience, it's true).  PowerView = the ability to create
    dashboards / eye candy / other neat visualizations like maps for data that is stored in a tabular data model (PowerPivot or SSAS in tabular mode)).
    2. No. PowerView was originally designed to use "tabular data models" as their source of data. There are two ways to create a tabular data model: An SSAS instance in tabular mode, or an Excel Workbook with a powerPivot data model (PowerPivot is the excel
    version of the tabular data model). So, if you have a workbook that has data loaded in through PowerPivot, you can create a PowerView report in that workbook and surface it with excel services, with no SSAS needed.
    3. On a SharePoint box. The only way to install SSRS in SharePoint integrated mode is to install SSRS on a SharePoint WFE. (The databases they use will go on the SQL box, but the bits for SSRS go on the SharePoint box)
    4. According to the following doc, SharePoint 2013 only supports SSRS 2012 SP1 (which means only SQL 2012):
    http://msdn.microsoft.com/en-us/library/gg492257.aspx
    Mike G.

  • Whenever an slowness issue came in sql server how should I start?

    whenever a slowness issue or performance issue came in sql server how should I start?
    Please guide.
    Thanks

    Hi Ajay,
    All answer given above would be useful to you in some or the other scenario. if you are facing performance issue the approach you take determines how easily and quickly you can find solution.
    First thing is know your system how it works for that you need to first baseline a system. A system with 70 % CPU utilization would not always be issue but for some environment it can be. If you know your system very well moment performance issue  comes
    you would know where is the problem. Wait stats alone is never helpful you must be able to correlate wait stats to what is going on your system. If you know which query is causing issue its easy to see execution plan and tune it. If issue is server wide it
    can be due to missing index outdated stats slow disk subsystem etc. So first step is finding the cause. As Uri suggested wait stats are always helpful I use wait stats and refer to below whitepaper by Microsoft for SQL Server troubleshooting
    http://technet.microsoft.com/en-us/library/dd672789%28v=sql.100%29.aspx
    Remember important thing is knowing your system and that can come from base lining.
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it.
    My TechNet Wiki Articles

  • How to make relationship block mandatory?

    Hi,
    Please let me know how to make relationship block mandatory to create a business partner?
    Vinay

    Hi Vinay,
    Did you try to set the flag mandatory in the configuration of the relationship view for a field (e.g. relationship).
    Just go to the view in the component workbench, select your configuration, mark a field and go to Show Field Properties.
    Hope this helps.
    Regards, Thea

  • How to make text columns with adobe muse

    Hi,How to make text columns with adobe muse (like InDesign)?

    Multiple columns can be acheived with CSS - http://www.w3schools.com/css/css3_multiple_columns.asp
    div
    -moz-column-count:3; /* Firefox */
    -webkit-column-count:3; /* Safari and Chrome */
    column-count:3;
    I'm surprised that Muse does not support text columns yet, but perhaps the custom CSS can be added in style tags on page properties. Haven't tried it, but don't see why it wouldn't work.

  • How to make turning page with ibooks author?

    How to make turning page with ibooks author?

    This question has been answered on this thread
    https://discussions.apple.com/message/17981772#17981772#17981772
    Best regards.
    Alex

  • How to use SELECTION-SET with FREE-SELECTIONS?

    Hi,
    how to use SELECTION-SET with FREE-SELECTIONS?
    Regards,
    Vinay.

    Check http://help.sap.com/abapdocu_70/en/ABAPSUBMIT_SELSCREEN_PARAMETERS.htm

  • How to make this work with Firefox, HELP!

    Downloading for Real-player, after watching the full movie, I click download and it has to reread the movie from the internet. When using explorer, after downloading the movie, it reads it from memory, which makes it a fast download. How to make this work with Firefox, I like not to use Microsoft products, and I really like Firefox 7.0.1!!!! HELP!

    -> click '''Firefox''' button and click '''Options''' (OR File Menu -> Options)
    * Advanced panel -> Network tab
    * place Checkmark on '''Override Automatic Cache Management''' -> under '''Limit Cache''' specify a large size of space
    * Remove Checkmark from '''Tell me when websites asks to store data for offline use'''
    * click OK on Options window
    * Restart Firefox
    Check and tell if ts working.

  • How to make email link with a button with AC2 in flash cs3?

    How to make email link with a button with AC2 in flash cs3?
    I wrote this, but it does not work:
    btn_emailinfo.on (release) {
    getURL("mailto:"[email protected]");
    }

    I am guessing you put that on a frame?
    If so, the syntax is as follows:
    btn_emailinfo.onRelease = function(){
    getURL("mailto:[email protected]");
    Though, if you are placing it directly on the button itself,
    the syntax is:
    on(release){
    getURL("mailto:[email protected]");
    }

  • I reset my phone and it now receives calls that were meant for my husband. I know how to fix this with messaging and facetime, but can't seem to find how to make it stop with the calls. Please help.

    I reset my phone and it now receives calls that were meant for my husband. I know how to fix this with messaging and facetime, but can't seem to find how to make it stop with the calls. Please help.

    It may be due to Continuity
    The following quote is from  Connect your iPhone, iPad, and iPod touch using Continuity
    Turn off iPhone cellular calls
    To turn off iPhone Cellular Calls on a device, go to Settings > FaceTime and turn off iPhone Cellular Calls.

  • In JSF, how to make a menu with access control?

    In JSF, how to make a menu with access control?
    The access control can be guided by programming, database or other means if possible?
    Thanks

    I want to make a dvd menu in iMovie because i don't have IDVD and can't find anywhere to download it?
    For making DVDs I would recommend iMovie 06 and iDVD 09 both readily available on Amazon or eBay.  Shop for iLife 06 and iLife 09.
    You can make menus and chapters with any version of iMovie except the latest one. There's nothing wrong with iMovie 11 either but I prefer iMovie 06.
    By using iMovie 06 and iDVD 09 I make DVDs with professional moving menus with very little effort. They look almost as good as Hollywood.

  • How to make slide images with Dreamweaver CS6?

    How to make slide images with Dreamweaver CS6? Please teach me.

    Hello
    in addition to Jon's hint, I'll send you some links to nice sliders (have fun with the different representations ):
    http://sandbox.scriptiny.com/javascript-slideshow/
    http://jquery.malsup.com/cycle/
    http://wowslider.com/best-jquery-slider-crystal-linear-demo.html
    http://www.jcoverflip.com/demo
    http://www.jacksasylum.eu/ContentFlow/
    http://addyosmani.com/blog/jqueryuicoverflow/
    You only need to use the source code to implant these shows there where you want. In my eyes it would be the best that you use first a very new and blank DW file to perform (maybe in LiveView) the one or the other.
    Hans-Günter

  • How to create a database in SQL Server Express 2014 to be used with LV Database Toolkit

    I use LV Database Toolkit, which by default uses Jet database. Now I want to use SQL database with LV Database Toolkit. I have downloaded and installed "SQL Server 2014 Express with Tools" in my Labview Laptop. Now I want to create a database in SQL Server, because I think it is a requirement for creating a "udl connection file" (*.udl file). Please help me!
    Solved!
    Go to Solution.

    I made a database whose path is: C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\depot1
    I tried to make .udl connection file, but when I click on "test connection" with the provider: "OLEDB Provider for Microsoft SQL Server" I get the Error "UDL Connection File Failure" (attached below)
    When I tried to make the .udl connection file with the provider "Microsoft SQL native Client 11.0" and when I click on "Test Connection" I get the Error" UDL Native Client_1"& " UDL Native Client_2".
    Please help!
    Thanks!
    Attachments:
    UDL Connection File Failure.png ‏78 KB
    UDL with Native Client_1.png ‏702 KB
    UDL with Native Client_2.png ‏714 KB

Maybe you are looking for

  • Can't open quicktime movie that I exported from FCP 5. Help!

    Can't open quicktime movie that I exported from FCP 5. Help!

  • How to get ODI execution results into ESS log file

    Hi, In out project, we are launching ODI scenario from ESS. Once, we launch the scenarion from ESS, we did not know whether the scenario got executed successfully or not. Is there any way to get the ODI execution result (i.e,. Failure message or succ

  • Where is the photo?

    This may come as a stupid question, but where must I look to find a photo I just converted from .jpg to PDF using the 'Save As' option under File of Elements 5.0? The photo in question was pulled from a file folder with a vertical zipper on the left

  • Can two users stream photos into iPhoto's PhotoStream?

    Can my wife and I both user steam photos into iPhoto's PhotoStream?  We don't want two iPhoto libraries and would love if we could both stream when we take pictures with our iPhones. Thanks, Memo

  • Mixing Non JSF and JSF components

    Hi All, I wanted to mix non jsf and jsf components and I able to do this   <f:verbatim>                                 <input type="radio" name="selectBidder" value="#{currentRow['BID_ID]}"/>                             </f:verbatim>But the problem