pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
When to close a Statement
4+ messages / 3 participants
[nested] [flat]

* When to close a Statement
@ 2006-02-15 13:03  Carlos Correia <carlos@m16e.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Carlos Correia @ 2006-02-15 13:03 UTC (permalink / raw)
  To: pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

After having had some problems with JDBC statements, as I mentioned in a
previous post, due to different behaviours between versions 7.x and 8.x
in the way Postgres deals with statements, I would like to know what is
the proper way of dealing with this.

I realized that the need to close the statement is implementaion
dependent, and now I'm a bit confused as I can't find anything in
Postgres docs about this issue... so the questions are:

- - must I close the statement after *every* executeXXX method call?

- - should I also close the connection as well, or may I leave it open
during the application execution?

Thanks,

Carlos
- --
MEMÓRIA PERSISTENTE, Lda.
Tel.: 219 291 591 - GSM:  967 511 762
e-mail: geral@m16e.com - URL: http://www.m16e.com
AIM: m16e - ICQ: 257488263 - Jabber: m16e@amessage.de
Skype.com username (VoIP): m16e.com
GnuPG: wwwkeys.eu.pgp.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFD8yaU90uzwjA1SJURAsyhAJ9twvK/oeZXrq/SEyoR9TaGr3ZNdQCgwaBC
H3eHaRZdHmZ3FuIhs/xgDIs=
=UI89
-----END PGP SIGNATURE-----



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

* Re: When to close a Statement
@ 2006-02-15 13:23  Markus Schaber <schabi@logix-tt.com>
  parent: Carlos Correia <carlos@m16e.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Markus Schaber @ 2006-02-15 13:23 UTC (permalink / raw)
  To: Carlos Correia <carlos@m16e.com>; +Cc: pgsql-jdbc

Hi, Carlos,

Carlos Correia wrote:

> After having had some problems with JDBC statements, as I mentioned in a
> previous post, due to different behaviours between versions 7.x and 8.x
> in the way Postgres deals with statements, I would like to know what is
> the proper way of dealing with this.
> 
> I realized that the need to close the statement is implementaion
> dependent, and now I'm a bit confused as I can't find anything in
> Postgres docs about this issue... so the questions are:
> 
> - must I close the statement after *every* executeXXX method call?

No, only close it if you don't need it any more.

But reusing a statement closes the ResultSet from the last query used
with this statement. The same is for begin/abort/commit.

> - should I also close the connection as well, or may I leave it open
> during the application execution?

Keep it open as long as possible (or use a connection pool), because
reopening a connection is slow.

HTH,
Markus
-- 
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org



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

* Re: When to close a Statement
@ 2006-02-15 13:41  Carlos Correia <carlos@m16e.com>
  parent: Achilleus Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Carlos Correia @ 2006-02-15 13:41 UTC (permalink / raw)
  To: pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Achilleus Mantzios escreveu:
