<commit_before>package br.com.caelum.vraptor.musicjungle.interceptor;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import br.com.caelum.vraptor.AroundCall;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.http.MutableResponse;
import br.com.caelum.vraptor.interceptor.SimpleInterceptorStack;

/**
 * Open EntityManager in view interceptor, that opens an EntityManager and a
 * transaction before a request, and closes at the end of request.
 */
@Intercepts
public class TransactionInterceptor {

	@Inject
	private EntityManager entityManager;

	@Inject
	private Validator validator;

	@Inject
	private MutableResponse response;

	@AroundCall
	public void intercept(SimpleInterceptorStack stack) {

		addRedirectListener();

		EntityTransaction transaction = null;
		try {
			transaction = entityManager.getTransaction();
			transaction.begin();

			stack.next();

			if (!validator.hasErrors() && transaction.isActive()) {
				transaction.commit();
			}
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
		}
	}

	/**
	 * We force the commit before the redirect, this way we can abort the
	 * redirect if a database error occurs.
	 */
	private void addRedirectListener() {
		response.addRedirectListener(new MutableResponse.RedirectListener() {
			@Override
			public void beforeRedirect() {
				if (!validator.hasErrors()
						&& entityManager.getTransaction().isActive()) {
					entityManager.getTransaction().commit();
				}
			}
		});
	}

}<commit_msg>Extracting commit operation into a method;<commit_after>package br.com.caelum.vraptor.musicjungle.interceptor;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import br.com.caelum.vraptor.AroundCall;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.http.MutableResponse;
import br.com.caelum.vraptor.interceptor.SimpleInterceptorStack;

/**
 * Open EntityManager in view interceptor, that opens an EntityManager and a
 * transaction before a request, and closes at the end of request.
 */
@Intercepts
public class TransactionInterceptor {

	@Inject
	private EntityManager entityManager;

	@Inject
	private Validator validator;

	@Inject
	private MutableResponse response;

	@AroundCall
	public void intercept(SimpleInterceptorStack stack) {

		addRedirectListener();

		EntityTransaction transaction = null;
		try {
			transaction = entityManager.getTransaction();
			transaction.begin();

			stack.next();

			commit(transaction);
			
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
		}
	}

	private void commit(EntityTransaction transaction) {
		if (!validator.hasErrors() && transaction.isActive()) {
			transaction.commit();
		}
	}

	/**
	 * We force the commit before the redirect, this way we can abort the
	 * redirect if a database error occurs.
	 */
	private void addRedirectListener() {
		response.addRedirectListener(new MutableResponse.RedirectListener() {
			@Override
			public void beforeRedirect() {
				commit(entityManager.getTransaction());
			}
		});
	}

}
{'fraction_non_alphanumeric': 0.10221413406126782, 'fraction_numerical': 0.0, 'mean_word_length': 4.4693200663349915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3409049', 'n_tokens_mistral': 1059, 'n_tokens_neox': 960, 'n_words': 231}