---
title: "Using System.Transactions.TransactionScope to Rollback Database changes under an integration test"
url: "https://osherove.com/blog/2013/3/28/using-systemtransactionstransactionscope-to-rollback-databas.md"
canonical: "https://osherove.com/blog/2013/3/28/using-systemtransactionstransactionscope-to-rollback-databas.html"
published: "2013-03-28T22:51:05"
updated: "2013-03-28T22:51:05"
tags: ["Unit Testing"]
author: "Roy Osherove"
description: "It has been several years since I came up with the idea of database rolled back tests based on com+ transactions, which later became ServicedComponent tests, which later became TransactionScope aided tests. At some…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Using System.Transactions.TransactionScope to Rollback Database changes under an integration test

By Roy Osherove March 28, 2013 In [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published March 28, 2013; technical details may be outdated.

It has been several years since I came up with the idea of database rolled back tests based on com+ transactions, which later became ServicedComponent tests, which later became TransactionScope aided tests.

At some point, a DataRollback attribute was added to NUnIt, but them removed for some reason, that used this idea. I know it exists in MbUnit…. ANYWAY.

I never blogged about the TransactionScope solution in this blog,

so here is a very simple way to rollback database  changes that your code under test performs under an integration test, so that the DB is not dirtied up for the next test Just use System.Transactions.TransactionScope class:

## More in Unit Testing

[All posts in Unit Testing](https://osherove.com/blog/category/unit-testing)

- Apr 10, 2013

    ### [April 18: Refactoring and Design Skills for Test Driven Development Workshop](https://osherove.com/blog/2013/4/11/april-18-refactoring-and-design-skills-for-test-driven-devel.html)

- Mar 29, 2013

    ### [Acceptance Frameworks Vs. Specification APis](https://osherove.com/blog/2013/3/29/acceptance-frameworks-vs-specification-apis.html)

- Jul 12, 2012

    ### [Video: (part 8 - The big Refactoring) TDD Pairing a Go Game Engine](https://osherove.com/blog/2012/7/13/video-part-8-the-big-refactoring-tdd-pairing-a-go-game-engin.html)

- Jul 9, 2012

    ### [Video: (part 7 - The bug that wouldn't die) Go Game Engine with TDD and Pair Programming](https://osherove.com/blog/2012/7/9/video-part-7-the-bug-that-wouldnt-die-go-game-engine-with-td.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
