|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.scaffold.sql.ConnectionAdaptor
public class ConnectionAdaptor
Base class for SQL connection adaptors. May be used as-is or subclassed.
This object does not provide any type of connection pooling. It simply provides a standard means for obtaining a connection whatever connection pool your application needs. Scaffold classes in the SQL package call this object internally.
To use as-is, a DataSource compliant connection pool must be available as "DATA_SOURCE" via JNDI. A subclass can locate the pool under any name by any means necessary.
A ConnectionAdaptor
can be instantiated once at
start-up, either from main() or via a servlet, like the
Scaffold ConnectionServlet.
Objects needing a connection can then call the static
method
ConnectionAdaptor.getPool().getConnection()
.
Most implementations will expect callers to use and close the connections promptly, since they would usually be part of a pool.
Subclasses can provide their own resource name or
getConnection() method, and a new constructor to
set the protected pool field to their own
instance. See PoolmanAdaptor
for an example.
ConnectionServlet
,
PoolmanAdaptor
Field Summary | |
---|---|
static String |
DATA_SOURCE_KEY
The default DataSource name. |
protected static DataSource |
datasource
Field to store singleton reference to default datasource. |
static String |
JNDI_CONTEXT_KEY
The default JNDI context for applications. |
protected static ConnectionAdaptor |
pool
Field to store singleton reference to adaptor pool. |
Constructor Summary | |
---|---|
ConnectionAdaptor()
|
Method Summary | |
---|---|
Connection |
getConnection()
Returns a JDBC connection from the default resource. |
Connection |
getConnection(String key)
Returns a JDBC connection from a connection pool or other named resource, to be used and closed promptly. |
protected DataSource |
getDataSource(String key)
Return datasource using JNDI lookup. |
String |
getKey()
Return the attribute under which our datasource is stored. |
protected String |
getMessage()
Return a message for an exception if pool returns null. |
static ConnectionAdaptor |
getPool()
Return adaptor instance to use in acquiring connection. |
void |
setKey(String key)
The attribute under which our datasource is stored. |
protected void |
setMessage(String message)
Set a message for an exception if pool returns null. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DATA_SOURCE_KEY
public static final String JNDI_CONTEXT_KEY
protected static DataSource datasource
protected static ConnectionAdaptor pool
if (null==pool) set pool = this
.
Constructor Detail |
---|
public ConnectionAdaptor()
Method Detail |
---|
protected String getMessage()
protected void setMessage(String message)
Message
- The message for an exception if pool returns nullpublic String getKey()
public void setKey(String key)
protected DataSource getDataSource(String key) throws SQLException
key
- The attribute name for the resource.
If null is passed, null is returned.
SQLException
public Connection getConnection() throws SQLException
getDatasource
to return the resource
associated with the default attribute name [getKey()].
The default datasource object is cached and reused in subsequent calls.
SQLException
- on SQL or other errors. May wrap other
exceptions depending on implementation. Will not return null.public Connection getConnection(String key) throws SQLException
Default implementation uses JNDI to lookup the resource named by getResource() ["DATASOURCE"]. Will not return null.
resource
- An attribute name for the resource to use for
this connection or null to call getResource()
SQLException
- on SQL or other errors. May wrap
other exceptions depending on implementation.
SQLException
- On any SQL error or if pool returns nullpublic static ConnectionAdaptor getPool()
This is the main entry method to the object.
Client's should call:
Adaptor.getPool().getConnection()
to acquire a connection from the default pool.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |