jdbc best practices - oracle/ioug, toronto, april 21, 2004

30
April 21, 2004 © 2004, Derek C. Ashmore JDBC Best Practices for Oracle Programmers Derek C. Ashmore Over 6 years of Java-related experience Over 10 years of database design/administration experience. Author of The J2EE™ Architect’s Handbook Downloadable at: http://www.dvtpress.com/javaarch Can be reached at [email protected]

Upload: derekclarkashmore

Post on 14-Jul-2015

203 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

JDBC

Bes

t Pr

actic

es f

or O

racl

e Pr

ogra

mm

ers

Der

ek C

. Ash

mor

eO

ver

6 ye

ars

of J

ava-

rela

ted

expe

rienc

eO

ver

10 y

ears

of

data

base

de

sign

/adm

inis

trat

ion

expe

rienc

e.Au

thor

of

The

J2EE

™Ar

chite

ct’s

Han

dboo

kD

ownl

oada

ble

at:

http

://w

ww

.dvt

pres

s.co

m/j

avaa

rch

Can

be r

each

ed a

t da

shm

ore@

dvt.

com

Page 2: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Dat

a Ac

cess

Opt

ions

JDBC

SQL/

JJ2

EE E

ntity

Bea

nsO

bjec

t-Rel

atio

nal M

appi

ng T

ools

ets

Hib

erna

teJD

OM

any

othe

rs…

..

Page 3: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Why

foc

us o

n JD

BC?

JDBC

the

mos

t co

mm

only

use

d.N

ot a

tec

hnic

al ju

dgm

ent

–ju

st a

n ob

serv

atio

n.W

hy is

JD

BC t

he m

ost

com

mon

acc

ess

met

hod?

It w

as t

he f

irst

acce

ss m

etho

d av

aila

ble.

It w

orks

It s

atis

fies

deve

lope

rs n

eeds

Mos

t da

taba

ses

supp

ort

it.M

any

Dev

elop

ers

alre

ady

know

it

Page 4: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Why

“JD

BC B

est

Prac

tices

”?If

we’

re g

oing

to

use

JDBC

, we

shou

ld u

se it

w

ell.

It’s

a c

ompl

ex t

ool w

ith lo

ts o

f di

ffer

ent

way

s to

do

the

sam

e th

ing

Wha

t’s b

est

isn’

t al

way

s ob

viou

sTh

e go

als

for

“Bes

t Pr

actic

es”:

Perf

orm

ance

Mai

ntai

nabi

lity

Port

abili

ty

Page 5: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Agen

daBe

st P

ract

ices

Prac

tices

app

licab

le t

o al

l typ

es o

f Ja

va/J

DBC

cod

e us

ed w

ith O

racl

e da

taba

ses

Prac

tices

tar

gete

d at

Ser

vlet

s, E

nter

pris

e Be

ans,

W

eb S

ervi

ces

Com

mon

Que

stio

nsLa

test

Dev

elop

men

ts w

ith J

DBC

3.0

Futu

re D

irect

ions

Page 6: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Best

Pra

ctic

es S

umm

ary

Clos

e JD

BC O

bjec

ts in

a “

final

ly”

bloc

k.Tu

rn o

ff a

uto-

com

mit

for

sele

ct a

ctiv

ityAu

dit

use

of p

latf

orm

-spe

cific

fea

ture

s.Al

way

s sp

ecify

col

umn

lists

in s

elec

t an

d in

sert

sta

tem

ents

.Co

nsid

er s

tate

men

t ba

tchi

ngCo

nsid

er q

uery

fet

ch s

izin

g

Page 7: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Best

Pra

ctic

es S

umm

ary

(con

’t)Ref

eren

ce ja

va.s

qlor

java

x.sq

lcla

sses

on

ly Avoi

d ve

ndor

-spe

cific

cla

ss

impl

emen

tatio

nsU

tiliz

e co

nnec

tion

pool

ing

feat

ures

Clos

ing

conn

ectio

ns a

re im

pera

tive

ns c

onne

ctio

n to

the

poo

lw

ill c

reat

e a

conn

ectio

n le

ak.

Page 8: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Best

Pra

ctic

es S

umm

ary

(con

’t)U

se T

imes

tam

p ob

ject

s as

hos

t va

riabl

es in

stea

d of

Str

ings

for

DAT

E co

lum

nsCo

nsol

idat

e SQ

L st

ring

form

atio

n.Se

para

te J

DBC

cod

e fr

om b

usin

ess

logi

c

Page 9: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Clos

e al

l JD

BC O

bjec

tsCl

ose

all J

DBC

Obj

ects

in a

fin

ally

blo

ckSt

rand

ed J

DBC

con

sum

e sc

arce

db

reso

urce

sCa

use

erro

rs d

