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

org.hibernate.search.batchindexing.impl.OptionallyWrapInJTATransaction Maven / Gradle / Ivy

/*
 * Hibernate Search, full-text search for your domain model
 *
 * 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.search.batchindexing.impl;

import javax.transaction.SystemException;

import org.hibernate.Session;
import org.hibernate.StatelessSession;

import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;

/**
 * Wraps the execution of a {@code Runnable} in a JTA Transaction if necessary:
 * 
    *
  • if the existing Hibernate Core transaction strategy requires a TransactionManager
  • *
  • if no JTA transaction is already started
  • *
* * Unfortunately at this time we need to have access to {@code SessionFactoryImplementor}. * * @author Emmanuel Bernard */ public class OptionallyWrapInJTATransaction extends ErrorHandledRunnable { private static final Log log = LoggerFactory.make(); private final SessionAwareRunnable sessionAwareRunnable; private final StatelessSessionAwareRunnable statelessSessionAwareRunnable; private final BatchTransactionalContext batchContext; private final boolean wrapInTransaction; public OptionallyWrapInJTATransaction(BatchTransactionalContext batchContext, SessionAwareRunnable sessionAwareRunnable) { super( batchContext.extendedIntegrator ); /* * Unfortunately we need to access SessionFactoryImplementor to detect: * - whether or not we need to start the JTA transaction * - start it */ this.batchContext = batchContext; this.sessionAwareRunnable = sessionAwareRunnable; this.statelessSessionAwareRunnable = null; this.wrapInTransaction = batchContext.wrapInTransaction(); } public OptionallyWrapInJTATransaction(BatchTransactionalContext batchContext, StatelessSessionAwareRunnable statelessSessionAwareRunnable) { super( batchContext.extendedIntegrator ); /* * Unfortunately we need to access SessionFactoryImplementor to detect: * - whether or not we need to start the JTA transaction * - start it */ this.batchContext = batchContext; this.sessionAwareRunnable = null; this.statelessSessionAwareRunnable = statelessSessionAwareRunnable; this.wrapInTransaction = batchContext.wrapInTransaction(); } @Override public void runWithErrorHandler() throws Exception { if ( wrapInTransaction ) { final Session session; final StatelessSession statelessSession; if ( sessionAwareRunnable != null ) { session = batchContext.factory.openSession(); statelessSession = null; } else { session = null; statelessSession = batchContext.factory.openStatelessSession(); } batchContext.transactionManager.begin(); if ( sessionAwareRunnable != null ) { sessionAwareRunnable.run( session ); } else { statelessSessionAwareRunnable.run( statelessSession ); } batchContext.transactionManager.commit(); if ( sessionAwareRunnable != null ) { session.close(); } else { statelessSession.close(); } } else { if ( sessionAwareRunnable != null ) { sessionAwareRunnable.run( null ); } else { statelessSessionAwareRunnable.run( null ); } } } @Override protected void cleanUpOnError() { if ( wrapInTransaction ) { try { batchContext.transactionManager.rollback(); } catch (SystemException e) { log.errorRollingBackTransaction( e.getMessage(), e ); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy