Java Reference
In-Depth Information
Listing 7.2
A method that provides a pooled connection
protected Connection getPooledConnection(String driver, String URL, String
userID, String password)
{
Connection conn = null;
Create parameter
list to access
naming system.
try
{
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY,
CNInitialContextFactory.class.getName());
Context context = new InitialContext(parms); c Access naming context
ds = (DataSource)context.lookup(getDataSourceName())
conn = ds.getConnection(userID, password);
}
catch (Throwable t)
{
try
B
d Get data
source factory
object.
Get connection
from data source
object.
Data source not found;
try to build one.
f
{
DataSourceFactory factory =
new DataSourceFactory(); g Create data source factory.
Attributes attrs = new Attributes();
attrs.name = getDataSourceName();
attrs.driver = getDriver();
attrs.url = getURL();
attrs.max = 30;
ds = factory.createJDBCDataSource(attrs);
try {
factory.bindDataSource(ds);
Create a JDBC specific
data source.
h
i Bind the factory
to the data source.
} catch (NamingException namingExc){
namingExc.printStackTrace();
}
conn = ds.getConnection(userID, password);
}
catch (Throwable t1)
{
t1.printStackTrace();
}
}
return conn;
}
}
b Our data source will be a named resource, so we need to get a context. The param-
eters to the context constructor are stored in a hash table, which is then passed to
the constructor. We'll use the context to find the data source that we create in steps
7 and 8.
Search WWH ::




Custom Search