own

the

line

Ora

cle

Curs

ors

are

cons

umed

Usu

ally

clo

sed

in t

he m

etho

d th

at c

reat

es

them

.As

the

rat

e st

rand

ed o

bjec

ts a

ccum

ulat

e is

le

ss in

dev

elop

men

t, y

ou m

ay n

ot s

ee

prob

lem

s ca

used

by

this

unt

il st

ress

tes

ting

or

prod

uctio

n.

Page 10: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Clos

ure

Issu

esCl

ose(

) th

row

s a

SQLE

xcep

tion

Lead

s to

nes

ted

try/

catc

h lo

gic

in t

he f

inal

ly

bloc

kA

lot

to t

ype

Use

gen

eric

clo

se u

tility

tha

t lo

gs

SQLE

xcep

tions

rece

ived

, but

doe

sn’t

thro

w

an e

xcep

tion

Get

s th

e cl

ose

dow

n to

one

line

.Ce

men

tJ –

http

://s

ourc

efor

ge.n

et/p

roje

cts/

cem

entj

org.

cem

entj.

util.

Dat

abas

eUtil

ity

Page 11: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Clos

ure

Issu

es (

con’

t)

Find

ing

Stra

nded

JD

BC O

bjec

ts

Prob

lem

atic

Use

P6S

py w

ith a

n ex

tens

ion

libra

ryW

ill id

entif

y al

l str

ande

d ob

ject

s an

d lis

t SQ

L st

atem

ents

ass

ocia

ted

with

the

m.

P6Sp

y av

aila

ble

at h

ttp:

//w

ww

.p6s

py.c

om/

Exte

nsio

ns a

t “R

esou

rces

” lin

k fr

om

ww

w.d

vtpr

ess.

com

/jav

aarc

h

Page 12: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Turn

off

aut

o-co

mm

it fo

r se

lect

act

ivity

Ora

cle

does

not

issu

e lo

cks

on r

eads

un

less

you

spe

cify

the

“fo

r up

date

” cl

ause

Com

mits

cau

se a

n ex

tra

netw

ork

roun

d-tr

ipI

turn

off

aut

o-co

mm

it fo

r m

ost

appl

icat

ions

.To

o ha

rd t

o m

anag

e w

hen

it’s

on v

s. o

ff

Page 13: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Audi

t us

e of

Pla

tfor

m-s

peci

fic

feat

ures

Onl

y us

e w

hen

clea

r be

nefit

–no

t ou

t of

hab

itCr

eate

s a

port

abili

ty o

bsta

cle

Your

cod

e m

ight

live

long

er t

han

you

thin

k (Y

2K).

Exam

ples

Stor

ed p

roce

dure

s w

ritte

n in

PL/

SQL

Prop

rieta

ry C

olum

n Fu

nctio

nsO

racl

e’s

Dec

ode

Prop

rieta

ry O

pera

tors

Ora

cle’

s M

inus

and

Int

erse

ct

Page 14: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Spec

ify C

olum

n Li

sts

Alw

ays

spec

ify c

olum

n lis

ts in

sel

ect

and

inse

rt s

tate

men

ts.

Code

won

’t br

eak

if D

BA c

hang

es c

olum

n or

der

Clea

rer

for

mai

nten

ance

pur

pose

sIm

agin

e a

sele

ct o

r in

sert

sta

tem

ent

invo

lvin

g 20

-30

colu

mns

Har

d to

tel

l whi

ch v

alue

per

tain

s to

whi

ch c

olum

n

Page 15: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Use

Sta

tem

ent

Batc

hing

Gro

ups

upda

tes,

inse

rts,

and

del

etes

tog

ethe

r in

gro

ups

Has

Few

er n

etw

ork

roun

d-tr

ips

like

Stor

ed

Proc

edur

e us

e do

es.

Mos

t be

nefit

usi

ng b

atch

es o

f 10

to

100

–di

min

ishi

ng r

etur

ns a

fter

tha

t.La

rger

ben

efit

redu

cing

net

wor

k tr

ips

from

100

,000

to

1,00

0 th

an f

rom

100

,000

to

100.

The

larg

er t

he b

atch

, the

mor

e m

emor

y re

quire

d on

the

cl

ient

.

I’ve

seen

Ins

ert

of 1

000

row

s im

prov

e fr

om

780

ms

to 5

0 m

s!

Page 16: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Set

the

quer

y fe

tch

size

Inst

ruct

dat

abas

e to

ret

urn

row

s in

bat

ches

of

10 t

o 10

0.H

as F

ewer

net

wor

k ro

und-

trip

sM

ost

bene

fit u

sing

bat

ches

of

10 t

o 10

0 –

dim

inis

hing

ret

urns

aft

er t

hat.

Larg

er b

enef

it re

