tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Øyvind Johansen <>
Subject SV: close pooled conection-by value or by reference
Date Sat, 23 Jul 2005 21:41:16 GMT
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:

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

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

	... manipulate data


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

Extend and Implement the methods of DataIO

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 [] 
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 =

    ....//jdbc work.


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

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

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


Start your day with Yahoo! - make it your home page 

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message