commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthew Firth <commons-...@matthew.matera.net.au>
Subject re: [DBCP] NamedDataSourceFactory to share DataSources between Servlet Contexts
Date Thu, 22 Aug 2002 05:38:30 GMT
Isaac,

My thought would be that an extension of BasicDataSourceFactory is
better than creating a paralell implementation.   If new resource
parameters are supported by BasicDataSource & BasicDataSourceFactory (I
have a list of them I plan to add sometime....) then this will work
transparantly - i.e. without having to place identical changes into two
separate classes.

I would have implemented it as below.  No idea if this code even compiles,
let alone functions properly (and will probably be mangled by MTAs
everywhere).  My apologies if there something obvious here that I have
overlooked & grossly simplified!


matthew


public class NamedDataSourceFactory extends BasicDataSourceFactory

static private Hashtable namedPools = new Hashtable();

public Object getObjectInstance(Object obj, Name name, 
		Context nameCtx, Hashtable environment)
        throws Exception {

        Reference ref = (Reference) obj;
        RefAddr ra = null;
        String poolName = "defaultPoolName";

        // Get the name of the pool to use
        ra = ref.get("poolName");
        if (ra != null) {
            poolName = ra.getContent().toString();
        }

        // Check the Map to see if a pool with that name already exists
        BasicDataSource namedDataSource = 
		(BasicDataSource)namedPools.get(poolName);
        if (namedDataSource != null) 
          return namedDataSource
        else {
          Object dataSource =
		super.getObjectInstance(obj,name,nameCtx,environment);
         
          // Put the DataSource in the namedPools Map
          namedPools.put(poolName, dataSource);

          return (dataSource);
       }
}



--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message