Repository: commons-dbcp Updated Branches: refs/heads/master cb48137d5 -> 0a928ca2b
Line length 120 and standard formatting. Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/0a928ca2 Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/0a928ca2 Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/0a928ca2 Branch: refs/heads/master Commit: 0a928ca2b7a16fd49b49223bba4cd778207d0fcb Parents: cb48137 Author: Gary Gregory <garydgreg...@gmail.com> Authored: Mon Jun 11 17:44:46 2018 -0600 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Mon Jun 11 17:44:46 2018 -0600 ---------------------------------------------------------------------- .../dbcp2/managed/BasicManagedDataSource.java | 82 ++++++++------ .../managed/DataSourceXAConnectionFactory.java | 47 +++++--- .../dbcp2/managed/LocalXAConnectionFactory.java | 109 +++++++++++-------- .../dbcp2/managed/ManagedConnection.java | 15 +-- .../dbcp2/managed/ManagedDataSource.java | 40 +++---- .../managed/PoolableManagedConnection.java | 38 ++++--- .../PoolableManagedConnectionFactory.java | 1 + .../dbcp2/managed/TransactionContext.java | 51 +++++---- .../managed/TransactionContextListener.java | 7 +- .../dbcp2/managed/TransactionRegistry.java | 34 +++--- .../dbcp2/managed/XAConnectionFactory.java | 22 ++-- 11 files changed, 252 insertions(+), 194 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java b/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java index 12fe740..c685134 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java @@ -30,22 +30,18 @@ import javax.transaction.TransactionManager; import java.sql.SQLException; /** - * <p>BasicManagedDataSource is an extension of BasicDataSource which - * creates ManagedConnections. This data source can create either - * full two-phase-commit XA connections or one-phase-commit - * local connections. Both types of connections are committed or - * rolled back as part of the global transaction (a.k.a. XA - * transaction or JTA Transaction), but only XA connections can be - * recovered in the case of a system crash. + * <p> + * BasicManagedDataSource is an extension of BasicDataSource which creates ManagedConnections. This data source can + * create either full two-phase-commit XA connections or one-phase-commit local connections. Both types of connections + * are committed or rolled back as part of the global transaction (a.k.a. XA transaction or JTA Transaction), but only + * XA connections can be recovered in the case of a system crash. * </p> - * <p>BasicManagedDataSource adds the TransactionManager and XADataSource - * properties. The TransactionManager property is required and is - * used to enlist connections in global transactions. The XADataSource - * is optional and if set is the class name of the XADataSource class - * for a two-phase-commit JDBC driver. If the XADataSource property - * is set, the driverClassName is ignored and a DataSourceXAConnectionFactory - * is created. Otherwise, a standard DriverConnectionFactory is created - * and wrapped with a LocalXAConnectionFactory. + * <p> + * BasicManagedDataSource adds the TransactionManager and XADataSource properties. The TransactionManager property is + * required and is used to enlist connections in global transactions. The XADataSource is optional and if set is the + * class name of the XADataSource class for a two-phase-commit JDBC driver. If the XADataSource property is set, the + * driverClassName is ignored and a DataSourceXAConnectionFactory is created. Otherwise, a standard + * DriverConnectionFactory is created and wrapped with a LocalXAConnectionFactory. * </p> * * @see BasicDataSource @@ -76,14 +72,17 @@ public class BasicManagedDataSource extends BasicDataSource { } /** - * <p>Sets the XADataSource instance used by the XAConnectionFactory.</p> * <p> - * Note: this method currently has no effect once the pool has been - * initialized. The pool is initialized the first time one of the - * following methods is invoked: <code>getConnection, setLogwriter, - * setLoginTimeout, getLoginTimeout, getLogWriter.</code></p> + * Sets the XADataSource instance used by the XAConnectionFactory. + * </p> + * <p> + * Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first + * time one of the following methods is invoked: <code>getConnection, setLogwriter, + * setLoginTimeout, getLoginTimeout, getLogWriter.</code> + * </p> * - * @param xaDataSourceInstance XADataSource instance + * @param xaDataSourceInstance + * XADataSource instance */ public synchronized void setXaDataSourceInstance(final XADataSource xaDataSourceInstance) { this.xaDataSourceInstance = xaDataSourceInstance; @@ -92,6 +91,7 @@ public class BasicManagedDataSource extends BasicDataSource { /** * Gets the required transaction manager property. + * * @return the transaction manager used to enlist connections */ public TransactionManager getTransactionManager() { @@ -100,6 +100,7 @@ public class BasicManagedDataSource extends BasicDataSource { /** * Gets the transaction registry. + * * @return the transaction registry associating XAResources with managed connections */ protected synchronized TransactionRegistry getTransactionRegistry() { @@ -108,7 +109,9 @@ public class BasicManagedDataSource extends BasicDataSource { /** * Sets the required transaction manager property. - * @param transactionManager the transaction manager used to enlist connections + * + * @param transactionManager + * the transaction manager used to enlist connections */ public void setTransactionManager(final TransactionManager transactionManager) { this.transactionManager = transactionManager; @@ -116,6 +119,7 @@ public class BasicManagedDataSource extends BasicDataSource { /** * Gets the optional XADataSource class name. + * * @return the optional XADataSource class name */ public synchronized String getXADataSource() { @@ -124,7 +128,9 @@ public class BasicManagedDataSource extends BasicDataSource { /** * Sets the optional XADataSource class name. - * @param xaDataSource the optional XADataSource class name + * + * @param xaDataSource + * the optional XADataSource class name */ public synchronized void setXADataSource(final String xaDataSource) { this.xaDataSource = xaDataSource; @@ -136,10 +142,12 @@ public class BasicManagedDataSource extends BasicDataSource { throw new SQLException("Transaction manager must be set before a connection can be created"); } - // If xa data source is not specified a DriverConnectionFactory is created and wrapped with a LocalXAConnectionFactory + // If xa data source is not specified a DriverConnectionFactory is created and wrapped with a + // LocalXAConnectionFactory if (xaDataSource == null) { final ConnectionFactory connectionFactory = super.createConnectionFactory(); - final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(getTransactionManager(), connectionFactory); + final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(getTransactionManager(), + connectionFactory); transactionRegistry = xaConnectionFactory.getTransactionRegistry(); return xaConnectionFactory; } @@ -163,15 +171,16 @@ public class BasicManagedDataSource extends BasicDataSource { } // finally, create the XAConnectionFactory using the XA data source - final XAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(getTransactionManager(), xaDataSourceInstance, getUsername(), getPassword()); + final XAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(getTransactionManager(), + xaDataSourceInstance, getUsername(), getPassword()); transactionRegistry = xaConnectionFactory.getTransactionRegistry(); return xaConnectionFactory; } @Override protected DataSource createDataSourceInstance() throws SQLException { - final PoolingDataSource<PoolableConnection> pds = - new ManagedDataSource<>(getConnectionPool(), transactionRegistry); + final PoolingDataSource<PoolableConnection> pds = new ManagedDataSource<>(getConnectionPool(), + transactionRegistry); pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed()); return pds; } @@ -179,16 +188,18 @@ public class BasicManagedDataSource extends BasicDataSource { /** * Creates the PoolableConnectionFactory and attaches it to the connection pool. * - * @param driverConnectionFactory JDBC connection factory created by {@link #createConnectionFactory()} - * @throws SQLException if an error occurs creating the PoolableConnectionFactory + * @param driverConnectionFactory + * JDBC connection factory created by {@link #createConnectionFactory()} + * @throws SQLException + * if an error occurs creating the PoolableConnectionFactory */ @Override - protected PoolableConnectionFactory createPoolableConnectionFactory( - final ConnectionFactory driverConnectionFactory) throws SQLException { + protected PoolableConnectionFactory createPoolableConnectionFactory(final ConnectionFactory driverConnectionFactory) + throws SQLException { PoolableConnectionFactory connectionFactory = null; try { - connectionFactory = new PoolableManagedConnectionFactory( - (XAConnectionFactory) driverConnectionFactory, getRegisteredJmxName()); + connectionFactory = new PoolableManagedConnectionFactory((XAConnectionFactory) driverConnectionFactory, + getRegisteredJmxName()); connectionFactory.setValidationQuery(getValidationQuery()); connectionFactory.setValidationQueryTimeout(getValidationQueryTimeout()); connectionFactory.setConnectionInitSql(getConnectionInitSqls()); @@ -198,8 +209,7 @@ public class BasicManagedDataSource extends BasicDataSource { connectionFactory.setDefaultCatalog(getDefaultCatalog()); connectionFactory.setCacheState(getCacheState()); connectionFactory.setPoolStatements(isPoolPreparedStatements()); - connectionFactory.setMaxOpenPreparedStatements( - getMaxOpenPreparedStatements()); + connectionFactory.setMaxOpenPreparedStatements(getMaxOpenPreparedStatements()); connectionFactory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis()); connectionFactory.setRollbackOnReturn(getRollbackOnReturn()); connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn()); http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/DataSourceXAConnectionFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/DataSourceXAConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/managed/DataSourceXAConnectionFactory.java index 6eb3f03..7e974e0 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/DataSourceXAConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/DataSourceXAConnectionFactory.java @@ -43,24 +43,30 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory { private char[] userPassword; /** - * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database - * connections. The connections are enlisted into transactions using the specified transaction manager. + * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database connections. + * The connections are enlisted into transactions using the specified transaction manager. * - * @param transactionManager the transaction manager in which connections will be enlisted - * @param xaDataSource the data source from which connections will be retrieved + * @param transactionManager + * the transaction manager in which connections will be enlisted + * @param xaDataSource + * the data source from which connections will be retrieved */ public DataSourceXAConnectionFactory(final TransactionManager transactionManager, final XADataSource xaDataSource) { this(transactionManager, xaDataSource, null, (char[]) null); } /** - * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database - * connections. The connections are enlisted into transactions using the specified transaction manager. + * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database connections. + * The connections are enlisted into transactions using the specified transaction manager. * - * @param transactionManager the transaction manager in which connections will be enlisted - * @param xaDataSource the data source from which connections will be retrieved - * @param userName the user name used for authenticating new connections or null for unauthenticated - * @param userPassword the password used for authenticating new connections + * @param transactionManager + * the transaction manager in which connections will be enlisted + * @param xaDataSource + * the data source from which connections will be retrieved + * @param userName + * the user name used for authenticating new connections or null for unauthenticated + * @param userPassword + * the password used for authenticating new connections */ public DataSourceXAConnectionFactory(final TransactionManager transactionManager, final XADataSource xaDataSource, final String userName, final char[] userPassword) { @@ -73,13 +79,17 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory { } /** - * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database - * connections. The connections are enlisted into transactions using the specified transaction manager. + * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database connections. + * The connections are enlisted into transactions using the specified transaction manager. * - * @param transactionManager the transaction manager in which connections will be enlisted - * @param xaDataSource the data source from which connections will be retrieved - * @param userName the user name used for authenticating new connections or null for unauthenticated - * @param userPassword the password used for authenticating new connections + * @param transactionManager + * the transaction manager in which connections will be enlisted + * @param xaDataSource + * the data source from which connections will be retrieved + * @param userName + * the user name used for authenticating new connections or null for unauthenticated + * @param userPassword + * the password used for authenticating new connections */ public DataSourceXAConnectionFactory(final TransactionManager transactionManager, final XADataSource xaDataSource, final String userName, final String userPassword) { @@ -88,6 +98,7 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory { /** * Gets the user name used to authenticate new connections. + * * @return the user name or null if unauthenticated connections are used */ public String getUsername() { @@ -96,7 +107,9 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory { /** * Sets the user name used to authenticate new connections. - * @param userName the user name used for authenticating the connection or null for unauthenticated + * + * @param userName + * the user name used for authenticating the connection or null for unauthenticated */ public void setUsername(final String userName) { this.userName = userName; http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java index af268bf..79fbd73 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java @@ -28,7 +28,7 @@ import java.sql.SQLException; import java.util.Objects; /** - * An implementation of XAConnectionFactory which manages non-XA connections in XA transactions. A non-XA connection + * An implementation of XAConnectionFactory which manages non-XA connections in XA transactions. A non-XA connection * commits and rolls back as part of the XA transaction, but is not recoverable since the connection does not implement * the 2-phase protocol. * @@ -39,13 +39,16 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { private final ConnectionFactory connectionFactory; /** - * Creates an LocalXAConnectionFactory which uses the specified connection factory to create database - * connections. The connections are enlisted into transactions using the specified transaction manager. + * Creates an LocalXAConnectionFactory which uses the specified connection factory to create database connections. + * The connections are enlisted into transactions using the specified transaction manager. * - * @param transactionManager the transaction manager in which connections will be enlisted - * @param connectionFactory the connection factory from which connections will be retrieved + * @param transactionManager + * the transaction manager in which connections will be enlisted + * @param connectionFactory + * the connection factory from which connections will be retrieved */ - public LocalXAConnectionFactory(final TransactionManager transactionManager, final ConnectionFactory connectionFactory) { + public LocalXAConnectionFactory(final TransactionManager transactionManager, + final ConnectionFactory connectionFactory) { Objects.requireNonNull(transactionManager, "transactionManager is null"); Objects.requireNonNull(connectionFactory, "connectionFactory is null"); this.transactionRegistry = new TransactionRegistry(transactionManager); @@ -72,16 +75,16 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { } /** - * LocalXAResource is a fake XAResource for non-XA connections. When a transaction is started - * the connection auto-commit is turned off. When the connection is committed or rolled back, - * the commit or rollback method is called on the connection and then the original auto-commit - * value is restored. + * LocalXAResource is a fake XAResource for non-XA connections. When a transaction is started the connection + * auto-commit is turned off. When the connection is committed or rolled back, the commit or rollback method is + * called on the connection and then the original auto-commit value is restored. * <p> - * The LocalXAResource also respects the connection read-only setting. If the connection is - * read-only the commit method will not be called, and the prepare method returns the XA_RDONLY. + * The LocalXAResource also respects the connection read-only setting. If the connection is read-only the commit + * method will not be called, and the prepare method returns the XA_RDONLY. * </p> - * It is assumed that the wrapper around a managed connection disables the setAutoCommit(), - * commit(), rollback() and setReadOnly() methods while a transaction is in progress. + * It is assumed that the wrapper around a managed connection disables the setAutoCommit(), commit(), rollback() and + * setReadOnly() methods while a transaction is in progress. + * * @since 2.0 */ protected static class LocalXAResource implements XAResource { @@ -103,14 +106,17 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { } /** - * Signals that a the connection has been enrolled in a transaction. This method saves off the - * current auto commit flag, and then disables auto commit. The original auto commit setting is - * restored when the transaction completes. + * Signals that a the connection has been enrolled in a transaction. This method saves off the current auto + * commit flag, and then disables auto commit. The original auto commit setting is restored when the transaction + * completes. * - * @param xid the id of the transaction branch for this connection - * @param flag either XAResource.TMNOFLAGS or XAResource.TMRESUME - * @throws XAException if the connection is already enlisted in another transaction, or if auto-commit - * could not be disabled + * @param xid + * the id of the transaction branch for this connection + * @param flag + * either XAResource.TMNOFLAGS or XAResource.TMRESUME + * @throws XAException + * if the connection is already enlisted in another transaction, or if auto-commit could not be + * disabled */ @Override public synchronized void start(final Xid xid, final int flag) throws XAException { @@ -134,13 +140,15 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { try { connection.setAutoCommit(false); } catch (final SQLException e) { - throw (XAException) new XAException("Count not turn off auto commit for a XA transaction").initCause(e); + throw (XAException) new XAException("Count not turn off auto commit for a XA transaction") + .initCause(e); } this.currentXid = xid; } else if (flag == XAResource.TMRESUME) { if (!xid.equals(this.currentXid)) { - throw new XAException("Attempting to resume in different transaction: expected " + this.currentXid + ", but was " + xid); + throw new XAException("Attempting to resume in different transaction: expected " + this.currentXid + + ", but was " + xid); } } else { throw new XAException("Unknown start flag " + flag); @@ -150,9 +158,12 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { /** * This method does nothing. * - * @param xid the id of the transaction branch for this connection - * @param flag ignored - * @throws XAException if the connection is already enlisted in another transaction + * @param xid + * the id of the transaction branch for this connection + * @param flag + * ignored + * @throws XAException + * if the connection is already enlisted in another transaction */ @Override public synchronized void end(final Xid xid, final int flag) throws XAException { @@ -162,17 +173,18 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { } // This notification tells us that the application server is done using this - // connection for the time being. The connection is still associated with an + // connection for the time being. The connection is still associated with an // open transaction, so we must still wait for the commit or rollback method } /** - * This method does nothing since the LocalXAConnection does not support two-phase-commit. This method - * will return XAResource.XA_RDONLY if the connection isReadOnly(). This assumes that the physical - * connection is wrapped with a proxy that prevents an application from changing the read-only flag - * while enrolled in a transaction. + * This method does nothing since the LocalXAConnection does not support two-phase-commit. This method will + * return XAResource.XA_RDONLY if the connection isReadOnly(). This assumes that the physical connection is + * wrapped with a proxy that prevents an application from changing the read-only flag while enrolled in a + * transaction. * - * @param xid the id of the transaction branch for this connection + * @param xid + * the id of the transaction branch for this connection * @return XAResource.XA_RDONLY if the connection.isReadOnly(); XAResource.XA_OK otherwise */ @Override @@ -199,9 +211,12 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { /** * Commits the transaction and restores the original auto commit setting. * - * @param xid the id of the transaction branch for this connection - * @param flag ignored - * @throws XAException if connection.commit() throws a SQLException + * @param xid + * the id of the transaction branch for this connection + * @param flag + * ignored + * @throws XAException + * if connection.commit() throws a SQLException */ @Override public synchronized void commit(final Xid xid, final boolean flag) throws XAException { @@ -210,8 +225,7 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { throw new XAException("There is no current transaction"); } if (!this.currentXid.equals(xid)) { - throw new XAException("Invalid Xid: expected " + - this.currentXid + ", but was " + xid); + throw new XAException("Invalid Xid: expected " + this.currentXid + ", but was " + xid); } try { @@ -239,8 +253,10 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { /** * Rolls back the transaction and restores the original auto commit setting. * - * @param xid the id of the transaction branch for this connection - * @throws XAException if connection.rollback() throws a SQLException + * @param xid + * the id of the transaction branch for this connection + * @throws XAException + * if connection.rollback() throws a SQLException */ @Override public synchronized void rollback(final Xid xid) throws XAException { @@ -265,7 +281,8 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { /** * Returns true if the specified XAResource == this XAResource. * - * @param xaResource the XAResource to test + * @param xaResource + * the XAResource to test * @return true if the specified XAResource == this XAResource; false otherwise */ @Override @@ -276,7 +293,8 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { /** * Clears the currently associated transaction if it is the specified xid. * - * @param xid the id of the transaction to forget + * @param xid + * the id of the transaction to forget */ @Override public synchronized void forget(final Xid xid) { @@ -286,9 +304,11 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { } /** - * Always returns a zero length Xid array. The LocalXAConnectionFactory can not support recovery, so no xids will ever be found. + * Always returns a zero length Xid array. The LocalXAConnectionFactory can not support recovery, so no xids + * will ever be found. * - * @param flag ignored since recovery is not supported + * @param flag + * ignored since recovery is not supported * @return always a zero length Xid array. */ @Override @@ -309,7 +329,8 @@ public class LocalXAConnectionFactory implements XAConnectionFactory { /** * Always returns false since we have no way to set a transaction timeout on a JDBC connection. * - * @param transactionTimeout ignored since we have no way to set a transaction timeout on a JDBC connection + * @param transactionTimeout + * ignored since we have no way to set a transaction timeout on a JDBC connection * @return always false */ @Override http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java index fa8f577..3d0289f 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java @@ -52,8 +52,7 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio private boolean isSharedConnection; private final Lock lock; - public ManagedConnection(final ObjectPool<C> pool, - final TransactionRegistry transactionRegistry, + public ManagedConnection(final ObjectPool<C> pool, final TransactionRegistry transactionRegistry, final boolean accessToUnderlyingConnectionAllowed) throws SQLException { super(null); this.pool = pool; @@ -80,7 +79,7 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio } // transaction should have been cleared up by TransactionContextListener, but in // rare cases another lister could have registered which uses the connection before - // our listener is called. In that rare case, trigger the transaction complete call now + // our listener is called. In that rare case, trigger the transaction complete call now transactionComplete(); } @@ -115,8 +114,7 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio // always be of type C since it has been shared by another // connection from the same pool. @SuppressWarnings("unchecked") - final - C shared = (C) transactionContext.getSharedConnection(); + final C shared = (C) transactionContext.getSharedConnection(); setDelegate(shared); // remember that we are using a shared connection so it can be cleared after the @@ -183,8 +181,8 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio } /** - * Delegates to {@link ManagedConnection#transactionComplete()} - * for transaction completion events. + * Delegates to {@link ManagedConnection#transactionComplete()} for transaction completion events. + * * @since 2.0 */ protected class CompletionListener implements TransactionContextListener { @@ -238,7 +236,6 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio super.setAutoCommit(autoCommit); } - @Override public void commit() throws SQLException { if (transactionContext != null) { @@ -255,7 +252,6 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio super.rollback(); } - @Override public void setReadOnly(final boolean readOnly) throws SQLException { if (transactionContext != null) { @@ -270,6 +266,7 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio /** * If false, getDelegate() and getInnermostDelegate() will return null. + * * @return if false, getDelegate() and getInnermostDelegate() will return null */ public boolean isAccessToUnderlyingConnectionAllowed() { http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java b/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java index 1c31fdd..a73db72 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java @@ -27,39 +27,40 @@ import java.util.Objects; /** * The ManagedDataSource is a PoolingDataSource that creates ManagedConnections. * - * @param <C> The kind of {@link Connection} to manage. + * @param <C> + * The kind of {@link Connection} to manage. * @since 2.0 */ public class ManagedDataSource<C extends Connection> extends PoolingDataSource<C> { private TransactionRegistry transactionRegistry; /** - * Creates a ManagedDataSource which obtains connections from the specified pool and - * manages them using the specified transaction registry. The TransactionRegistry must - * be the transaction registry obtained from the XAConnectionFactory used to create - * the connection pool. If not, an error will occur when attempting to use the connection - * in a global transaction because the XAResource object associated with the connection - * will be unavailable. + * Creates a ManagedDataSource which obtains connections from the specified pool and manages them using the + * specified transaction registry. The TransactionRegistry must be the transaction registry obtained from the + * XAConnectionFactory used to create the connection pool. If not, an error will occur when attempting to use the + * connection in a global transaction because the XAResource object associated with the connection will be + * unavailable. * - * @param pool the connection pool - * @param transactionRegistry the transaction registry obtained from the - * XAConnectionFactory used to create the connection pool object factory + * @param pool + * the connection pool + * @param transactionRegistry + * the transaction registry obtained from the XAConnectionFactory used to create the connection pool + * object factory */ - public ManagedDataSource(final ObjectPool<C> pool, - final TransactionRegistry transactionRegistry) { + public ManagedDataSource(final ObjectPool<C> pool, final TransactionRegistry transactionRegistry) { super(pool); this.transactionRegistry = transactionRegistry; } /** - * Sets the transaction registry from the XAConnectionFactory used to create the pool. - * The transaction registry can only be set once using either a connector or this setter - * method. - * @param transactionRegistry the transaction registry acquired from the XAConnectionFactory - * used to create the pool + * Sets the transaction registry from the XAConnectionFactory used to create the pool. The transaction registry can + * only be set once using either a connector or this setter method. + * + * @param transactionRegistry + * the transaction registry acquired from the XAConnectionFactory used to create the pool */ public void setTransactionRegistry(final TransactionRegistry transactionRegistry) { - if(this.transactionRegistry != null) { + if (this.transactionRegistry != null) { throw new IllegalStateException("TransactionRegistry already set"); } Objects.requireNonNull(transactionRegistry, "transactionRegistry is null"); @@ -76,7 +77,8 @@ public class ManagedDataSource<C extends Connection> extends PoolingDataSource<C throw new IllegalStateException("TransactionRegistry has not been set"); } - final Connection connection = new ManagedConnection<>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed()); + final Connection connection = new ManagedConnection<>(getPool(), transactionRegistry, + isAccessToUnderlyingConnectionAllowed()); return connection; } } http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnection.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnection.java b/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnection.java index c35c8b1..7eba6e7 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnection.java @@ -33,39 +33,43 @@ import org.apache.commons.pool2.ObjectPool; public class PoolableManagedConnection extends PoolableConnection { private final TransactionRegistry transactionRegistry; - /** * Create a PoolableManagedConnection. * - * @param transactionRegistry transaction registry - * @param conn underlying connection - * @param pool connection pool + * @param transactionRegistry + * transaction registry + * @param conn + * underlying connection + * @param pool + * connection pool */ - public PoolableManagedConnection(final TransactionRegistry transactionRegistry, - final Connection conn, final ObjectPool<PoolableConnection> pool) { + public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, + final ObjectPool<PoolableConnection> pool) { this(transactionRegistry, conn, pool, null, false); } - /** * Create a PoolableManagedConnection. * - * @param transactionRegistry transaction registry - * @param conn underlying connection - * @param pool connection pool - * @param disconnectSqlCodes SQL_STATE codes considered fatal disconnection errors - * @param fastFailValidation true means fatal disconnection errors cause subsequent - * validations to fail immediately (no attempt to run query or isValid) + * @param transactionRegistry + * transaction registry + * @param conn + * underlying connection + * @param pool + * connection pool + * @param disconnectSqlCodes + * SQL_STATE codes considered fatal disconnection errors + * @param fastFailValidation + * true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to + * run query or isValid) */ - public PoolableManagedConnection(final TransactionRegistry transactionRegistry, - final Connection conn, final ObjectPool<PoolableConnection> pool, - final Collection<String> disconnectSqlCodes, + public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, + final ObjectPool<PoolableConnection> pool, final Collection<String> disconnectSqlCodes, final boolean fastFailValidation) { super(conn, pool, null, disconnectSqlCodes, fastFailValidation); this.transactionRegistry = transactionRegistry; } - /** * Actually close the underlying connection. */ http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java index 69e1842..6b23804 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java @@ -16,6 +16,7 @@ */ package org.apache.commons.dbcp2.managed; + import java.sql.Connection; import javax.management.ObjectName; http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/TransactionContext.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/TransactionContext.java b/src/main/java/org/apache/commons/dbcp2/managed/TransactionContext.java index 00a0121..dfb6ebd 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/TransactionContext.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/TransactionContext.java @@ -29,10 +29,9 @@ import java.util.Objects; import java.lang.ref.WeakReference; /** - * TransactionContext represents the association between a single XAConnectionFactory and a Transaction. - * This context contains a single shared connection which should be used by all ManagedConnections for - * the XAConnectionFactory, the ability to listen for the transaction completion event, and a method - * to check the status of the transaction. + * TransactionContext represents the association between a single XAConnectionFactory and a Transaction. This context + * contains a single shared connection which should be used by all ManagedConnections for the XAConnectionFactory, the + * ability to listen for the transaction completion event, and a method to check the status of the transaction. * * @since 2.0 */ @@ -43,13 +42,13 @@ public class TransactionContext { private boolean transactionComplete; /** - * Creates a TransactionContext for the specified Transaction and TransactionRegistry. The - * TransactionRegistry is used to obtain the XAResource for the shared connection when it is - * enlisted in the transaction. + * Creates a TransactionContext for the specified Transaction and TransactionRegistry. The TransactionRegistry is + * used to obtain the XAResource for the shared connection when it is enlisted in the transaction. * - * @param transactionRegistry the TransactionRegistry used to obtain the XAResource for the - * shared connection - * @param transaction the transaction + * @param transactionRegistry + * the TransactionRegistry used to obtain the XAResource for the shared connection + * @param transaction + * the transaction */ public TransactionContext(final TransactionRegistry transactionRegistry, final Transaction transaction) { Objects.requireNonNull(transactionRegistry, "transactionRegistry is null"); @@ -60,9 +59,9 @@ public class TransactionContext { } /** - * Gets the connection shared by all ManagedConnections in the transaction. Specifically, - * connection using the same XAConnectionFactory from which the TransactionRegistry was - * obtained. + * Gets the connection shared by all ManagedConnections in the transaction. Specifically, connection using the same + * XAConnectionFactory from which the TransactionRegistry was obtained. + * * @return the shared connection for this transaction */ public Connection getSharedConnection() { @@ -70,13 +69,13 @@ public class TransactionContext { } /** - * Sets the shared connection for this transaction. The shared connection is enlisted - * in the transaction. + * Sets the shared connection for this transaction. The shared connection is enlisted in the transaction. * - * @param sharedConnection the shared connection - * @throws SQLException if a shared connection is already set, if XAResource for the connection - * could not be found in the transaction registry, or if there was a problem enlisting the - * connection in the transaction + * @param sharedConnection + * the shared connection + * @throws SQLException + * if a shared connection is already set, if XAResource for the connection could not be found in the + * transaction registry, or if there was a problem enlisting the connection in the transaction */ public void setSharedConnection(final Connection sharedConnection) throws SQLException { if (this.sharedConnection != null) { @@ -88,7 +87,7 @@ public class TransactionContext { final Transaction transaction = getTransaction(); try { final XAResource xaResource = transactionRegistry.getXAResource(sharedConnection); - if ( !transaction.enlistResource(xaResource) ) { + if (!transaction.enlistResource(xaResource)) { throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'."); } } catch (final IllegalStateException e) { @@ -106,8 +105,10 @@ public class TransactionContext { /** * Adds a listener for transaction completion events. * - * @param listener the listener to add - * @throws SQLException if a problem occurs adding the listener to the transaction + * @param listener + * the listener to add + * @throws SQLException + * if a problem occurs adding the listener to the transaction */ public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException { try { @@ -132,8 +133,10 @@ public class TransactionContext { /** * True if the transaction is active or marked for rollback only. + * * @return true if the transaction is active or marked for rollback only; false otherwise - * @throws SQLException if a problem occurs obtaining the transaction status + * @throws SQLException + * if a problem occurs obtaining the transaction status */ public boolean isActive() throws SQLException { try { @@ -166,7 +169,7 @@ public class TransactionContext { } /** - * Gets the transaction complete flag to true. + * Gets the transaction complete flag to true. * * @return The transaction complete flag. * http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/TransactionContextListener.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/TransactionContextListener.java b/src/main/java/org/apache/commons/dbcp2/managed/TransactionContextListener.java index 8637097..8506d28 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/TransactionContextListener.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/TransactionContextListener.java @@ -25,8 +25,11 @@ package org.apache.commons.dbcp2.managed; public interface TransactionContextListener { /** * Occurs after the transaction commits or rolls back. - * @param transactionContext the transaction context that completed - * @param commited true if the transaction committed; false otherwise + * + * @param transactionContext + * the transaction context that completed + * @param commited + * true if the transaction committed; false otherwise */ void afterCompletion(TransactionContext transactionContext, boolean commited); } http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/TransactionRegistry.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/TransactionRegistry.java b/src/main/java/org/apache/commons/dbcp2/managed/TransactionRegistry.java index 7ab3461..7715497 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/TransactionRegistry.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/TransactionRegistry.java @@ -30,36 +30,38 @@ import javax.transaction.xa.XAResource; import org.apache.commons.dbcp2.DelegatingConnection; - /** * TransactionRegistry tracks Connections and XAResources in a transacted environment for a single XAConnectionFactory. * <p> * The TransactionRegistry hides the details of transaction processing from the existing DBCP pooling code, and gives * the ManagedConnection a way to enlist connections in a transaction, allowing for the maximal rescue of DBCP. * </p> + * * @since 2.0 */ public class TransactionRegistry { private final TransactionManager transactionManager; - private final Map<Transaction, TransactionContext> caches = - new WeakHashMap<>(); + private final Map<Transaction, TransactionContext> caches = new WeakHashMap<>(); private final Map<Connection, XAResource> xaResources = new WeakHashMap<>(); /** * Creates a TransactionRegistry for the specified transaction manager. - * @param transactionManager the transaction manager used to enlist connections + * + * @param transactionManager + * the transaction manager used to enlist connections */ public TransactionRegistry(final TransactionManager transactionManager) { this.transactionManager = transactionManager; } /** - * Registers the association between a Connection and a XAResource. When a connection - * is enlisted in a transaction, it is actually the XAResource that is given to the transaction - * manager. + * Registers the association between a Connection and a XAResource. When a connection is enlisted in a transaction, + * it is actually the XAResource that is given to the transaction manager. * - * @param connection the JDBC connection - * @param xaResource the XAResource which managed the connection within a transaction + * @param connection + * the JDBC connection + * @param xaResource + * the XAResource which managed the connection within a transaction */ public synchronized void registerConnection(final Connection connection, final XAResource xaResource) { Objects.requireNonNull(connection, "connection is null"); @@ -69,9 +71,12 @@ public class TransactionRegistry { /** * Gets the XAResource registered for the connection. - * @param connection the connection + * + * @param connection + * the connection * @return the XAResource registered for the connection; never null - * @throws SQLException if the connection does not have a registered XAResource + * @throws SQLException + * if the connection does not have a registered XAResource */ public synchronized XAResource getXAResource(final Connection connection) throws SQLException { Objects.requireNonNull(connection, "connection is null"); @@ -85,8 +90,10 @@ public class TransactionRegistry { /** * Gets the active TransactionContext or null if not Transaction is active. + * * @return the active TransactionContext or null if no Transaction is active - * @throws SQLException if an error occurs while fetching the transaction + * @throws SQLException + * if an error occurs while fetching the transaction */ public TransactionContext getActiveTransactionContext() throws SQLException { Transaction transaction = null; @@ -117,6 +124,7 @@ public class TransactionRegistry { /** * Unregisters a destroyed connection from {@link TransactionRegistry} + * * @param connection */ public synchronized void unregisterConnection(final Connection connection) { @@ -124,7 +132,6 @@ public class TransactionRegistry { xaResources.remove(key); } - private Connection getConnectionKey(final Connection connection) { Connection result; if (connection instanceof DelegatingConnection) { @@ -135,4 +142,3 @@ public class TransactionRegistry { return result; } } - http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/0a928ca2/src/main/java/org/apache/commons/dbcp2/managed/XAConnectionFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/managed/XAConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/managed/XAConnectionFactory.java index 39ca074..09d4af9 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/XAConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/XAConnectionFactory.java @@ -23,19 +23,17 @@ import java.sql.SQLException; import org.apache.commons.dbcp2.ConnectionFactory; /** - * XAConnectionFactory is an extension of ConnectionFactory used to create connections - * in a transaction managed environment. The XAConnectionFactory operates like a normal - * ConnectionFactory except a TransactionRegistry is provided from which the XAResource - * for a connection can be obtained. This allows the existing DBCP pool code to work with - * XAConnections and gives a the ManagedConnection a way to enlist a connection in the - * the transaction. + * XAConnectionFactory is an extension of ConnectionFactory used to create connections in a transaction managed + * environment. The XAConnectionFactory operates like a normal ConnectionFactory except a TransactionRegistry is + * provided from which the XAResource for a connection can be obtained. This allows the existing DBCP pool code to work + * with XAConnections and gives a the ManagedConnection a way to enlist a connection in the the transaction. * * @since 2.0 */ public interface XAConnectionFactory extends ConnectionFactory { /** - * Gets the TransactionRegistry for this connection factory which contains a the - * XAResource for every connection created by this factory. + * Gets the TransactionRegistry for this connection factory which contains a the XAResource for every connection + * created by this factory. * * @return the transaction registry for this connection factory */ @@ -44,13 +42,13 @@ public interface XAConnectionFactory extends ConnectionFactory { /** * Create a new {@link java.sql.Connection} in an implementation specific fashion. * <p> - * An implementation can assume that the caller of this will wrap the connection in - * a proxy that protects access to the setAutoCommit, commit and rollback when - * enrolled in a XA transaction. + * An implementation can assume that the caller of this will wrap the connection in a proxy that protects access to + * the setAutoCommit, commit and rollback when enrolled in a XA transaction. * </p> * * @return a new {@link java.sql.Connection} - * @throws java.sql.SQLException if a database error occurs creating the connection + * @throws java.sql.SQLException + * if a database error occurs creating the connection */ @Override Connection createConnection() throws SQLException;