duci

ng n

etw

ork

trip

s fr

om 1

00,0

00 t

o 1,

000

than

fro

m 1

00,0

00 t

o 10

0.Th

e la

rger

the

bat

ch, t

he m

ore

mem

ory

requ

ired.

Mor

e be

nefit

with

larg

er R

esul

tSet

sI’v

e se

en 5

0% p

erfo

rman

ce im

prov

emen

ts

Page 17: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Refe

renc

e ja

va.s

qlor

java

x.sq

lcl

asse

s on

lyAv

oid

dire

ct u

se o

f O

racl

e-sp

ecifi

c cl

ass

impl

emen

tatio

nsU

sual

ly n

ot n

eces

sary

now

Was

nec

essa

ry in

ear

ly d

ays

befo

re f

orm

al s

uppo

rt f

orFe

tch

sizi

ng/A

rray

Pro

cess

ing

Stat

emen

t Ba

tchi

ng

Crea

tes

a po

rtab

ility

issu

eH

arde

r to

sw

itch

data

base

sCr

eate

s a

mai

nten

ance

issu

eTh

e JD

BC in

terf

aces

are

fam

iliar

Ora

cle-

spec

ific

obje

cts

may

not

be

Page 18: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Util

ize

Conn

ectio

n Po

olin

gCo

nnec

tion

Pool

s el

imin

ate

wai

t tim

e fo

r da

taba

se c

onne

ctio

ns b

y cr

eatin

g th

em a

head

of

tim

e. I’ve

seen

eno

ugh

J2EE

app

s m

anag

ing

conn

ectio

n cr

eatio

n di

rect

ly t

o w

arra

nt t

his

prac

tice.

Conn

ectio

ns t

ake

30 -

50 m

s de

pend

ing

on p

latf

orm

.Al

low

s fo

r ca

paci

ty p

lann

ing

of d

atab

ase

reso

urce

sPr

ovid

es a

utom

atic

rec

over

y fr

om d

atab

ase

or n

etw

ork

outa

ges

Issu

ing

clos

e()

on a

poo

led

conn

ectio

n m

erel

y re

turn

s it

to t

he p

ool f

or u

se b

y an

othe

r re

ques

t.

Page 19: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Use

Row

Id f

or f

aste

r up

date

s an

d de

lete

sRow

Id c

onta

ins

info

rmat

ion

abou

t w

here

a

row

phy

sica

lly is

Use

d by

inde

x en

trie

sU

se t

he r

owId

in t

he w

here

cla

use

for

upda

tes

and

dele

tes

Fast

er t

han

inde

xed

acce

ssEa

sy t

o se

lect

the

row

Id

Goo

d fo

r m

aint

enan

ce s

cree

ns w

here

sel

ect

is

usua

lly p

erfo

rmed

bef

ore

upda

te o

r de

lete

.

Page 20: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Use

Tim

esta

mp

obje

cts

for

DAT

E co

lum

nsO

racl

e D

ATE

colu

mns

con

tain

“tim

e”U

se T

imes

tam

p fo

r se

lect

s an

d as

hos

t va

riabl

es f

or u

pdat

es, i

nser

ts, a

nd

dele

tes

Min

imiz

e co

nver

ting

betw

een

date

s an

d st

rings

It

’s e

xpen

sive

in J

ava

and

Ora

cle

Page 21: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Cons

olid

ate

SQL

Strin

g fo

rmat

ion

Som

e de

velo

pers

dyn

amic

ally

bui

ld t

he S

QL

strin

g w

ith s

catt

ered

con

cate

natio

n lo

gic

Strin

gsq

lStm

t=

“se

lect

col

1, c

ol2

from

tab

1”;

<<

< m

ore

appl

icat

ion

code

>>

>sq

lStm

t=

sqlS

tmt

+ “

whe

re c

ol2

> 2

00”;

<<

< m

ore

appl

icat

ion

code

>>

>sq

lStm

t=

sqlS

tmt

+ “

and

col

3 <

5”;

With

a s

mal

l num

ber

of a

pps,

thi

s is

ne

cess

ary,

but

mos

t ca

n co

nsol

idat

e th

e lo

gic.

Page 22: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Cons

olid

ate

SQL

Strin

g (c

on’t)

Adva

ntag

esEa

sier

to

read

Save

s St

ring

Proc

essi

ngSa

ves

Mem

ory

Exam

ple

public static final String CUST_SQL=

“select name from Cust where id = ?”;

……..

pStmt = conn.prepareStatement(CUST_SQL)

Page 23: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Sepa

rate

JD

BC c

ode

from

bu

sine

ss lo

gic

Mak

e it

a se

para

te p

acka

geco

m.jm

u.m

yapp

.dat

a

Easi

er t

o sh

are

data

