All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.cache.infinispan.util.InvocationAfterCompletion Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.cache.infinispan.util;

import java.sql.Connection;
import java.sql.SQLException;
import javax.transaction.Status;
import javax.transaction.Synchronization;

import org.hibernate.HibernateException;
import org.hibernate.jdbc.WorkExecutor;
import org.hibernate.jdbc.WorkExecutorVisitable;
import org.hibernate.resource.transaction.spi.TransactionCoordinator;

import org.infinispan.AdvancedCache;

/**
 * @author Radim Vansa <[email protected]>
 */
public abstract class InvocationAfterCompletion implements Synchronization {
	protected static final InfinispanMessageLogger log = InfinispanMessageLogger.Provider.getLog( InvocationAfterCompletion.class );

	protected final TransactionCoordinator tc;
	protected final AdvancedCache cache;
	protected final boolean requiresTransaction;

	public InvocationAfterCompletion(TransactionCoordinator tc, AdvancedCache cache, boolean requiresTransaction) {
		this.tc = tc;
		this.cache = cache;
		this.requiresTransaction = requiresTransaction;
	}

	@Override
	public void beforeCompletion() {
	}

	@Override
	public void afterCompletion(int status) {
		switch (status) {
			case Status.STATUS_COMMITTING:
			case Status.STATUS_COMMITTED:
				invokeIsolated(true);
				break;
			default:
				// it would be nicer to react only on ROLLING_BACK and ROLLED_BACK statuses
				// but TransactionCoordinator gives us UNKNOWN on rollback
				invokeIsolated(false);
				break;
		}
	}

	protected void invokeIsolated(final boolean success) {
		try {
			// TODO: isolation without obtaining Connection -> needs HHH-9993
			tc.createIsolationDelegate().delegateWork(new WorkExecutorVisitable() {
				@Override
				public Void accept(WorkExecutor executor, Connection connection) throws SQLException {
					invoke(success, cache);
					return null;
				}
			}, requiresTransaction);
		}
		catch (HibernateException e) {
			// silently fail any exceptions
			if (log.isTraceEnabled()) {
				log.trace("Exception during query cache update", e);
			}
		}
	}

	protected abstract void invoke(boolean success, AdvancedCache cache);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy