jdbc best practices - chicago java users group, march 4, 2004

27
March 4, 2004 © 2004, Derek C. Ashmore JDBC Best Practices 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

211 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

JDBC

Bes

t Pr

actic

esD

erek

C. A

shm

ore

Ove

r 6

year

s of

Jav

a-re

late

d ex

perie

nce

Ove

r 10

yea

rs o

f da

taba

se

desi

gn/a

dmin

istr

atio

n ex

perie

nce.

Auth

or o

f Th

e J2

EE™

Arch

itect

’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

Page 2: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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 - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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 - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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.

I se

e co

mm

on c

odin

g ha

bits

tha

t:H

urt

Perf

orm

ance

Mak

e co

de h

ard

to r

ead/

mai

ntai

nM

ake

code

less

por

tabl

e to

oth

er d

atab

ases

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

s

Page 5: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Gen

eral

Bes

t Pr

actic

es

Sum

mar

yCl

ose

JDBC

Obj

ects

in a

“fin

ally

” bl

ock.

Use

Pre

pare

dSta

tem

ents

with

hos

t va

riabl

es in

stea

d of

Sta

tem

ents

.Co

nsol

idat

e SQ

L st

ring

form

atio

n.Li

mit

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

.

Page 6: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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

DB2

w/D

B2 C

lient

St

atem

ent

hand

les

are

cons

umed

Usu

ally

clo

sed

in t

he m

etho

d th

at c

reat

es

them

.As

the

gar

bage

col

lect

or “

final

izes

” th

ese

obje

cts,

you

may

not

see

pro

blem

s ca

used

by

this

unt

il st

ress

tes

ting

or p

rodu

ctio

n.

Page 7: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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 b

lock

A lo

t to

typ

e

Use

gen

eric

clo

se u

tility

tha

t lo

gs S

QLE

xcep

tions

rece

ived

, but

doe

sn’t

thro

w a

n ex

cept

ion

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 8: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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 9: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Use

Pre

pare

dSta

tem

ents

Use

Pre

pare

dSta

tem

ents

with

par

amet

er

mar

kers

inst

ead

of S

tate

men

tsU

se “

sele

ct n

ame

from

Cus

tom

er w

here

id =

?” In

stea

d of

“…

. whe

re id

= ‘H

23’”

Stat

emen

ts m

ean

less

typ

ing

but…

….

Extr

a St

ring

Proc

essi

ng t

o as

sem

ble

the

whe

re

clau

se.

Prev

ents

reu

se o

f th

e qu

ery

acce

ss p

ath

by t

he

data

base

.Th

is m

eans

tha

t st

atem

ents

will

be

Slow

er

Page 10: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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

nec

essa

ry,

but

mos

t ca

n co

nsol

idat

e th

e lo

gic.

Adva

ntag

esEa

sier

to

read

Save

s St

ring

Proc

essi

ngSa

ves

Mem

ory

Page 11: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Cons

olid

ate

SQL

Strin

g Ex

ampl

eEx

ampl

epublic static final String CUST_SQL=

“select name from Cust where id = ?”;

……..

pStmt = conn.prepareStatement(CUST_SQL)

Page 12: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Lim

it us

e of

Pla

tfor

m-s

peci

fic

feat

ures

Crea

tes

a po

rtab

ility

obs

tacl

eYo

ur c

ode

mig

ht li

ve lo

nger

tha

n yo

u th

ink

(Y2K

).O

nly

use

whe

n cl

ear

bene

fit –

not

out

of h

abit

Exam

ples

Stor

ed p

roce

dure

s re

turn

ing

Resu

ltSet

sor

usi

ng

prop

rieta

ry la

ngua

gePr

oprie

tary

Col

umn

Func

tions

Ora

cle’

s D

ecod

ePr

oprie

tary

Ope

rato

rsO

racl

e’s

Min

us a

nd I

nter

sect

Page 13: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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 14: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Gen

eral

Bes

t Pr

actic

es

Sum

mar

y (c

on’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

ns

Util

ize

stat

emen

t ba

tchi

ngU

tiliz

e qu

ery

fetc

h si

zing

Page 15: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Refe

renc

e ja

va.s

qlor

java

x.sq

lcl

asse

s on

lyAv

oid

vend

or-s

peci

fic c

lass

impl

emen

tatio

ns

unle

ss r

equi

red

for

perf

orm

ance

Usu

ally

not

nec

essa

ry n

owW

as n

eces

sary

in e

arly

day

s be

fore

for

mal

sup

port

for

Fetc

h si

zing

/Arr

ay P

roce

ssin

gSt

atem

ent

Batc

hing

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

Prop

rieta

ry o

bjec

ts m

ay n

ot b

e

Page 16: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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.

Page 17: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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

s

Page 18: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

J2EE

-spe

cific

Bes

t Br

actic

es

Util

ize

conn

ectio

n po

olin

g fe

atur

esCl

osin

g co

nnec

tions

are

impe

rativ

eCl

ose(

) re

turn

s co

nnec

tion

to t

he p

ool

Failu

re t

o cl

ose

will

cre

ate

a co

nnec

tion

leak

.

Page 19: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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

200

–100

0 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 20: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

DB2

-Spe

cific

Bes

t Pr

actic

es

Bew

are

that

sel

ects

issu

e sh

ared

lock

s by

def

ault

Bew

are

all t

ypes

of

stra

nded

JD

BC

obje

cts

crea

te o

bjec

t le

aks.

JDBC

vs.

SQ

L/J?

Whe

n to

use

sto

red

proc

edur

es?

Page 21: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Bew

are

of S

hare

d Lo

cks

Bew

are

of s

hare

d lo

ckin

g w

ith S

elec

t st

atem

ents

Com

mon

Myt

h: R

eadi

ng is

har

mle

ssCu

rsor

Sta

bilit

y is

def

ault

==

Sha

red

Lock

sW

hen

only

Rea

ding

: Co

mm

it as

ear

ly a

s po

ssib

le (

or u

se a

utoc

omm

it)

Page 22: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Pena

lty f

or O

bjec

t Le

aks

Appl

ies

if yo

u’re

usi

ng D

B2Cl

ient

(w

hich

m

ost

do)

Each

JD

BC O

bjec

t aq

uire

s a

stat

emen

t ha

ndle

with

in D

B2Cl

ient

.Li

mite

d to

bet

wee

n 60

0 an

d 13

00

(dep

endi

ng o

n ve

rsio

n)

Page 23: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Ora

cle-

Spec

ific

Best

Pra

ctic

es

Arra

y Fe

tchi

ng a

nd U

pdat

e ba

tchi

ng

used

to

be O

racl

e-sp

ecifi

c re

com

men

datio

ns –

not

anym

ore.

Th

ey’re

in t

he J

DBC

spe

c.Tu

rn o

ff a

uto-

com

mit

for

sele

ct a

ctiv

ityU

se o

f Row

Id f

or f

aste

r up

date

s,

inse

rts,

and

del

etes

.

Page 24: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 200

2004

, Der

ek C

. Ash

mor

e

Late

st D

evel

opm

ents

JDBC

3.0

Spe

cific

atio

nRe

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

pro

cedu

re

fans

Stan

dard

izes

Con

nect

ion

Pool

ing

Adds

Pre

pare

dSta

tem

ent

pool

ing

Save

poin

t su

ppor

t

Page 25: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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 26: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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

unl

ess

its

Syba

se.

Page 27: Jdbc Best Practices - Chicago Java Users Group, March 4, 2004

Mar

ch 4

, 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