base

acc

ess

code

be

twee

n bu

sine

ss f

unct

ions

or

mul

tiple

ap

plic

atio

nsEa

sier

to

port

Easi

er t

o lo

cate

and

cha

nge

Page 24: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Com

mon

Que

stio

ns

Whe

n sh

ould

I b

e us

ing

Stor

ed

Proc

edur

es?

Whe

n sh

ould

Jav

a be

insi

de t

he d

atab

ase

inst

ead

of o

utsi

de?

Shou

ld I

be

usin

g Pr

epar

edSt

atem

ent

inst

ead

of S

tate

men

t?

Page 25: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Stor

ed P

roce

dure

Use

Aren

’t St

ored

Pro

cedu

res

bett

er p

erfo

rmin

g?D

epen

ds o

n pl

atfo

rmSy

base

–ye

s, O

racl

e/D

B2 –

not

alw

ays

As a

gen

eral

rul

e, C

PU in

tens

ive

actio

ns a

re b

ad

as s

tore

d pr

oced

ures

As a

rul

e, s

tore

d pr

oced

ures

hel

p pe

rfor

man

ce b

y re

duci

ng t

he n

umbe

r of

net

wor

k tr

ansm

issi

ons.

Cond

ition

al s

elec

ts o

r up

date

sAs

a b

atch

upd

ate

surr

ogat

e (c

ombi

ning

larg

er n

umbe

rs

of S

QL

stat

emen

ts)

Ask:

How

man

y ne

twor

k tr

ansm

issi

ons

will

be

save

d by

m

akin

g th

is a

sto

red

proc

edur

e?

If t

he a

nsw

er is

“0”

, pe

rfor

man

ce is

not

like

ly t

o be

impr

oved

.

Page 26: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Prep

ared

Stat

emen

t U

sePr

epar

edSt

atem

ents

are

reco

mm

ende

d fo

r m

ost

case

sSt

atem

ents

can

be

fast

er f

rom

the

clie

nt

pers

pect

ive

For

SQL

not

need

ed d

ata

type

for

mat

ting

for

the

whe

re c

laus

e (e

.g. D

ATE

colu

mns

)Fo

r SQ

L w

ith f

ew r

epet

ition

sEl

imin

ates

sha

red

pool

opt

imiz

atio

nsW

ill d

ecre

ase

data

base

eff

icie

ncy

for

the

entir

e in

stan

ce

Page 27: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Prep

ared

Stat

emen

t Ca

chin

gO

racl

e ha

s th

e ab

ility

to

cach

e Pr

epar

edSt

atem

ents

on

the

clie

nt s

ide

This

is a

n O

racl

e-sp

ecifi

c fe

atur

e th

at

will

cre

ate

a po

rtab

ility

issu

eTo

use

issu

e tw

o st

atem

ents

((OracleConnection)con).setStatementCacheSize(10);

((OracleConnection)con).setExplicitCachingEnabled(true);

Abou

t 33

% im

prov

emen

t ov

er 1

00

stat

emen

t te

st

Page 28: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Late

st D

evel

opm

ents

JDBC

3.0

Spe

cific

atio

nSt

anda

rdiz

es C

onne

ctio

n Po

olin

gAd

ds P

repa

redS

tate

men

t po

olin

gSa

vepo

int

supp

ort

Not

yet

sup

port

ed b

y O

racl

eRe

turn

gen

erat

ed P

K va

lue

on in

sert

.Re

sultS

et H

olda

bilit

y –

exis

t th

roug

h co

mm

itsSu

ppor

t m

ultip

le R

esul

tSet

s fo

r st

ored

pr

oced

ure

fans

Page 29: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Futu

re D

irect

ions

JDBC

is a

mat

urin

g sp

ecEx

pect

fre

quen

cy o

f ch

ange

to

slow

con

side

rabl

y

Use

of

Obj

ect-

Rela

tiona

l map

ping

too

lset

s is

in

crea

sing

Hib

erna

te (

ww

w.h

iber

nate

.org

)JD

O (

ww

w.jd

ocen

tral

.com

)

Des

pite

tec

hnic

al a

dvan

ces,

ent

ity b

eans

are

cl

ose

to b

ecom

ing

a pa

rt o

f hi

stor

y.

Page 30: Jdbc Best Practices - Oracle/IOUG, Toronto, April 21, 2004

April

21,

200

2004

, Der

ek C

. Ash

mor

e

Que

stio

ns

Der

ek C

. Ash

mor

eAu

thor

of

The

J2EE

™Ar

chite

ct’s

H

andb

ook

Dow

nloa

dabl

e at

:ht

tp:/

/ww

w.d

vtpr

ess.

com

/jav

aarc

h

Can

be r

each

ed a

t da

shm

ore@

dvt.

com