This isnt really related to Tomcat, but I really don't care:

You could make the connection static in the class DatabaseManager, so that
your application only uses one connection against DB. This could (?) cause
problems with Threading and several users accessing at once (I guess...).
Really not sure about this though...

However, to be 100% sure that the connection is closed and the Connection
object is destroyed by the garbage-collector, you should add connection =
null in the class MyMainClass as well, otherwise this class would hold a
copy of this object until your program terminates...

You should consider using a layered approach to programming. 
Create a class, DataIO (reading and writing) that has abstract methods for
retrieving data and updating them etc.
Create classes for your data, which act as dataholders. I.e. create a class
MyMainClassData that holds the data retrieved in MyMainClass by the
database. Then create abstract methods for retrieving and manipulating the
data in this object. 

Finally you implement these methods in yet another class, DatabaseDataIO
that implements your interface against the database. 

The solution would look something like this:

MyMainClass:
public static final void main(String[] args) {
   New MyMainClass().doTheStuff(neededArgs);
}

Public void doTheStuff() {
        DatabaseDataIO ddio = new DatbaseDataIO();
        MyMainClassData data = ddio.getData();

        ... manipulate data

        Ddio.saveData(data);
}

DataIO:
Public abstract MyMainClassData getData();
Public abstract void saveData(MyMainClassData data);

DatabaseDataIO:
Extend and Implement the methods of DataIO

MyMainClassData:
Private fields for every data needed. 
Get and set methods for manipulating the data


In this case you could, of course, still use DataBaseManager, but with or
without it, when you find yourself creating the Main-class, you don't have
to worry about whether or not the connection is being closed. You can trust
that the DatabaseDataIO class does this for you. 

Hope my late Saturday night writing is understandable.

Öyvind Johansen
ETC ElectricTimeCar

-----Opprinnelig melding-----
Fra: Tony Smith [mailto:[EMAIL PROTECTED] 
Sendt: 23. juli 2005 23:19
Til: Tomcat Users List
Emne: close pooled conection-by value or by reference

I think this is a classic java question: Pass by
reference or by value. I have a DatabaseManager class
which takes care of get conection from connection pool
and release the connectoin. I am not sure about the
release part. Here is my code:

class MyMainClass

public static final void main(String[] args){

    Connection connection =
DatabaseManager.getConnection();

    ....//jdbc work.

   DatabaseManager.closeConnection(connection);
...
}


public class DatabaseManager {
 
    public static Connection getConnection(){
        Connection connection =
getConectionFromDataSource();

       return connection;
    }
    
    public static void closeConnection(Connection
conn){
        if (conn != null){
                try{
                        conn.close();
                }catch(SQLException sqle){
                        sqle.printStackTrace();
                }
        }
        conn = null;
    }}


I am not sure if the connection is released....


Thanks,




                
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to