Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


April 21, 2008

Jump Start: Tracking Transactions

RSS
Subscribe to Windows IT Pro | See More SQL Server and Database Articles Here | Reprints
Or get the Monthly Online Pass—only $5.95 a month!

My "Using Transactions" article (InstantDoc ID 98827, www.sqlmag.com/articles/index.cfm?articleid=98827) prompted some questions from readers about how transactions work, when and where the data is written, and whether the data is put into the database before the transaction is committed.

 

SQL Server and SQL Server Express databases are stored in two types of files. Database data is stored in data files, which have an .mdf extension. Transactions are stored in transaction log files, which have an .ldf extension. The data is written to both the transaction log file and the data file.

 

When you use a BEGIN TRANSACTION statement to start a transaction, you essentially mark a set of related data in the transaction log. SQL Server can use that transaction log marker to pull the data out of the data file in the event of a ROLLBACK TRANSACTION operation.

 

The sample code below can help illustrate how transactions work. The code contains a multistatement transaction that consists of two INSERT statements.

 

USE Northwind

GO

 

BEGIN TRANSACTION MyTempRegion;

 

INSERT INTO Region (RegionID, RegionDescription)

 VALUES (5, 'Middle');

 

INSERT INTO Territories (TerritoryID, TerritoryDescription, RegionID)

 VALUES (55555, 'Chicago', 5);

 

-- The rows are added

SELECT TerritoryDescription, RegionDescription

FROM Territories INNER JOIN Region

ON Territories.RegionID = Region.RegionID

WHERE Territories.RegionID = 5

 

ROLLBACK TRANSACTION MyTempRegion;

 

-- The rows are gone

SELECT TerritoryDescription, RegionDescription

FROM Territories INNER JOIN Region

ON Territories.RegionID = Region.RegionID

WHERE Territories.RegionID = 5

 

The code first starts a transaction, which inserts data into two tables. The transaction then executes a SELECT statement. If you run the code, the results of that SELECT statement will show that data has been written to the database--no COMMIT statement is needed to write the data to the database. Because the INSERT and SELECT statements are within a transaction, the data is also written to the transaction log file and marked as a transaction.

 

Next, the ROLLBACK TRANSACTION statement is run, which causes the data that's marked in the transaction log to be pulled out of the database. Finally, another SELECT statement is executed. If you run the code, the second SELECT statement will show that the data that had originally been added to the tables is no longer there.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Managing Virtual Sprawl

As some wise person once said, nothing is ever truly free. Such is the case with VMs, which can quickly mutate from a cost-reducing Dr. Jekyll into a time-consuming, profligate nightmare that would do Mr. Hyde proud. ...

How can I uninstall the Microsoft Java Virtual Machine (JVM) from Windows XP?

...


SQL Server and Database Whitepapers SQL Server® 2005 – 64-Bit Migration Best Practices

It’s What You Make IT

Database Professionals: Experience Profile and Need Gaps in Development and Database Tools

Related Events Check out our list of Free Email Newsletters!

SQL Server and Database eBooks SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

A Guide to Windows Performance Tuning

Related SQL Server and Database Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Critical Challenges of ESI & Email Retention
Are you storing too much electronic information? Get expert legal advice and better understanding of what you are required to do as an IT professional.

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Sustainable Compliance: Are You Having a Resource Crisis?
Read this white paper to examine trends in compliance and security management and review approaches to reducing the cost and operational burden of compliance.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.
Windows IT Pro Home Register About Us Affiliates / Licensing Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing