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.

32<br />

CHAPTER 1 ■ DEVELOPING SUCCESSFUL ORACLE APPLICATIONS<br />

The theoretical advantage of the first approach is that to move from database to database<br />

you need not change anything. I call it a “theoretical” advantage because the downside of this<br />

implementation is so huge that it makes this solution totally infeasible. What you would have<br />

to do to develop a totally database-independent process is to create a table such as<br />

ops$tkyte@ORA10G> create table id_table<br />

2 ( id_name varchar2(30) primary key,<br />

3 id_value number );<br />

Table created.<br />

ops$tkyte@ORA10G> insert into id_table values ( 'MY_KEY', 0 );<br />

1 row created.<br />

ops$tkyte@ORA10G> commit;<br />

Commit complete.<br />

Then, in order to get a new key, you would have to execute the following code:<br />

ops$tkyte@ORA10G> update id_table<br />

2 set id_value = id_value+1<br />

3 where id_name = 'MY_KEY';<br />

1 row updated.<br />

ops$tkyte@ORA10G> select id_value<br />

2 from id_table<br />

3 where id_name = 'MY_KEY';<br />

ID_VALUE<br />

----------<br />

1<br />

Looks simple enough, but the outcomes (notice plural) are as follows:<br />

• Only one user at a time may process a transaction row. You need to update that row to<br />

increment a counter, <strong>and</strong> this will cause your program to serialize on that operation. At<br />

best, one person at a time will generate a new value for this key.<br />

• In <strong>Oracle</strong> (<strong>and</strong> the behavior might be different in other databases), all but the first user<br />

to attempt to concurrently perform this operation would receive the error “ORA-08177:<br />

can’t serialize access for this transaction” in the SERIALIZABLE isolation level.<br />

For example, using a serializable transaction (which is more common in the J2EE environment,<br />

where many tools automatically use this as the default mode of isolation, often<br />

unbeknownst to the developers), you would observe the following behavior. Notice that the<br />

SQL prompt (using the SET SQLPROMPT SQL*Plus comm<strong>and</strong>) contains information about which<br />

session is active in this example:

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

Saved successfully!

Ooh no, something went wrong!