Search in sources :

Example 1 with ManagedConnectionInfo

use of org.apache.geronimo.connector.outbound.ManagedConnectionInfo in project tomee by apache.

the class AutoConnectionTracker method handleReleased.

/**
 * Removes the released collection from the garbage collection reference tracker, since this
 * connection is being release via a normal close method.
 *
 * @param interceptor    ignored
 * @param connectionInfo the connection that was released
 * @param action         ignored
 */
@Override
@SuppressWarnings("unchecked")
public void handleReleased(final ConnectionTrackingInterceptor interceptor, final ConnectionInfo connectionInfo, final ConnectionReturnAction action) {
    TransactionImpl currentTx = null;
    try {
        currentTx = (TransactionImpl) txMgr.getTransaction();
    } catch (SystemException | ClassCastException e) {
    // ignore
    }
    if (currentTx != null) {
        Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>> txConnections = (Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>>) currentTx.getResource(KEY);
        if (txConnections == null) {
            txConnections = new HashMap<>();
            registry.putResource(KEY, txConnections);
        }
        Map<ConnectionInfo, Object> connectionObjects = txConnections.computeIfAbsent(connectionInfo.getManagedConnectionInfo(), k -> new HashMap<>());
        connectionObjects.remove(connectionInfo);
        if (connectionObjects.size() == 0) {
            txConnections.remove(connectionInfo.getManagedConnectionInfo());
        }
    }
    @SuppressWarnings("rawtypes") final PhantomReference phantomReference = references.remove(connectionInfo.getManagedConnectionInfo());
    if (phantomReference != null) {
        phantomReference.clear();
    }
}
Also used : ManagedConnectionInfo(org.apache.geronimo.connector.outbound.ManagedConnectionInfo) TransactionImpl(org.apache.geronimo.transaction.manager.TransactionImpl) SystemException(javax.transaction.SystemException) PhantomReference(java.lang.ref.PhantomReference) ManagedConnectionInfo(org.apache.geronimo.connector.outbound.ManagedConnectionInfo) ConnectionInfo(org.apache.geronimo.connector.outbound.ConnectionInfo) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ManagedConnectionInfo

use of org.apache.geronimo.connector.outbound.ManagedConnectionInfo in project tomee by apache.

the class AutoConnectionTracker method handleObtained.

/**
 * Proxies new connection handles so we can detect when they have been garbage collected.
 *
 * @param interceptor    the interceptor used to release the managed connection when the handled is garbage collected.
 * @param connectionInfo the connection that was obtained
 * @param reassociate    should always be false
 */
@SuppressWarnings("unchecked")
@Override
public void handleObtained(final ConnectionTrackingInterceptor interceptor, final ConnectionInfo connectionInfo, final boolean reassociate) throws ResourceException {
    if (txMgr != null && registry != null) {
        try {
            final TransactionImpl currentTx = (TransactionImpl) txMgr.getTransaction();
            if (currentTx != null) {
                Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>> txConnections = (Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>>) registry.getResource(KEY);
                if (txConnections == null) {
                    txConnections = new HashMap<ManagedConnectionInfo, Map<ConnectionInfo, Object>>();
                    registry.putResource(KEY, txConnections);
                }
                Map<ConnectionInfo, Object> connectionObjects = txConnections.get(connectionInfo.getManagedConnectionInfo());
                if (connectionObjects == null) {
                    connectionObjects = new HashMap<ConnectionInfo, Object>();
                    txConnections.put(connectionInfo.getManagedConnectionInfo(), connectionObjects);
                }
                connectionObjects.put(connectionInfo, connectionInfo.getConnectionProxy());
                registry.registerInterposedSynchronization(new Synchronization() {

                    @Override
                    public void beforeCompletion() {
                    }

                    @Override
                    public void afterCompletion(final int status) {
                        final Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>> txConnections = (Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>>) currentTx.getResource(KEY);
                        if (txConnections != null && txConnections.size() > 0) {
                            for (final ManagedConnectionInfo managedConnectionInfo : txConnections.keySet()) {
                                final StringBuilder sb = new StringBuilder();
                                final Collection<ConnectionInfo> connectionInfos = txConnections.get(managedConnectionInfo).keySet();
                                for (final ConnectionInfo connectionInfo : connectionInfos) {
                                    sb.append("\n  ").append("Connection handle opened at ").append(stackTraceToString(connectionInfo.getTrace().getStackTrace()));
                                }
                                logger.warning("Transaction complete, but connection still has handles associated: " + managedConnectionInfo + "\nAbandoned connection information: " + sb.toString());
                            }
                        }
                    }
                });
            }
        } catch (SystemException | ClassCastException e) {
        // ignore
        }
    }
    if (!reassociate) {
        proxyConnection(interceptor, connectionInfo);
    }
}
Also used : ManagedConnectionInfo(org.apache.geronimo.connector.outbound.ManagedConnectionInfo) TransactionImpl(org.apache.geronimo.transaction.manager.TransactionImpl) Synchronization(javax.transaction.Synchronization) SystemException(javax.transaction.SystemException) Collection(java.util.Collection) ManagedConnectionInfo(org.apache.geronimo.connector.outbound.ManagedConnectionInfo) ConnectionInfo(org.apache.geronimo.connector.outbound.ConnectionInfo) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 SystemException (javax.transaction.SystemException)2 ConnectionInfo (org.apache.geronimo.connector.outbound.ConnectionInfo)2 ManagedConnectionInfo (org.apache.geronimo.connector.outbound.ManagedConnectionInfo)2 TransactionImpl (org.apache.geronimo.transaction.manager.TransactionImpl)2 PhantomReference (java.lang.ref.PhantomReference)1 Collection (java.util.Collection)1 Synchronization (javax.transaction.Synchronization)1