05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 6 ■ LOCKING AND LATCHING 185<br />

were simply reinforced <strong>and</strong>, furthermore, I learned to treat a lock as a very scarce resource—<br />

something to be coveted. I learned that you should manually escalate locks from row level to<br />

table level to try to avoid acquiring too many of them <strong>and</strong> bringing the system down, <strong>and</strong><br />

bring it down I did—many times.<br />

When I started using <strong>Oracle</strong>, I didn’t really bother reading the manuals to find out how<br />

locking worked in this particular database. After all, I had been using databases for quite a<br />

while <strong>and</strong> was considered something of an expert in this field (in addition to Sybase, SQL<br />

Server, <strong>and</strong> Informix, I had used Ingress, DB2, Gupta SQLBase, <strong>and</strong> a variety of other databases).<br />

I had fallen into the trap of believing that I knew how things should work, so I thought<br />

of course they would work in that way. I was wrong in a big way.<br />

It was during a benchmark that I discovered just how wrong I was. In the early days of<br />

these databases (around 1992/1993), it was common for the vendors to “benchmark” for really<br />

large procurements to see who could do the work the fastest, the easiest, with the most features.<br />

The benchmark was between Informix, Sybase, SQL Server, <strong>and</strong> <strong>Oracle</strong>. <strong>Oracle</strong> was first.<br />

Their technical people came on-site, read through the benchmark specs, <strong>and</strong> started setting it<br />

up. The first thing I noticed was that the technicians from <strong>Oracle</strong> were going to use a database<br />

table to record their timings, even though we were going to have many dozens of connections<br />

doing work, each of which would frequently need to insert <strong>and</strong> update data in this log table.<br />

Not only that, but they were going to read the log table during the benchmark as well! Being<br />

a nice guy, I pulled one of the <strong>Oracle</strong> technicians aside to ask him if they were crazy—why<br />

would they purposely introduce another point of contention into the system? Wouldn’t the<br />

benchmark processes all tend to serialize around their operations on this single table? Would<br />

they jam the benchmark by trying to read from this table as others were heavily modifying it?<br />

Why would they want to introduce all of these extra locks that they would need to manage?<br />

I had dozens of “Why would you even consider that?”–type questions. The technical folks<br />

from <strong>Oracle</strong> thought I was a little daft at that point. That is, until I pulled up a window into<br />

SQL Server or Informix, <strong>and</strong> showed them the effects of two people inserting into a table, or<br />

someone trying to query a table with others inserting rows (the query returns zero rows per<br />

second). The differences between the way <strong>Oracle</strong> does it <strong>and</strong> the way almost every other<br />

database does it are phenomenal—they are night <strong>and</strong> day.<br />

Needless to say, neither the Informix nor the SQL Server technicians were too keen on the<br />

database log table approach during their attempts. They preferred to record their timings to<br />

flat files in the operating system. The <strong>Oracle</strong> people left with a better underst<strong>and</strong>ing of exactly<br />

how to compete against SQL Server <strong>and</strong> Informix: just ask the audience “How many rows per<br />

second does your current database return when data is locked?” <strong>and</strong> take it from there.<br />

The moral to this story is twofold. First, all databases are fundamentally different. Second,<br />

when designing an application for a new database platform, you must make no assumptions<br />

about how that database works. You must approach each new database as if you had never<br />

used a database before. Things you would do in one database are either not necessary or simply<br />

won’t work in another database.<br />

In <strong>Oracle</strong> you will learn that<br />

• Transactions are what databases are all about. They are a “good thing.”<br />

• You should defer committing until the correct moment. You should not do it quickly to<br />

avoid stressing the system, as it does not stress the system to have long or large transactions.<br />

The rule is commit when you must, <strong>and</strong> not before. Your transactions should only<br />

be as small or as large as your business logic dictates.

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

Saved successfully!

Ooh no, something went wrong!