Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/jdbc-pool/doc/jdbc-pool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,16 @@
Default value is <code>false</code>.<br/>
</p>
</attribute>
<attribute name="useRequestBoundaries" required="false">
<p>(boolean) If <code>true</code>, the pool sends the JDBC 4.3 request boundary hints to the driver:
<code>Connection.beginRequest()</code> when a connection is borrowed and
<code>Connection.endRequest()</code> when it is returned. The behaviour of these hints is vendor specific
and some drivers close the statements created during the request in <code>endRequest()</code>, so the
hints are not sent for a connection that uses the <code>StatementCache</code> interceptor. Do not enable
this attribute if a custom interceptor or the application keeps statements across borrows.
Default value is <code>false</code>.<br/>
</p>
</attribute>
<attribute name="commitOnReturn" required="false">
<p>(boolean) If <code>autoCommit==false</code> then the pool can complete the transaction by calling commit on the connection as it is returned to the pool
If <code>rollbackOnReturn==true</code> then this attribute is ignored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,9 @@ private Connection setupConnection(PooledConnection con, boolean countBorrow) th
// optimises per-request session handling knows the unit of work
// begins here. The matching endRequest() is sent at the return,
// in returnConnection().
invokeRequestHint(BEGIN_REQUEST, con.getConnection());
if (sendRequestHints(con)) {
invokeRequestHint(BEGIN_REQUEST, con.getConnection());
}
} catch (Throwable x) {
ExceptionUtils.handleThrowable(x);
SQLException s = new SQLException();
Expand Down Expand Up @@ -680,6 +682,30 @@ private static Method getRequestHintMethod(String methodName) {
}
}

/**
* Determines whether the JDBC 4.3 request boundary hints are sent for a pooled connection.
*
* @param con the pooled connection, with its interceptor chain already built
*
* @return {@code true} if the hints are enabled and no statement cache is part of the connection's interceptor
* chain
*/
private boolean sendRequestHints(PooledConnection con) {
if (!getPoolProperties().isUseRequestBoundaries()) {
return false;
}
// The driver may close the statements of a request in endRequest() - the behaviour of the hints is vendor
// specific - while a statement cache hands a statement of one borrow to the next borrow of the session.
JdbcInterceptor interceptor = con.getHandler();
while (interceptor != null) {
if (interceptor instanceof StatementCache) {
return false;
}
interceptor = interceptor.getNext();
}
return true;
}

/**
* Sends one of the JDBC 4.3 request boundary hints to a physical connection. The hint is advisory - the
* specification lets the driver treat it as a no-op - so a driver that refuses one does not fail the borrow or the
Expand Down Expand Up @@ -2224,7 +2250,9 @@ private void returnConnection(PooledConnection con, boolean countReturn) {
// discarded instead of going back to the pool is
// not hinted; it is about to be closed, which
// subsumes the hint.
invokeRequestHint(END_REQUEST, con.getConnection());
if (sendRequestHints(con)) {
invokeRequestHint(END_REQUEST, con.getConnection());
}
if (isClosed()) {
// The pool closed while this return was in
// flight: the checks above each read the flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public DataSourceFactory() {
/** The {@code rollbackOnReturn} property name. */
protected static final String PROP_ROLLBACKONRETURN = "rollbackOnReturn";

/** The {@code useRequestBoundaries} property name. */
protected static final String PROP_USEREQUESTBOUNDARIES = "useRequestBoundaries";

