17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Transaction 4<br />

This transaction wasn’t completed at the time of system failure and must therefore be rolled back. In<br />

effect, it never happened from a row-data perspective. The user would have to reenter any data, and any<br />

process would need to start from the beginning.<br />

Transaction 5<br />

This one is no different from Transaction 4. It appears to be different because the transaction has been<br />

running longer, but that makes no difference. The transaction was not committed at the time of system<br />

failure and must therefore be rolled back.<br />

Implicit Transactions<br />

Primarily for compatibility with other major RDBMS systems, such as Oracle or DB2, <strong>SQL</strong> <strong>Server</strong><br />

supports (it is off by default, but can be turned on if you choose) the notion of what is called an implicit<br />

transaction. Implicit transactions do not require a BEGIN TRAN statement — instead, they are automatically<br />

started with your first statement. They then continue until you issue a COMMIT TRAN or ROLLBACK<br />

TRAN statement. The next transaction then begins with your next statement.<br />

Implicit transactions are dangerous territory and are well outside the scope of this book. Suffice to say<br />

that I highly recommend that you leave this option off unless you have a very specific reason to turn it<br />

on (such as compatibility with code written in another system).<br />

Locks and Concur rency<br />

Chapter 14: Transactions and Locks<br />

Concurrency is a major issue for any database system. It addresses the notion of two or more users each<br />

trying to interact with the same object at the same time. The nature of that interaction may be different<br />

for each user (updating, deleting, reading, inserting), and the ideal way to handle the competition for<br />

control of the object changes depending on just what all the users in question are doing and just how<br />

important their actions are. The more users — more specifically, the more transactions — that you can<br />

run with reasonable success at the same time, the higher your concurrency is said to be.<br />

In the OLTP environment, concurrency is usually the first thing we deal with in data and it is the focus of<br />

most of the database notions put forward in this book. (OLAP is usually something of an afterthought —<br />

it shouldn’t necessarily be that way, but it is.) Dealing with the issue of concurrency can be critical to the<br />

performance of your system. At the foundation of dealing with concurrency in databases is a process<br />

called locking.<br />

Locks are a mechanism for preventing a process from performing an action on an object that conflicts with<br />

something already being done to that object. That is, you can’t do some things to an object if someone<br />

else got there first. What you can and cannot do depends on what the other user is doing. Locks are also<br />

a means of describing what is being done, so the system knows if the second process action is compatible<br />

with the first process or not. For example, 1, 2, 10, 100, 1,000, or whatever number of user connections<br />

the system can handle, are usually all able to share the same piece of data at the same time as long as<br />

they all want the record on a read-only basis. Think of it as being like a crystal shop: Lots of people can be<br />

in looking at things — even the same thing — as long as they don’t go to move it, buy it, or otherwise<br />

change it. If more than one person does that at the same time, you’re liable to wind up with broken crystal.<br />

That’s why the shopkeeper usually keeps a close eye on things and will usually decide who gets to<br />

handle things first.<br />

433

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!