How do I set the correct transaction level?

How do I set the correct transaction level?



I am using Dapper on ADO.NET. So at present I am doing the following:


using (IDbConnection conn = new SqlConnection("MyConnectionString")))
{
conn.Open());
using (IDbTransaction transaction = conn.BeginTransaction())
{
// ...



However, there are various levels of transactions that can be set. I think this is the various settings.



My first question is how do I set the transaction level (where I am using Dapper)?



My second question is what is the correct level for each of the following cases? In each of these cases we have multiple instances of a web worker (Azure) service running that will be hitting the DB at the same time.



So what transaction do I use for the access that will be updating the processed column? And what transaction do I use for the other access that just needs to verify that the record is active?



In this case it's fine if a conflict causes the charge to not be run (we'll get it the next day). But it is critical that we not charge someone twice. And it is critical that the read to verify that the record is active succeed immediately while the other operation is in its transaction.



But it's key that the record stay consistent. And this includes the use case of "set NumUses = NumUses + @ParamNum" so it needs to treat the read, calculation, write of the column value as an atomic action. And if I am setting 3 column values, they all get written together.






How many invoicing jobs do you run at once? Can you not simply stop others from starting, while one is running?

– Alex
Sep 13 '18 at 4:29






To answer your question about transaction types: there is no transaction type that allows you to grant "selective" access to data. You will need to code this mechanism yourself.

– Alex
Sep 13 '18 at 4:35






Please elaborate on case 2. I am confused regarding deleting and updating records at the same time. Maybe some code sample will help.

– Alex
Sep 13 '18 at 4:42






@Alex 1) I set it so that only 1 service is running the invoicing. But they way cloud services work on Azure, when it flips to a different server, you can have the service running on 2 servers for a bit. 2) I don't think this is selective access, it's just not executing the second select until after the first transaction completes. Or throws an exception or returns nothing. 3) What if in this case, as I am updating a record, another service deletes that record? What happens then?

– David Thielen
Sep 13 '18 at 16:40




1 Answer
1



1) Assuming that Invoicing process is an SP with multiple statements your best bet is to create another "lock" table to store the fact that invoicing job is already running e.g.


CREATE TABLE InvoicingJob( JobStarted DATETIME, IsRunning BIT NOT NULL )

-- Table will only ever have one record
INSERT INTO InvoicingJob
SELECT NULL, 0


EXEC InvoicingProcess

ALTER PROCEDURE InvoicingProcess
AS
BEGIN
DECLARE @InvoicingJob TABLE( IsRunning BIT )

-- Try to aquire lock
UPDATE InvoicingJob WITH( TABLOCK )
SET JobStarted = GETDATE(), IsRunning = 1
OUTPUT INSERTED.IsRunning INTO @InvoicingJob( IsRunning )
WHERE IsRunning = 0
-- job has been running for more than a day i.e. likely crashed without releasing a lock
-- OR ( IsRunning = 1 AND JobStarted <= DATEADD( DAY, -1, GETDATE())

IF NOT EXISTS( SELECT * FROM @InvoicingJob )
BEGIN
PRINT 'Another Job is already running'
RETURN
END
ELSE
RAISERROR( 'Start Job', 0, 0 ) WITH NOWAIT


-- Do invoicing tasks
WAITFOR DELAY '00:01:00' -- to simulate execution time

-- Release lock
UPDATE InvoicingJob
SET IsRunning = 0
END



2) Read about how transactions work: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transactions-transact-sql?view=sql-server-2017



https://docs.microsoft.com/en-us/sql/t-sql/statements/set-transaction-isolation-level-transact-sql?view=sql-server-2017



You second question is quite broad.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)