/**
* Property name for useDisposableConnectionFacade. The property is deprecated and has no effect; the disposable
* connection facade is always used.
Expand Down Expand Up @@ -254,7 +257,7 @@ public DataSourceFactory() {
PROP_ABANDONWHENPERCENTAGEFULL, PROP_MAXAGE, PROP_USE_LOCK, PROP_DATASOURCE, PROP_DATASOURCE_JNDI,
PROP_SUSPECT_TIMEOUT, PROP_ALTERNATE_USERNAME_ALLOWED, PROP_COMMITONRETURN, PROP_ROLLBACKONRETURN,
PROP_USEDISPOSABLECONNECTIONFACADE, PROP_LOGVALIDATIONERRORS, PROP_PROPAGATEINTERRUPTSTATE,
PROP_IGNOREEXCEPTIONONPRELOAD, PROP_USESTATEMENTFACADE };
PROP_IGNOREEXCEPTIONONPRELOAD, PROP_USESTATEMENTFACADE, PROP_USEREQUESTBOUNDARIES };

/**
* Names of the reference addresses that the JNDI container itself places
Expand Down Expand Up @@ -917,6 +920,14 @@ public static PoolConfiguration parsePoolProperties(Properties properties) {
}
}

value = properties.getProperty(PROP_USEREQUESTBOUNDARIES);
if (value != null) {
Boolean b = parseBoolean(PROP_USEREQUESTBOUNDARIES, value);
if (b != null) {
poolProperties.setUseRequestBoundaries(b.booleanValue());
}
}

value = properties.getProperty(PROP_USEDISPOSABLECONNECTIONFACADE);
if (value != null) {
Boolean b = parseBoolean(PROP_USEDISPOSABLECONNECTIONFACADE, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,16 @@ public boolean getRollbackOnReturn() {
return getPoolProperties().getRollbackOnReturn();
}

@Override
public void setUseRequestBoundaries(boolean useRequestBoundaries) {
getPoolProperties().setUseRequestBoundaries(useRequestBoundaries);
}

@Override
public boolean isUseRequestBoundaries() {
return getPoolProperties().isUseRequestBoundaries();
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,27 @@ public interface PoolConfiguration {
*/
boolean getRollbackOnReturn();

/**
* Set to true if you want the connection pool to send the JDBC 4.3 request boundary hints to the driver:
* {@link java.sql.Connection#beginRequest()} when a connection is borrowed and
* {@link java.sql.Connection#endRequest()} when it is returned. The behaviour of these hints is vendor specific and
* some drivers close the statements created during the request in {@code endRequest()}, so the hints are never
* sent for a connection whose interceptor chain includes a
* {@link org.apache.tomcat.jdbc.pool.interceptor.StatementCache}. The hints are not sent on a runtime that
* predates JDBC 4.3. The default value is false.
*
* @param useRequestBoundaries set to true if the pool should send the request boundary hints
*/
void setUseRequestBoundaries(boolean useRequestBoundaries);

/**
* Returns whether the pool sends the JDBC 4.3 request boundary hints. See
* {@link PoolConfiguration#setUseRequestBoundaries(boolean)} for details.
*
* @return {@code true} if the pool should send the request boundary hints
*/
boolean isUseRequestBoundaries();

/**
* Stores the value of the deprecated {@code useDisposableConnectionFacade} property. The pool always wraps the
* connection handle in a facade that disallows use of the connection after {@link java.sql.Connection#close()} has
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public PoolProperties() {
private volatile boolean commitOnReturn = false;
/** Whether to rollback on connection return. */
private volatile boolean rollbackOnReturn = false;
/** Whether to send the JDBC 4.3 request boundary hints. */
private volatile boolean useRequestBoundaries = false;
/**
* Whether to use a disposable connection facade.
*
Expand Down Expand Up @@ -2288,6 +2290,16 @@ public boolean getRollbackOnReturn() {
return this.rollbackOnReturn;
}

@Override
public void setUseRequestBoundaries(boolean useRequestBoundaries) {
this.useRequestBoundaries = useRequestBoundaries;
}

@Override
public boolean isUseRequestBoundaries() {
return this.useRequestBoundaries;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,16 @@ public boolean getRollbackOnReturn() {
return getPoolProperties().getRollbackOnReturn();
}

@Override
public void setUseRequestBoundaries(boolean useRequestBoundaries) {
getPoolProperties().setUseRequestBoundaries(useRequestBoundaries);
}

@Override
public boolean isUseRequestBoundaries() {
return getPoolProperties().isUseRequestBoundaries();
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@
is="false"
writeable="false"/>

<attribute name="useRequestBoundaries"
description="If true the pool sends the JDBC 4.3 request boundary hints to the driver when a connection is borrowed and returned"
type="java.lang.Boolean"
is="true"
writeable="false"/>

<attribute name="commitOnReturn"
description="If autoCommit==false then the pool can complete the transaction by calling commit on the connection as it is returned to the pool"
type="java.lang.Boolean"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ void anAbortTakesTheSessionThroughTheDisconnectHookWithoutEndingTheRequest()
DataSource ds = newDataSource();
ds.setDriverClassName(SpecAbortDriver.class.getName());
ds.getPoolProperties().setInitialSize(0);
ds.getPoolProperties().setUseRequestBoundaries(true);
ds.getPoolProperties().setJdbcInterceptors(
DisconnectRecordingInterceptor.class.getName());
ds.createPool();
Expand Down
Loading
Loading