pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
About Connections and PreparedStatements
3+ messages / 3 participants
[nested] [flat]

* About Connections and PreparedStatements
@ 2005-10-12 19:10 Hector Rosas <jeziel.rosas@gmail.com>
  2005-10-12 19:50 ` Re: About Connections and PreparedStatements Oliver Jowett <oliver@opencloud.com>
  2005-10-12 20:01 ` Re: About Connections and PreparedStatements Mark Lewis <mark.lewis@mir3.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Hector Rosas @ 2005-10-12 19:10 UTC (permalink / raw)
  To: pgsql-jdbc

Hello, I have a doubt with connections and PreparedStatemens. If I open a
connection to Postgres and then request a prepared statement, this will be
compiled and reused as long as I don't close the connection right? Or it
doesn't matter if I close the connection, I'm using PooledConnection
provided by my container (Orion), I want to know if there's an advantage to
keep a connections open, in order to reutilize PreparedStatement. Also, I'm
closing PreparedStatement after every query, PreparedStatement need to be
closed after every query right? I mean, is not closing a PreparedStatement a
way to increase performance?

Well, thanks everyone.

Jeziel.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: About Connections and PreparedStatements
  2005-10-12 19:10 About Connections and PreparedStatements Hector Rosas <jeziel.rosas@gmail.com>
@ 2005-10-12 19:50 ` Oliver Jowett <oliver@opencloud.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Oliver Jowett @ 2005-10-12 19:50 UTC (permalink / raw)
  To: Hector Rosas <jeziel.rosas@gmail.com>; +Cc: pgsql-jdbc

Hector Rosas wrote:
> Hello, I have a doubt with connections and PreparedStatemens. If I open 
> a connection to Postgres and then request a prepared statement, this 
> will be compiled and reused as long as I don't close the connection 
> right?

As long as you reuse the same PreparedStatement object, yes.

> Or it doesn't matter if I close the connection

If you close a Connection, all PreparedStatements created by that 
connection are implicitly closed.

>, I'm using 
> PooledConnection provided by my container (Orion),

I can't speak for that PooledConnection implementation.. it may do extra 
statement caching, I don't know.

> I want to know if 
> there's an advantage to keep a connections open, in order to reutilize 
> PreparedStatement. Also, I'm closing PreparedStatement after every 
> query, PreparedStatement need to be closed after every query right?

You do not need to close the PreparedStatement after each query.

> I 
> mean, is not closing a PreparedStatement a way to increase performance?

If you close a PreparedStatement, the postgresql JDBC driver will free 
any corresponding server-side resources such as server-side prepared 
statements.

So if you want to avoid the parse/plan cost when reexecuting the same 
query many times, you should reuse the same PreparedStatement object 
without closing it between executions.

-O



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: About Connections and PreparedStatements
  2005-10-12 19:10 About Connections and PreparedStatements Hector Rosas <jeziel.rosas@gmail.com>
@ 2005-10-12 20:01 ` Mark Lewis <mark.lewis@mir3.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Mark Lewis @ 2005-10-12 20:01 UTC (permalink / raw)
  To: Hector Rosas <jeziel.rosas@gmail.com>; +Cc: pgsql-jdbc

If you need to execute the same query many times, then keep the
preparedStatement open and just keep assigning parameters and
(re)executing it.

To get the best performance out of PostgreSQL, you'll need to be running
a relatively recent PostgreSQL version (7.4 or later, I think) along
with a newer JDBC driver, and you'll want to set the prepareThreshold to
something low, like 1 or 2.  See docs here:
http://jdbc.postgresql.org/documentation/80/server-prepare.html

-- Mark

On Wed, 2005-10-12 at 12:10 -0700, Hector Rosas wrote:
> Hello, I have a doubt with connections and PreparedStatemens. If I
> open a connection to Postgres and then request a prepared statement,
> this will be compiled and reused as long as I don't close the
> connection right? Or it doesn't matter if I close the connection, I'm
> using PooledConnection provided by my container (Orion), I want to
> know if there's an advantage to keep a connections open, in order to
> reutilize PreparedStatement. Also, I'm closing PreparedStatement after
> every query, PreparedStatement need to be closed after every query
> right? I mean, is not closing a PreparedStatement a way to increase
> performance? 
> 
> Well, thanks everyone.
> 
> Jeziel.




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-10-12 20:01 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-10-12 19:10 About Connections and PreparedStatements Hector Rosas <jeziel.rosas@gmail.com>
2005-10-12 19:50 ` Oliver Jowett <oliver@opencloud.com>
2005-10-12 20:01 ` Mark Lewis <mark.lewis@mir3.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox