<commit_before>package com.commafeed.backend.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.context.internal.ManagedSessionContext;

public abstract class UnitOfWork<T> {

	private SessionFactory sessionFactory;

	public UnitOfWork(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected abstract T runInSession() throws Exception;

	public T run() {
		final Session session = sessionFactory.openSession();
		if (ManagedSessionContext.hasBind(sessionFactory)) {
			throw new IllegalStateException("Already in a unit of work!");
		}
		T t = null;
		try {
			ManagedSessionContext.bind(session);
			session.beginTransaction();
			try {
				t = runInSession();
				commitTransaction(session);
			} catch (Exception e) {
				rollbackTransaction(session);
				this.<RuntimeException> rethrow(e);
			}
		} finally {
			session.close();
			ManagedSessionContext.unbind(sessionFactory);<commit_msg>units of work can now be chained<commit_after>package com.commafeed.backend.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.context.internal.ManagedSessionContext;

public abstract class UnitOfWork<T> {

	private SessionFactory sessionFactory;

	public UnitOfWork(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected abstract T runInSession() throws Exception;

	public T run() {
		boolean newSession = !ManagedSessionContext.hasBind(sessionFactory);

		final Session session = newSession ? sessionFactory.openSession() : sessionFactory.getCurrentSession();
		T t = null;
		try {
			if (newSession) {
				ManagedSessionContext.bind(session);
				session.beginTransaction();
			}
			try {
				t = runInSession();
				if (newSession) {
					commitTransaction(session);
				}
			} catch (Exception e) {
				if (newSession) {
					rollbackTransaction(session);
				}
				this.<RuntimeException> rethrow(e);
			}
		} finally {
			if (newSession) {
				session.close();
				ManagedSessionContext.unbind(sessionFactory);
			}
{'fraction_non_alphanumeric': 0.09925093632958802, 'fraction_numerical': 0.0, 'mean_word_length': 4.5942408376963355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1083489', 'n_tokens_mistral': 707, 'n_tokens_neox': 590, 'n_words': 136}