Thread: Where should I connect to the database?
Hi all, I would like to know which one is the best method of managing database connection in Java 1.3 (with PostgreSQL 7.1.2 JDBC). Option 1: Connect to the database in the "main" function and disconnect on program exit. Use it throughout the life of the application. "Connect once and use for ever". Option 2: Connect to the database near the code that uses it and disconnect after that block of code. "Use and throw away". Please help me. Thanks Dino __________________________________________________ Do You Yahoo!? Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. http://geocities.yahoo.com/ps/info1
Dino, I tend to get a connection when I need it, and release it right away. It sort of depends on what you are doing. My applications are servlets, so the connections need to be shared with users. To this end I use a connection pool. Dave -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Dino Cherian Sent: Friday, November 23, 2001 7:12 AM To: pgsql-jdbc@postgresql.org Subject: [JDBC] Where should I connect to the database? Hi all, I would like to know which one is the best method of managing database connection in Java 1.3 (with PostgreSQL 7.1.2 JDBC). Option 1: Connect to the database in the "main" function and disconnect on program exit. Use it throughout the life of the application. "Connect once and use for ever". Option 2: Connect to the database near the code that uses it and disconnect after that block of code. "Use and throw away". Please help me. Thanks Dino __________________________________________________ Do You Yahoo!? Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. http://geocities.yahoo.com/ps/info1 ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
Option 2 if you want your application to scale. -----Original Message----- From: Dino Cherian [mailto:inimss@yahoo.com] Sent: Friday, November 23, 2001 4:12 AM To: pgsql-jdbc@postgresql.org Subject: [JDBC] Where should I connect to the database? Hi all, I would like to know which one is the best method of managing database connection in Java 1.3 (with PostgreSQL 7.1.2 JDBC). Option 1: Connect to the database in the "main" function and disconnect on program exit. Use it throughout the life of the application. "Connect once and use for ever". Option 2: Connect to the database near the code that uses it and disconnect after that block of code. "Use and throw away". Please help me. Thanks Dino __________________________________________________ Do You Yahoo!? Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. http://geocities.yahoo.com/ps/info1 ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
Option 1 if you want your application more fast. Vic On Fri, 23 Nov 2001 04:11:50 -0800 (PST) Dino Cherian <inimss@yahoo.com> wrote: > Hi all, > > I would like to know which one is the best method of > managing database connection in Java 1.3 (with > PostgreSQL 7.1.2 JDBC). > > Option 1: > Connect to the database in the "main" function and > disconnect on program exit. Use it throughout the life > of the application. "Connect once and use for ever". > > Option 2: > Connect to the database near the code that uses it > and disconnect after that block of code. "Use and > throw away". > > Please help me. > > Thanks > Dino > > __________________________________________________ > Do You Yahoo!? > Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. > http://geocities.yahoo.com/ps/info1 > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >
Performance improvement with Option 1 is nigligible if you are using connection pooling. Jayesh -----Original Message----- From: Vicktor [mailto:vic@adv.ru] Sent: Friday, November 23, 2001 9:51 AM To: Dino Cherian Cc: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Where should I connect to the database? Option 1 if you want your application more fast. Vic On Fri, 23 Nov 2001 04:11:50 -0800 (PST) Dino Cherian <inimss@yahoo.com> wrote: > Hi all, > > I would like to know which one is the best method of > managing database connection in Java 1.3 (with > PostgreSQL 7.1.2 JDBC). > > Option 1: > Connect to the database in the "main" function and > disconnect on program exit. Use it throughout the life > of the application. "Connect once and use for ever". > > Option 2: > Connect to the database near the code that uses it > and disconnect after that block of code. "Use and > throw away". > > Please help me. > > Thanks > Dino > > __________________________________________________ > Do You Yahoo!? > Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. > http://geocities.yahoo.com/ps/info1 > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > ---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives? http://archives.postgresql.org
Option 1 & 2 utilizing a connection pool if you want scalability, and speed. Dave -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Vicktor Sent: Friday, November 23, 2001 12:51 PM To: Dino Cherian Cc: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Where should I connect to the database? Option 1 if you want your application more fast. Vic On Fri, 23 Nov 2001 04:11:50 -0800 (PST) Dino Cherian <inimss@yahoo.com> wrote: > Hi all, > > I would like to know which one is the best method of > managing database connection in Java 1.3 (with > PostgreSQL 7.1.2 JDBC). > > Option 1: > Connect to the database in the "main" function and disconnect on > program exit. Use it throughout the life of the application. "Connect > once and use for ever". > > Option 2: > Connect to the database near the code that uses it > and disconnect after that block of code. "Use and > throw away". > > Please help me. > > Thanks > Dino > > __________________________________________________ > Do You Yahoo!? > Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. > http://geocities.yahoo.com/ps/info1 > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > ---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives? http://archives.postgresql.org
Dino- It depends on the nature of your application: If you expect to have many copies of the application running (like in a servlet app), you'll want to release the resource (connection), otherwise you'll have a lot of processes idling out there when they aren't needed & may bump into other resource limits. The drawback is that it takes time to re-establish a connection each time you need it. As some of the other folks mentioned, using a connection pool would be the best approach in this circumstance. If you don't expect to have many copies of the app running, you might as well leave the connection open & get better performance by having the connection always ready. -NF > -----Original Message----- > From: pgsql-jdbc-owner@postgresql.org > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Dino Cherian > Sent: Friday, November 23, 2001 7:12 AM > To: pgsql-jdbc@postgresql.org > Subject: [JDBC] Where should I connect to the database? > > > Hi all, > > I would like to know which one is the best method of > managing database connection in Java 1.3 (with > PostgreSQL 7.1.2 JDBC). > > Option 1: > Connect to the database in the "main" function and > disconnect on program exit. Use it throughout the life > of the application. "Connect once and use for ever". > > Option 2: > Connect to the database near the code that uses it > and disconnect after that block of code. "Use and > throw away". > > Please help me. > > Thanks > Dino > > __________________________________________________ > Do You Yahoo!? > Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. > http://geocities.yahoo.com/ps/info1 > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >