jdbc best practices - db2/ idug - orlando, may 10, 2004
TRANSCRIPT
JDB
C B
est
Pra
ctic
es f
or D
B2
Pro
gram
mer
s
Der
ek C
. Ash
mor
e,D
elta
Vor
tex
Tech
nolo
gies
Sess
ion
B1
–10
:30a
mM
ay 1
0, 2
004
JDB
C B
est
Pra
ctic
es f
or D
B2
Pro
gram
mer
s
•Pr
esen
tatio
n Su
mm
ary
–JD
BC
Cod
ing
for P
erfo
rman
ce–
JDB
C C
odin
g fo
r Mai
ntai
nabi
lity
–JD
BC
Cod
ing
for P
orta
bilit
y–
Futu
re D
irect
ions
Who
Am
I?
•D
erek
C. A
shm
ore
•A
utho
r of T
he J2
EE™
Arc
hite
ct’s
Han
dboo
k–
Dow
nloa
dabl
e at
:–
http
://w
ww
.dvt
pres
s.com
/java
arch
•O
ver 6
yea
rs o
f Jav
a-re
late
d ex
perie
nce
•O
ver 1
0 ye
ars o
f dat
abas
e de
sign
/adm
inis
tratio
n ex
perie
nce.
•C
an b
e re
ache
d at
das
hmor
e@dv
t.com
Dat
a A
cces
s O
ptio
ns
•JD
BC
(dyn
amic
acc
ess)
•SQ
L/J (
stat
ic a
cces
s)•
J2EE
Ent
ity B
eans
•O
bjec
t-Rel
atio
nal M
appi
ng T
ools
ets
–H
iber
nate
–JD
O–
Man
y ot
hers
…..
Why
foc
us o
n JD
BC
?
•JD
BC
the
mos
t com
mon
ly u
sed.
–N
ot a
tech
nica
l jud
gmen
t –ju
st a
n ob
serv
atio
n.•
Why
is JD
BC
the
mos
t com
mon
acc
ess m
etho
d?–
It w
as th
e fir
st a
cces
s met
hod
avai
labl
e.–
It w
orks
–It
satis
fies d
evel
oper
s nee
ds–
Mos
t DB
MSs
supp
ort i
t.–
JDB
C sk
ills a
re e
asy
to fi
nd in
the
mar
ket
•JD
BC
tuto
rial a
t: ht
tp://
java
.sun.
com
/doc
s/bo
oks/
tuto
rial/j
dbc/
JDB
C C
odin
g fo
r P
erfo
rman
ce
•U
se P
repa
redS
tate
men
tsw
ith h
ost v
aria
ble
mar
kers
inst
ead
of
Stat
emen
ts.
•B
ewar
e th
at se
lect
s iss
ue sh
ared
lock
s by
defa
ult
•C
onsi
der q
uery
fetc
h si
zing
•U
tiliz
e co
nnec
tion
pool
ing
feat
ures
Use
Pre
pare
dSta
tem
ents
Use
Pre
pare
dSta
tem
ents
with
par
amet
er m
arke
rs in
stea
d of
Sta
tem
ents
Use
“se
lect
nam
e fr
om C
usto
mer
whe
re id
= ?
”In
stea
d of
“…
. whe
re id
= ‘H
23’”
Stat
emen
ts a
re le
ss ty
ping
but
……
.Ex
tra S
tring
Pro
cess
ing
to a
ssem
ble
the
whe
re c
laus
e.C
ircum
vent
s Dyn
amic
Cac
hing
Prev
ents
reus
e of
the
quer
y ac
cess
pat
h by
the
data
base
.Th
is m
eans
that
stat
emen
ts w
ill b
e Sl
ower
Dyn
amic
Cac
hing
impr
oves
per
form
ance
of d
ynam
ic S
QL
DB
2/U
DB
(all
envi
ronm
ents
) cac
hes p
lans
ass
ocia
ted
with
dyn
amic
SQ
L st
atem
ents
Like
stat
emen
ts w
ill re
use
that
dyn
amic
ally
stor
ed p
lan
Dyn
amic
SQ
L ge
ts m
any
of th
e pe
rfor
man
ce b
enef
its th
at st
atic
SQ
L ha
s.
Bew
are
of S
hare
d Lo
cks
•B
ewar
e of
shar
ed lo
ckin
g w
ith S
elec
t sta
tem
ents
–C
omm
on M
yth:
Rea
ding
is h
arm
less
–C
urso
r Sta
bilit
y is
def
ault
== S
hare
d Lo
cks
–W
hen
only
Rea
ding
: Com
mit
as e
arly
as p
ossi
ble
(or u
se a
utoc
omm
it)–
Use
“co
mm
it” o
r JD
BC
s aut
ocom
mit
feat
ure
for s
elec
ts, b
ut d
on’t
use
both
.•
Issu
es th
e co
mm
it tw
ice
low
er p
erfo
rman
ce
Con
side
r S
etti
ng t
he q
uery
fet
ch s
ize
•In
stru
ct d
atab
ase
to re
turn
row
s in
batc
hes o
f 10
to 1
00.
•Ex
ampl
e Q
uery
Fet
ch S
izin
g–
stat
emen
t.set
Fetc
hSiz
e(10
)•
Hig
her i
sn’t
alw
ays b
ette
r–
Can
deg
rade
per
form
ance
if u
sed
for s
mal
l Res
ultS
ets.
•H
as F
ewer
net
wor
k ro
und-
trips
–M
ost b
enef
it us
ing
batc
hes o
f 10
to 1
00 –
dim
inis
hing
retu
rns a
fter t
hat.
•La
rger
ben
efit
redu
cing
net
wor
k tri
ps fr
om 1
00,0
00 to
1,0
00 th
anfr
om 1
00,0
00 to
100
.•
The
larg
er th
e ba
tch,
the
mor
e m
emor
y re
quire
d.
•N
eeds
to b
e te
sted
on
a ca
se-b
y-ca
se b
asis
.
Uti
lize
Con
nect
ion
Poo
ling
•C
onne
ctio
n Po
ols e
limin
ate
wai
t tim
e fo
r dat
abas
e co
nnec
tions
by
crea
ting
them
ahe
ad o
f tim
e.–
I’ve
seen
eno
ugh
J2EE
app
s man
agin
g co
nnec
tion
crea
tion
dire
ctly
to
war
rant
this
pra
ctic
e.–
Con
nect
ions
take
30
–50
ms d
epen
ding
on
plat
form
.–
Allo
ws f
or c
apac
ity p
lann
ing
of d
atab
ase
reso
urce
s–
Prov
ides
aut
omat
ic re
cove
ry fr
om d
atab
ase
or n
etw
ork
outa
ges
•Is
suin
g cl
ose(
) on
a po
oled
con
nect
ion
mer
ely
retu
rns i
t to
the
pool
for u
se b
y an
othe
r req
uest
.
JDB
C C
odin
g fo
r M
aint
aina
bilit
y
•C
lose
JDB
C O
bjec
ts in
a “
final
ly”
bloc
k.•
Con
solid
ate
SQL
strin
g fo
rmat
ion.
•A
lway
s spe
cify
col
umn
nam
es in
sele
ct a
nd in
sert
stat
emen
ts.
Clo
se a
ll JD
BC
Obj
ects
•M
ost J
DB
C O
bjec
ts re
quire
a S
tate
men
t Han
dle
from
DB
2 C
lient
.–
Incl
udes
Pre
pare
dSta
tem
ent,
Stat
emen
t, an
d R
esul
tSet
–Th
is is
a fi
nite
reso
urce
(e.g
. eith
er 6
00 o
r 130
0 de
pend
ing
upon
ver
sion
).•
Clo
se a
ll JD
BC
Obj
ects
in a
fina
lly b
lock
–St
rand
ed JD
BC
con
sum
e sc
arce
db
reso
urce
s•
Cau
se e
rror
s dow
n th
e lin
e•
DB
2/U
DB
(all
envi
ronm
ents
) w/D
B2
Clie
nt
Stat
emen
t han
dles
are
con
sum
ed
•C
lose
JDB
C o
bjec
ts in
the
met
hod
that
cre
ates
them
.–
Easi
er to
impl
emen
t thi
s hab
it.–
Easi
er to
vis
ually
iden
tify
obje
cts n
ot b
eing
clo
sed.
•A
s the
gar
bage
col
lect
or “
clos
es”
thes
e ob
ject
s, bu
t no
guar
ante
e of
bei
ng
unde
r the
reso
urce
lim
it.
–Y
ou m
ay n
ot se
e pr
oble
ms u
ntil
stre
ss te
stin
g or
pro
duct
ion.
Clo
sure
Iss
ues
•C
losi
ng JD
BC
Obj
ects
is in
conv
enie
nt–
Clo
se()
thro
ws a
SQ
LExc
eptio
n–
Lead
s to
nest
ed tr
y/ca
tch
logi
c in
the
final
ly b
lock
–A
lot t
o ty
pe•
Util
ity S
uppo
rt ca
n m
ake
this
eas
ier
–U
se g
ener
ic c
lose
util
ity th
at lo
gs S
QLE
xcep
tions
rece
ived
, but
do
esn’
t thr
ow a
n ex
cept
ion
–G
ets t
he “
clos
e” d
own
to o
ne li
ne.
–C
emen
tJ–
http
://so
urce
forg
e.ne
t/pro
ject
s/ce
men
tj•
org.
cem
entj.
util.
Dat
abas
eUtil
ity
Pen
alty
for
Obj
ect
Leak
s
•A
pplie
s if y
ou’r
e us
ing
DB
2/C
lient
(whi
ch m
ost d
o)•
Each
JDB
C O
bjec
t acq
uire
s a st
atem
ent h
andl
e w
ithin
D
B2/
Clie
nt.
•Li
mite
d to
bet
wee
n 60
0 an
d 13
00 (d
epen
ding
on
vers
ion
of
DB
2/C
lient
)
Clo
sure
Iss
ues
(con
’t)
•Fi
ndin
g St
rand
ed JD
BC
Obj
ects
Pro
blem
atic
–Th
is is
esp
ecia
lly d
iffic
ult i
f you
nee
d to
iden
tify
leak
s in
anap
plic
atio
n yo
u di
dn’t
writ
e.–
Use
P6S
py w
ith a
n ex
tens
ion
libra
ry–
P6Sp
y is
a JD
BC
Pro
filer
that
logs
SQ
L st
atem
ents
, and
thei
r exe
cutio
n tim
e.–
I’ve
ext
ende
d P6
Spy
so th
at it
will
iden
tify
all s
trand
ed o
bjec
ts a
nd li
st S
QL
stat
emen
ts a
ssoc
iate
d w
ith th
em.
–P6
Spy
avai
labl
e at
http
://w
ww
.p6s
py.c
om/
–P6
Spy
Exte
nsio
n at
“R
esou
rces
” lin
k fr
om w
ww
.dvt
pres
s.com
/java
arch
Con
solid
ate
SQ
L S
trin
g fo
rmat
ion
Som
e de
velo
pers
dyn
amic
ally
bui
ld th
e SQ
L st
ring
with
scat
tere
d co
ncat
enat
ion
logi
cSt
ring
sqlS
tmt=
“se
lect
col
1, c
ol2
from
tab1
”;<<
< m
ore
appl
icat
ion
code
>>>
sqlS
tmt=
sqlS
tmt +
“ w
here
col
2 >
200”
;<<
< m
ore
appl
icat
ion
code
>>>
sqlS
tmt=
sqlS
tmt+
“ a
nd c
ol3
< 5”
;W
ith a
smal
l num
ber o
f app
s, th
is is
nec
essa
ry, b
ut m
ost c
an c
onso
lidat
e th
e lo
gic.
Dis
adva
ntag
esH
arde
r to
use
dyna
mic
cac
hing
Har
der t
o re
adM
ore
Strin
g Pr
oces
sing
Mor
e M
emor
y A
lloca
tion
Con
solid
ate
SQ
L S
trin
g E
xam
ple
Usi
ng “s
tatic
” var
iabl
es fo
r SQ
L te
xtR
educ
es s
tring
pro
cess
ing
and
mem
ory
allo
catio
n as
hap
pens
w
hen
the
clas
s is
firs
t ref
eren
ced.
Con
solid
ates
SQ
L te
xt s
o th
at it
’s e
asie
r to
read
.Ex
ampl
epublic static final String CUST_SQL=
“select name from Cust where id = ?”;
……
pStmt = conn.prepareStatement(CUST_SQL)
Spe
cify
Col
umn
Nam
es
•A
lway
s spe
cify
col
umn
nam
es in
sele
ct a
nd in
sert
stat
emen
ts.
–C
ode
won
’t br
eak
if D
BA
cha
nges
col
umn
orde
r–
Cle
arer
for m
aint
enan
ce p
urpo
ses
•Im
agin
e a
sele
ct o
r ins
ert s
tate
men
t inv
olvi
ng 2
0-30
col
umns
–H
ard
to te
ll w
hich
val
ue p
erta
ins t
o w
hich
col
umn
•Sp
ecify
col
umn
nam
e in
stea
d of
off
set w
hen
usin
g R
esul
tSet
s–
Use
resu
ltSet
.get
Strin
g(“c
ol1”
);–
Inst
ead
of re
sultS
et.g
etSt
ring(
3);
JDB
C C
odin
g fo
r P
orta
bilit
y
•Li
mit
use
of p
latfo
rm-s
peci
fic fe
atur
es.
•R
efer
ence
java
.sqlo
rjav
ax.sq
lcla
sses
onl
y–
Avo
id D
B2-
spec
ific
clas
ses
Lim
it u
se o
f P
latf
orm
-spe
cifi
c fe
atur
es
•Po
rtabi
lity
== T
he a
bilit
y to
switc
h D
BM
Ss.
•U
se o
f pla
tform
-spe
cific
feat
ures
cre
ate
porta
bilit
y ob
stac
les
–Y
our c
ode
mig
ht li
ve lo
nger
than
you
thin
k (Y
2K).
•O
nly
use
whe
n cl
ear b
enef
it –
not o
ut o
f hab
it•
Exam
ples
–St
ored
pro
cedu
res u
sing
pro
prie
tary
lang
uage
–Pr
oprie
tary
Col
umn
Func
tions
•EN
CR
YPT
•N
ULL
IF
–Pr
oprie
tary
Ope
rato
rs•
CA
SE•
OLA
P (e
.g. R
AN
K)
Ref
eren
ce ja
va.s
qlor
java
x.sq
lcla
sses
on
ly
•A
void
ven
dor-
spec
ific
clas
s im
plem
enta
tions
unl
ess r
equi
red
for p
erfo
rman
ce–
Usu
ally
not
nec
essa
ry n
ow•
Was
nec
essa
ry in
ear
ly d
ays b
efor
e fo
rmal
supp
ort f
or–
Fetc
h si
zing
/Arr
ay P
roce
ssin
g–
Stat
emen
t Bat
chin
g
–C
reat
es a
por
tabi
lity
issu
e•
Har
der t
o sw
itch
DB
MSs
–C
reat
es a
mai
nten
ance
issu
e•
The
JDB
C in
terf
aces
are
fam
iliar
•Pr
oprie
tary
obj
ects
may
not
be
Late
st D
evel
opm
ents
•JD
BC
3.0
Spe
cific
atio
n–
Ret
urn
gene
rate
d PK
val
ue o
n in
sert.
–R
esul
tSet
Hol
dabi
lity
–ex
ist t
hrou
gh c
omm
its–
Supp
ort m
ultip
le R
esul
tSet
sfor
stor
ed p
roce
dure
fans
–St
anda
rdiz
es C
onne
ctio
n Po
olin
g–
Add
s Pre
pare
dSta
tem
entp
oolin
g–
Save
poin
t sup
port
Futu
re D
irec
tion
s
•JD
BC
is a
mat
urin
g sp
ec–
Expe
ct fr
eque
ncy
of c
hang
e to
slow
con
side
rabl
y
•U
se o
f Obj
ect-R
elat
iona
l map
ping
tool
sets
is in
crea
sing
–H
iber
nate
(ww
w.h
iber
nate
.org
)–
JDO
(ww
w.jd
ocen
tral.c
om)
•D
espi
te te
chni
cal a
dvan
ces,
entit
y be
ans a
re c
lose
to
beco
min
g a
part
of h
isto
ry.
Sto
red
Pro
cedu
re U
se
•A
ren’
t Sto
red
Proc
edur
es b
ette
r per
form
ing?
–D
epen
ds o
n pl
atfo
rm•
Syba
se –
yes,
Ora
cle/
DB
2 –
not a
lway
s
–A
s a g
ener
al ru
le, C
PU in
tens
ive
actio
ns a
re b
ad a
s sto
red
proc
edur
es–
SQL
are
stat
ical
ly b
ound
•U
sed
to b
e m
ore
sign
ifica
nt b
efor
e dy
nam
ic c
achi
ng
–A
s a ru
le, s
tore
d pr
oced
ures
hel
p pe
rfor
man
ce b
y re
duci
ng th
e nu
mbe
r of
netw
ork
trans
mis
sion
s.•
Con
ditio
nal s
elec
ts o
r upd
ates
•A
s a b
atch
upd
ate
surr
ogat
e (c
ombi
ning
larg
er n
umbe
rs o
f SQ
L st
atem
ents
)
•A
sk: H
ow m
any
netw
ork
trans
mis
sion
s will
be
save
d by
mak
ing
this
a st
ored
pro
cedu
re?
If th
e an
swer
is “
0”, p
erfo
rman
ce is
not
like
ly to
be
impr
oved
.
Hea
der
Tex
t
Que
stio
ns
•JD
BC
Bes
t Pra
ctic
es fo
r DB
2 Pr
ogra
mm
ers
•Se
ssio
n B
1•
Der
ek C
. Ash
mor
e•
Emai
l: d
ashm
ore@
dvt.c
om