| O Carlos Correia Ýãñáøå óôéò Feb 15, 2006 :
|
|
| Hi,
|
| After having had some problems with JDBC statements, as I mentioned in a
| previous post, due to different behaviours between versions 7.x and 8.x
| in the way Postgres deals with statements, I would like to know what is
| the proper way of dealing with this.
|
| I realized that the need to close the statement is implementaion
| dependent, and now I'm a bit confused as I can't find anything in
| Postgres docs about this issue... so the questions are:
|
| - must I close the statement after *every* executeXXX method call?
|
| - should I also close the connection as well, or may I leave it open
| during the application execution?
|
|
|> Close a statement when you dont need it any more.
|> e.g.
|> PreparedStatement neinsst=con.prepareStatement("insert into
|> sparecasesdets(scid,no,descr,qty_obd,qty_req,drgno,partno)
|> VALUES(?,?,?,?,?,?,?)");
|
|> java.util.Enumeration en = request.getParameterNames();
|
|> for (;en.hasMoreElements();) {
|
|>     String str_no=(String) en.nextElement();
|>     boolean isdascd = str_no.startsWith("no_dascdid");
|>     .....
|>     neinsst.setInt(1,Integer.parseInt(scid));
|>     .....
|>     int rc = neinsst.executeUpdate();
|>     ....
|> }
|> neinsst.close();
|

I haven't fully test it with PreparedStatement yet, but with normal
statements -- the ones you get after calling connection's
createStatement() -- after several executeXXX() calls I get an
SQLException saying "the connection has been closed" (this problem only
occurs with PostgreSQL v8.x in Windows -- I haven't been able to test in
Linux yet -- with v. 7.x I haven't notice such problems).

Thanks,

Carlos
- --
MEM?RIA PERSISTENTE, Lda.
Tel.: 219 291 591 - GSM:  967 511 762
e-mail: geral@m16e.com - URL: http://www.m16e.com
AIM: m16e - ICQ: 257488263 - Jabber: m16e@amessage.de
Skype.com username (VoIP): m16e.com
GnuPG: wwwkeys.eu.pgp.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFD8y9490uzwjA1SJURAvixAKCuXSPDP3FPdxV9med+wGsh70QrBQCgr9ax
qDq8B8xu5D43aqFNaY1EA44=
=cqL5
-----END PGP SIGNATURE-----



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

* Re: When to close a Statement
@ 2006-02-15 14:21  Achilleus Mantzios <achill@matrix.gatewaynet.com>
  parent: Carlos Correia <carlos@m16e.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Achilleus Mantzios @ 2006-02-15 14:21 UTC (permalink / raw)
  To: Carlos Correia <carlos@m16e.com>; +Cc: pgsql-jdbc

O Carlos Correia Ýãñáøå óôéò Feb 15, 2006 :

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi,
> 
> After having had some problems with JDBC statements, as I mentioned in a
> previous post, due to different behaviours between versions 7.x and 8.x
> in the way Postgres deals with statements, I would like to know what is
> the proper way of dealing with this.
> 
> I realized that the need to close the statement is implementaion
> dependent, and now I'm a bit confused as I can't find anything in
> Postgres docs about this issue... so the questions are:
> 
> - - must I close the statement after *every* executeXXX method call?
> 
> - - should I also close the connection as well, or may I leave it open
> during the application execution?

Close a statement when you dont need it any more.
e.g.
PreparedStatement neinsst=con.prepareStatement("insert into 
sparecasesdets(scid,no,descr,qty_obd,qty_req,drgno,partno) 
VALUES(?,?,?,?,?,?,?)");

java.util.Enumeration en = request.getParameterNames();

for (;en.hasMoreElements();) {
    
    String str_no=(String) en.nextElement();
    boolean isdascd = str_no.startsWith("no_dascdid");
    .....
    neinsst.setInt(1,Integer.parseInt(scid));
    .....
    int rc = neinsst.executeUpdate();
    ....
}
neinsst.close();



> 
> Thanks,
> 
> Carlos
> - --
> MEMÓRIA PERSISTENTE, Lda.
> Tel.: 219 291 591 - GSM:  967 511 762
> e-mail: geral@m16e.com - URL: http://www.m16e.com
> AIM: m16e - ICQ: 257488263 - Jabber: m16e@amessage.de
> Skype.com username (VoIP): m16e.com
> GnuPG: wwwkeys.eu.pgp.net
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (GNU/Linux)
> Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
> 
> iD8DBQFD8yaU90uzwjA1SJURAsyhAJ9twvK/oeZXrq/SEyoR9TaGr3ZNdQCgwaBC
> H3eHaRZdHmZ3FuIhs/xgDIs=
> =UI89
> -----END PGP SIGNATURE-----
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faq
> 

-- 
-Achilleus




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


end of thread, other threads:[~2006-02-15 14:21 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-02-15 13:03 When to close a Statement Carlos Correia <carlos@m16e.com>
2006-02-15 13:23 ` Markus Schaber <schabi@logix-tt.com>
2006-02-15 14:21 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
2006-02-15 13:41   ` Carlos Correia <carlos@m16e.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