<commit_before>package org.neo4j.kernel.impl.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.transaction.Status;
import javax.transaction.Synchronization;

import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.event.TransactionEventHandler;

public class TransactionEventsSyncHook<T> implements Synchronization
{
    private final Collection<TransactionEventHandler<T>> handlers;
    private final NodeManager nodeManager;
    private final Transaction transaction;

    /**
     * This is null at construction time, then populated in beforeCompletion and
     * used in afterCompletion.
     */
    private List<HandlerAndState> states;
    private TransactionData transactionData;

    public TransactionEventsSyncHook(
            NodeManager nodeManager, Transaction transaction,
            Collection<TransactionEventHandler<T>> transactionEventHandlers )
    {
        this.nodeManager = nodeManager;
        this.transaction = transaction;
        this.handlers = transactionEventHandlers;
    }

    public void beforeCompletion()
    {
        TransactionData data = null;
        data = nodeManager.getTransactionData();
        states = new ArrayList<HandlerAndState>();
        for ( TransactionEventHandler<T> handler : this.handlers )
        {
            try
            {
                T state = handler.beforeCommit( data );
                states.add( new HandlerAndState( handler, state ) );
            }
            catch ( Throwable t )
            {
                // TODO Do something more than calling failure and
                // throw exception?
                transaction.failure();
                
                // This will cause the transaction to throw a
                // TransactionFailureException
                throw new RuntimeException( t );
            }
        }
    }

    public void afterCompletion( int status )
    {
        if ( status == Status.STATUS_COMMITTED )
        {
            for ( HandlerAndState state : this.states )
            {
                state.handler.afterCommit( this.transactionData, state.state );
            }
        }
        else if ( status == Status.STATUS_ROLLEDBACK )
        {
            for ( HandlerAndState state : this.states )
            {
                state.handler.afterRollback( this.transactionData, state.state );
            }
        }
        else
        {
            throw new RuntimeException( "Unknown status " + status );
        }
    }

    private class HandlerAndState
    {
        private final TransactionEventHandler<T> handler;
        private final T state;

        public HandlerAndState( TransactionEventHandler<T> handler, T state )
        {
            this.handler = handler;
            this.state = state;
        }
    }
}
<commit_msg>Fixed a bug where the afterCommit wouldn't get any TransactionData<commit_after>package org.neo4j.kernel.impl.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.transaction.Status;
import javax.transaction.Synchronization;

import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.event.TransactionEventHandler;

public class TransactionEventsSyncHook<T> implements Synchronization
{
    private final Collection<TransactionEventHandler<T>> handlers;
    private final NodeManager nodeManager;
    private final Transaction transaction;

    /**
     * This is null at construction time, then populated in beforeCompletion and
     * used in afterCompletion.
     */
    private List<HandlerAndState> states;
    private TransactionData transactionData;

    public TransactionEventsSyncHook(
            NodeManager nodeManager, Transaction transaction,
            Collection<TransactionEventHandler<T>> transactionEventHandlers )
    {
        this.nodeManager = nodeManager;
        this.transaction = transaction;
        this.handlers = transactionEventHandlers;
    }

    public void beforeCompletion()
    {
        this.transactionData = nodeManager.getTransactionData();
        states = new ArrayList<HandlerAndState>();
        for ( TransactionEventHandler<T> handler : this.handlers )
        {
            try
            {
                T state = handler.beforeCommit( transactionData );
                states.add( new HandlerAndState( handler, state ) );
            }
            catch ( Throwable t )
            {
                // TODO Do something more than calling failure and
                // throw exception?
                transaction.failure();
                
                // This will cause the transaction to throw a
                // TransactionFailureException
                throw new RuntimeException( t );
            }
        }
    }

    public void afterCompletion( int status )
    {
        if ( status == Status.STATUS_COMMITTED )
        {
            for ( HandlerAndState state : this.states )
            {
                state.handler.afterCommit( this.transactionData, state.state );
            }
        }
        else if ( status == Status.STATUS_ROLLEDBACK )
        {
            for ( HandlerAndState state : this.states )
            {
                state.handler.afterRollback( this.transactionData, state.state );
            }
        }
        else
        {
            throw new RuntimeException( "Unknown status " + status );
        }
    }

    private class HandlerAndState
    {
        private final TransactionEventHandler<T> handler;
        private final T state;

        public HandlerAndState( TransactionEventHandler<T> handler, T state )
        {
            this.handler = handler;
            this.state = state;
        }
    }
}

{'fraction_non_alphanumeric': 0.0728898057417913, 'fraction_numerical': 0.0013752793536187038, 'mean_word_length': 2.003613835828601, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253048', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1365, 'n_words': 356}