Class JdbcDataSource
- All Implemented Interfaces:
AutoCloseable,Wrapper,CommonDataSource,DataSource
DataSource that delegates every call to an application-owned source and records
connection checkouts through the Axiom Metrics SPI.
DataSource counted = new JdbcDataSource(applicationOwnedDataSource, options, app.metrics());
The delegate is never closed by this wrapper, on any path. Each returned Connection is
a delegate that forwards to the real connection; the first close() on it counts one
release and closes the real connection. A later close() is a no-op. The name from
JdbcOptions is the only tag value on the connection series, so the series stay bounded
however many connections the application opens. No SQL, URL, user, password or request value is
ever recorded.
The acquired counter increases on every successful checkout. The released
counter increases once per returned connection that is closed. The waited counter
increases on a checkout that overlapped an outstanding checkout of this wrapper: it reports
contention seen here, not the delegate's internal queue depth. The wait timer records the
time spent inside the delegate's getConnection, so an application can see whether a wait
was a real one. Health probes use the unwrapped source and are not counted.
Instances are thread-safe. close() never closes the delegate DataSource; it
closes only wrapped connections that are still open, and only when JdbcOptions.closeConnectionsOnShutdown() is true.
-
Constructor Summary
ConstructorsConstructorDescriptionJdbcDataSource(DataSource delegate, JdbcOptions options, Metrics metrics) Wraps a source with checked-out connection counters. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the wrapped connections this wrapper still tracks, ifJdbcOptions.closeConnectionsOnShutdown()is true.Checks out a connection from the delegate and returns a wrapper whose firstclose()counts one release.getConnection(String user, String password) Checks out a connection with the supplied credentials, recording the same series asgetConnection().intReturns the delegate's login timeout.Returns the delegate's log writer.Returns the delegate's parent logger.booleanisWrapperFor(Class<?> type) Reports whether this wrapper or its delegate implements a type.voidsetLoginTimeout(int seconds) Sets the delegate's login timeout.voidsetLogWriter(PrintWriter out) Sets the delegate's log writer.<T> TUnwraps this wrapper or asks the delegate.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
JdbcDataSource
Wraps a source with checked-out connection counters.- Parameters:
delegate- application-owned source, never closed by this wrapperoptions- bounds and the bounded metric namemetrics- where the counters and timer are recorded- Throws:
NullPointerException- if an argument is null
-
-
Method Details
-
getConnection
Checks out a connection from the delegate and returns a wrapper whose firstclose()counts one release. Credentials are passed through and never recorded.- Specified by:
getConnectionin interfaceDataSource- Returns:
- a wrapped connection owned by the caller
- Throws:
SQLException- if the delegate cannot supply a connection
-
getConnection
Checks out a connection with the supplied credentials, recording the same series asgetConnection(). The credentials are never recorded.- Specified by:
getConnectionin interfaceDataSource- Parameters:
user- database userpassword- database password- Returns:
- a wrapped connection owned by the caller
- Throws:
SQLException- if the delegate cannot supply a connection
-
getLoginTimeout
Returns the delegate's login timeout.- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Returns:
- the delegate's login timeout in seconds
- Throws:
SQLException- if the delegate fails
-
setLoginTimeout
Sets the delegate's login timeout.- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Parameters:
seconds- timeout in seconds- Throws:
SQLException- if the delegate fails
-
getLogWriter
Returns the delegate's log writer.- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource- Returns:
- the delegate's log writer, possibly null
- Throws:
SQLException- if the delegate fails
-
setLogWriter
Sets the delegate's log writer.- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource- Parameters:
out- log writer, possibly null- Throws:
SQLException- if the delegate fails
-
getParentLogger
Returns the delegate's parent logger.- Specified by:
getParentLoggerin interfaceCommonDataSource- Returns:
- the delegate's parent logger
- Throws:
SQLFeatureNotSupportedException- if the delegate does not support it
-
unwrap
Unwraps this wrapper or asks the delegate. No SQL object is exposed by this wrapper itself.- Specified by:
unwrapin interfaceWrapper- Type Parameters:
T- requested interface type- Parameters:
type- requested interface- Returns:
- an object of that type
- Throws:
SQLException- if the delegate cannot unwrap
-
isWrapperFor
Reports whether this wrapper or its delegate implements a type.- Specified by:
isWrapperForin interfaceWrapper- Parameters:
type- requested interface- Returns:
- true if it can be unwrapped
- Throws:
SQLException- if the delegate fails
-
close
public void close()Closes the wrapped connections this wrapper still tracks, ifJdbcOptions.closeConnectionsOnShutdown()is true. It never closes the delegateDataSource. Failures from the underlying connections are swallowed and reported by class name only, so a driver message cannot leak a URL or credential to logs. Idempotent.- Specified by:
closein interfaceAutoCloseable
-