Accessing in a static way

R

Rizwan

I have 2 applications: one is stand-alone and one is using J2EE container.
So for Connection logic I wrote code to support both DataSource (to be used
by J2EE app) and DriverManager (to be used by stand-alone app). So

public interface ConnectionManager {
public void configure( Properties properties );
public Connection getConnection();
public void closeConnection( Connection connection );
}


Then I wrote a DataSource and a DriverManager implementation of
ConnectionManager (The DataSource one uses JNDI lookup and connection
pooling):

public class DataSourceConnectionManager implements ConnectionManager {
....
}
public class DriverManagerConnectionManager implements ConnectionManager {
....
}


Then I wrote factory:

public final class ConnectionManagerFactory {
public static final String CONNECTIONTYPE = "CONNECTIONTYPE";
public static final int DATASOURCE = 1;
public static final int DRIVERMANAGER = 2;

public static ConnectionManager getConnectionManager( Properties
properties ) {
ConnectionManager cm = null;
int connectionType = new Integer( properties.getProperty(
CONNECTIONTYPE ) ).intValue();
if ( connectionType == DATASOURCE )
cm = new DataSourceConnectionManager();
else if ( connectionType == DRIVERMANAGER )
cm = new DriverManagerConnectionManager();

cm.configure( properties );
return cm;
}
}


To Get Connection:

Properties props = new Properties();
// populate the props accordingly (stand-alone or J2EE)
....
ConnectionManager cm = ConnectionManagerFactory.getConnectionManager(
props );
Connection connection = cm.getConnection();
....
cm.closeConnection( connection );


The above code is working very good. I have no problems so far.

Now I want to build a utiliy class for connection obtaining logic. I want it
to have these static methods:

public class ConnectionUtil {
public static Connection getConnectionForStandalone(Properties props)
{...}
public static Connection getConnectionForJ2EE(Properties props) {...}
public static void closeConnection(Connection conn) {...}
}

The stand-alone app programmers will use getConnectionForStandalone() while
J2EE app programmers will use getConnectionForJ2EE() to obtain Connection.

My concern is how to do it? Can anyobdy help me in this regard? Thanks.
 
O

Oliver Wong

Rizwan said:
Anyone please? Thanks

I don't know if you have the best architecture, but if you really want a
utility class with those methods, here's how to implement it:

public class ConnectionUtil {
public static Connection getConnectionForStandalone(Properties props) {
ConnectionManager cm =
ConnectionManagerFactory.getConnectionManager(props);
return connection = cm.getConnection();
}

public static Connection getConnectionForJ2EE(Properties props) {
return getConnectionForStandalone(props);
//Since they seem to have the same code
}

public static void closeConnection(Connection conn) {
conn.close(); //Add this method to class Connection.
}
}

- Oliver
 
R

Rizwan

Oliver Wong said:
I don't know if you have the best architecture, but if you really want
a utility class with those methods, here's how to implement it:

public class ConnectionUtil {
public static Connection getConnectionForStandalone(Properties props) {
ConnectionManager cm =
ConnectionManagerFactory.getConnectionManager(props);
return connection = cm.getConnection();
}

public static Connection getConnectionForJ2EE(Properties props) {
return getConnectionForStandalone(props);
//Since they seem to have the same code
}

public static void closeConnection(Connection conn) {
conn.close(); //Add this method to class Connection.
}
}

- Oliver

Thanks Oliver for your reply. getConnectionForStandalone() and
getConnectionForJ2EE() are a little different.
In getConnectionForStandalone() method I put this code as the first line:
props.put( ConnectionManagerFactory.CONNECTIONTYPE, new
Integer(ConnectionManagerFactory.DRIVERMANAGER) );

In getConnectionForJ2EE() method I put this code as the first line:
props.put( ConnectionManagerFactory.CONNECTIONTYPE, new
Integer(ConnectionManagerFactory.DATASOURCE) );

This way the other programmers dont have to know how to instruct their props
to use the appropriate connection type.

Regarding your solution, after I got the Connection, I will loose the
ConnectionManager which is used to get this Connection. Then there is no use
of closeConnection() method of ConnectionManager interface. If
ConnectionManager does create/open some other things while getting
Connection, how can i make sure that those things get closed/destroyed when
closing Connection?

Another thing is if I relase ConnectionUtil then how can I make sure that
ConnectionManagerFactory and ConnectionManager are not used directly to get
Connection?

Thanks
 
O

Oliver Wong

Rizwan said:
Thanks Oliver for your reply. getConnectionForStandalone() and
getConnectionForJ2EE() are a little different.
In getConnectionForStandalone() method I put this code as the first line:
props.put( ConnectionManagerFactory.CONNECTIONTYPE, new
Integer(ConnectionManagerFactory.DRIVERMANAGER) );

In getConnectionForJ2EE() method I put this code as the first line:
props.put( ConnectionManagerFactory.CONNECTIONTYPE, new
Integer(ConnectionManagerFactory.DATASOURCE) );

Sorry, I didn't notice that difference. You can just put the above code
in the appropriate functions, rather than have one function call the other
like I did.
Regarding your solution, after I got the Connection, I will loose the
ConnectionManager which is used to get this Connection. Then there is no
use of closeConnection() method of ConnectionManager interface. If
ConnectionManager does create/open some other things while getting
Connection, how can i make sure that those things get closed/destroyed
when closing Connection?

Create a delegate function containing everything you want done when the
connection closes. Pass the delegate to the Connection object. Have
Connection call this function when it closes itself.
Another thing is if I relase ConnectionUtil then how can I make sure that
ConnectionManagerFactory and ConnectionManager are not used directly to
get Connection?

You could make all the relevant methods in ConnectionManagerFactory and
ConnectionManager package-visibility (AKA default visibility) and put them
all in the same package as ConnectionUtil so that no one can "see" those
methods except for ConnectionUtil itself.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top