You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matt Raible <ma...@raibledesigns.com> on 2002/02/05 22:08:51 UTC

Service Locator pattern with Poolman & Struts

Is it a good idea to use a Service Locator Pattern with Poolman?  I have the
following class:

public class ServiceLocator {

    /** singleton's private instance */
    private static ServiceLocator me;

    static {
        me = new ServiceLocator();
    }

    private ServiceLocator() {}

    /** returns the Service Locator instance */
    static public ServiceLocator getInstance() {
        return me;
    }

    private DataSource ds = null;
    private Connection con = null;

    /** Retrieves a connection from the connection pool */
    public Connection getConnection() {
        try {
            ds = PoolMan.getDataSource();
            con = ds.getConnection();
        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        } catch(Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }
				return con;
    }

		/** Returns a connection from the connection pool */
    public void closeConnection(Connection con) {
        JDBCPool.closeConnection(con);
    }
}

So in my Struts Action classes, I can simply get and close connections with
the following code:

	ServiceLocator serviceLocator = ServiceLocator.getInstance();

      con = serviceLocator.getConnection();
	serviceLocator.closeConnection(con);

Any thoughts are appreciated.

Matt


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>