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 223<br />

SQL, or unique SQL for each statement). To do this, we’ll use a very small Java program that<br />

simply logs into <strong>Oracle</strong>, turns off auto-commit (as all Java programs should do immediately<br />

after connecting to a database), <strong>and</strong> executes 25,000 unique INSERT statements in a loop. We’ll<br />

perform two sets of tests: our program will not use bind variables in the first set, <strong>and</strong> in the<br />

second set it will.<br />

To evaluate these programs <strong>and</strong> their behavior in a multiuser environment, I opted to use<br />

Statspack to gather the metrics, as follows:<br />

1. Execute a Statspack snapshot to gather the current state of the system.<br />

2. Run N copies of the program, having each program INSERT into its own database table<br />

so as to avoid the contention associated with having all programs trying to insert into a<br />

single table.<br />

3. Take another snapshot immediately after the last copy of the program finishes.<br />

Then it is a simple matter of printing out the Statspack report <strong>and</strong> finding out how long<br />

it took N copies of the program to complete, how much CPU was used, what the major wait<br />

events occurred, <strong>and</strong> so on.<br />

These tests were performed on a dual-CPU machine with hyperthreading enabled (making<br />

it appear as if there were four CPUs). Given that there were two physical CPUs, you might<br />

expect very linear scaling here—that is, if one user uses 1 unit of CPU to process her inserts,<br />

then you might expect that two users would require 2 units of CPU. You’ll discover that this<br />

premise, while sounding plausible, may well be inaccurate (just how inaccurate depends on<br />

your programming technique, as you’ll see). It would be correct if the processing we were performing<br />

needed no shared resource, but our process will use a shared resource, namely the<br />

Shared pool. We need to latch the Shared pool to parse SQL statements, <strong>and</strong> we need to latch<br />

the Shared pool because it is a shared data structure, <strong>and</strong> we cannot modify it while others are<br />

reading it <strong>and</strong> we cannot read it while it is being modified.<br />

■Note I’ve performed these tests using Java, PL/SQL, Pro*C, <strong>and</strong> other languages. The end results are very<br />

much the same every time. This demonstration <strong>and</strong> discussion applies to all languages <strong>and</strong> all interfaces to<br />

the database. I chose Java for this example as I find Java <strong>and</strong> Visual Basic applications are most likely to not<br />

use bind variables when working with the <strong>Oracle</strong> database.<br />

Without Bind Variables<br />

In the first instance, our program will not use bind variables, but rather will use string concatenation<br />

to insert data:<br />

import java.sql.*;<br />

public class instest<br />

{<br />

static public void main(String args[]) throws Exception<br />

{<br />

DriverManager.registerDriver(new oracle.jdbc.driver.<strong>Oracle</strong>Driver());<br />

Connection

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

Saved successfully!

Ooh no, something went wrong!