13.07.2015 Views

Software Engineering for Internet Applications - Student Community

Software Engineering for Internet Applications - Student Community

Software Engineering for Internet Applications - Student Community

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

You can solve all of these problems by separating SQL code andvariable data. Here's a pseudo-code example of how it has beendone using standard libraries going back to the late 1970s:// associate the name "event_query" with// a string of SQLPrepareStatement("event_query","select * from events where event_id = :event_id");// associate the bind variable :event_id with// the particular value <strong>for</strong> this pageBindVar("event_query",":event_id",3722);// ask the RDBMS to execute the completed queryExecuteStatement("event_query");... fetch results ...Note that the structure of the SQL seen by the RDBMS is fixed as"select * from events where event_id = :event_id",regardless of what input is received in the <strong>for</strong>m. Only the value of:event_id changes.This is an example of using bind variables, which is standard practicein most software that talks to an RDBMS.4.7 Bind Variables in C#using System;using System.Configuration;using System.Data;using System.Data.SqlClient;namespace ExecuteScalar{////// An example of how to use named parameters/// in ADO.NET.///class Class1{////// The main entry point <strong>for</strong> the application.///[STAThread]static void Main(string[] args)68This is a seemingly perverse way to use SQL but in fact is fairlyconventional because there are no IF statements in standard SQL.Suppose, however, that two copies of this INSERT startsimultaneously. Recall that a transaction processing system providesthe ACID guarantees: Atomicity, Consistency, Isolation, andDurability. Oracle's implementation of isolation, "the results of atransaction are invisible to other transactions until the transaction iscomplete", works by giving each user a virtual version of thedatabase as it was when the transaction started.Session ASends INSERT to Oracle atsystem change number ("SCN", apseudo-time internal to Oracle)30561.Oracle counts the rows inkm_object_views and finds 0.Oracle inserts a row intokm_object_views at SCN30567 (took a while <strong>for</strong> theCOUNT(*) to complete;meanwhile other users have beeninserting and updates rows inother tables).Session BSends INSERT to Oracle atsystem change number 30562,a tick after Session A startedits transaction but several ticksbe<strong>for</strong>e Session Aaccomplished its insertion.Oracle, busy with other users,doesn't start counting rows inkm_object_views until SCN30568, after the insert fromSession A. The database,however, will return 0 blocksbecause it is presentingSession B with a view of thedatabase as it was at SCN30562, when the transactionstarted.Having found 0 rows in thecount, the INSERT proceedsto insert one row, thus creatinga duplicate log entry.More: See the "Data Concurrency and Consistency" chapter ofOracle9i Database Concepts one of the books included in Oracledocumentation.Now consider the same query running in SQL Server:insert into km_object_views (user_id, object_id,table_name, view_time)281

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

Saved successfully!

Ooh no, something went wrong!