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.
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.