Connection object as an instance or local variable?

From: relaxedrob_at_optushome.com.au (relaxedrob_at_optusnet.com.au)
Date: 10/07/04


Date: 6 Oct 2004 20:20:04 -0700

Hi All!

I have a 'data manager class' that has a bunch of methods a bit like
this:

public Hotel [] getHotels()
{
  // Open a connection to the database.
  // Execute sql command.
  // Format results as a Hotel array.
  // Close database connection.
  // Return array.
}

Is it a better design to have the connection object as an instance
variable or leave it as a local variable in this case?

As an instance variable:
- may put less load on the database.
- allow me to keep a 'one client, one connection' paradigm.
- but I may need to synchronize access to the connection.
- I would have to expose a 'close()' method which doesn't really fit
the contract of a data manager (which should hide the fact that it may
or may not use a database) or:
- I put the connection closing code in a finalize() method or;
- use a thread run method somehow?

As a local variable:
- 'heavy' usage on the database.
- lots of over head for my application opening and closing all the
time..

Any advice would be most appreciated!

Rob
:)



Relevant Pages

  • Re: ADO Connection Timeout
    ... so what happens when a connection failure forces one station to revert ... to a local database? ... Further, you *will* have contention issues, Jet does not support record ... to the central server, but you are willing to live with periods where it ...
    (microsoft.public.data.ado)
  • Re: ADO Connection Timeout
    ... When the first test is run, the results are stored in the central database. ... to the central server, but you are willing to live with periods where it ... i.e. a local database or even a text file. ... to function until the connection can be restored to the server. ...
    (microsoft.public.data.ado)
  • Re: ADO Connection Timeout
    ... to the central server, but you are willing to live with periods where it ... i.e. a local database or even a text file. ... to function until the connection can be restored to the server. ...
    (microsoft.public.data.ado)
  • Re: ADO Connection Timeout
    ... much rather write stored procedures and use server side cursors. ... local database, it is vital that I get the information to a central server. ... Once the connection is restored, ...
    (microsoft.public.data.ado)
  • Re: ESQL/C documentation nightmare
    ... documentation about explicit and implicit connections. ... CONNECTION connection statements for new applications of Version 6.0 ... the SQL database statements (such as ...
    (comp.databases.informix)