8

To my problem: I call a stored procedure from my business code. This call is in a explicit transaction. The stored procedure sometimes calls another one to write something into the database. This data should stay in the database even when the transaction is rolled back. A similar scenario is when you want to write something in a log table and the log message should be kept (this is not my case, it is just a similar requirement).

How could I exclude the second stored procedure from the outer transaction? I think that I am looking for something like "autonomous transactions" in Oracle. I looked for a possible emulation but all the solutions didn't look very "nice" (create a loopback server, add some .NET methods, ...)

Any ideas? Thank you!

1

3 Answers 3

2

There is no elegant solution to this type of problem although it seems to be common. Everything between begin transaction and either commit or rollback is done as a whole. You cannot just insert a line into a log table for instance and keep that one after an eventual rollback.

But you can do some tricks.

1) Call your procedure with xp_cmdshell to call OSQL.exe. Performance would be bad, but external commands do not participate in the transaction and nothing keeps you from executing SQL statements externally.

2) in the stored procedure you could add the records into a table-variable instead of a real table. Table-variables do not participate in the transaction, as they do not alter the database. Afterwards append the content of the variable to your table when you closed the transaction in either way.

3) If you cannot change the inner procedure, you can fetch the record(s) that it possibly did create into a table variable after the call but from within the still open transaction. Rollback the transaction and append the fetched records to the table again.

1

Yes you can! Use a linked server to yourself and set the 'remote proc transaction promotion' to false. Here is an example:

EXEC master.dbo.sp_addlinkedserver @server = N'LOOPBACK', @srvproduct=N'Microsoft', @provider=N'SQLNCLI', @datasrc=N'MYMACHINE\INSTANCE', @catalog=N'DB_NAME_HERE'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'remote proc transaction promotion', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'rpc', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'rpc out', @optvalue=N'true'
--I think most below are defaults
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'collation name', @optvalue=null
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'query timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'LOOPBACK', @optname=N'use remote collation', @optvalue=N'true'

CREATE PROCEDURE dbo.ap_deleteme_outsideTransaction  AS
    --codes is just some random table I have
    exec ('insert into codes values (1, ''TEST'', ''TEST_ED'', ''DELTE_ME'', 0, ''1234'', ''myId'', getDate())') at LOOPBACK
GO

CREATE PROCEDURE dbo.ap_deleteme_test_transaction  AS
    begin transaction
        insert into codes values (10, 'TEST', 'TEST_ED', 'DELTE_ME_1', 0, '1234', 'myId', getDate())
        --exec ('generic query you may want to execute') at LOOPBACK
        exec dbo.ap_deleteme_outsideTransaction
        insert into codes values (20, 'TEST', 'TEST_ED', 'DELTE_ME_2', 0, '1234', 'myId', getDate())
    --rolling back like this makes no sense, but here you should be able to see 3 records 
    --inserted and then two rolled back.  The record inserted in the second proc call
    --will still remain.
    rollback transaction
GO

I would not call this optimal, but once you setup this link, you should be able to direct any call you do not want to participate in the transaction to this link and it will not participate. My initial tests do not show a significant performance hit, but be aware that making calls in a loop may prove to be costly.

-Update: Performance tests show it is 1-2 millisecond hit, but this is about 60x slower than a "direct" call. 500 or so hits is not noticeable, but when you get thousands, you can start to see seconds add up - not that this would be a common practice. We see that it far outweighs other side effects such as blocking, which is what we put this in place for.

5
  • Whow, that one is crazy - and works. Nice. Not sure where you got that from, but nice. Never thought about that :WELL done.
    – TomTom
    Aug 6, 2018 at 19:12
  • Lots o research. Was a big problem for us. We have container managed transactions and huge stored procs we call (would love to get rid of that). Getting nailed on some sequence number generation stuff and this will seem to do the trick.
    – Chewy
    Aug 6, 2018 at 20:15
  • Yeah. ALso can see that for logging calls out of a SP - when the SP rolls back you somehow may not want to loose the log ;) NICE. Smart. And seriously, look up my profile - that is rare praise from me. Love it
    – TomTom
    Aug 6, 2018 at 20:42
  • Thank you for your answer @Chewy. As I wrote in my question I was aware of the loopback server, but it looks kind of "crazy" to support a requirement that I thought that wasn't exotic at all! Oracle has got "autonomous transactions", so they had the requirement as well.
    – Raul
    Aug 14, 2018 at 8:31
  • Ah, I actually missed the last part of your comment about the loopback. I had been reading so many posts trying to figure out how to do this, that when I came back here, I missed it. Hopefully someone else who needs the idea will find this solution too. I spent a lot of time in the Oracle world and just assumed that this was easy - boy was I wrong. Of course I'm a java guy and spring will handle this for you.
    – Chewy
    Aug 14, 2018 at 15:14
0

To the best of my knowledge, you can't exclude specific operations from the transactions that contain them. My approach would be to try to explicitly do the things you want to do instead of rolling back the entire transaction. Or, if it's an option, rollback the transaction, and then re-run the stored procedure that you didn't want to rollback.

1
  • Thank you, but this is not feasible. The stored procedures are quite big, they could call the stored proc for storing some information multiple times. This information must be kept there but all other posible changes should rollback when something happens.
    – Raul
    Oct 8, 2014 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.