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

org.activiti.engine.impl.cfg.standalone.StandaloneMybatisTransactionContext Maven / Gradle / Ivy

There is a newer version: 7.1.0.M6
Show newest version
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti.engine.impl.cfg.standalone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.impl.cfg.TransactionContext;
import org.activiti.engine.impl.cfg.TransactionListener;
import org.activiti.engine.impl.cfg.TransactionState;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Tom Baeyens
 */
public class StandaloneMybatisTransactionContext implements TransactionContext {

  private static Logger log = LoggerFactory.getLogger(StandaloneMybatisTransactionContext.class);

  protected CommandContext commandContext;
  protected Map> stateTransactionListeners;

  public StandaloneMybatisTransactionContext(CommandContext commandContext) {
    this.commandContext = commandContext;
  }

  public void addTransactionListener(TransactionState transactionState, TransactionListener transactionListener) {
    if (stateTransactionListeners == null) {
      stateTransactionListeners = new HashMap>();
    }
    List transactionListeners = stateTransactionListeners.get(transactionState);
    if (transactionListeners == null) {
      transactionListeners = new ArrayList();
      stateTransactionListeners.put(transactionState, transactionListeners);
    }
    transactionListeners.add(transactionListener);
  }

  public void commit() {
    log.debug("firing event committing...");
    fireTransactionEvent(TransactionState.COMMITTING);
    log.debug("committing the ibatis sql session...");
    getDbSqlSession().commit();
    log.debug("firing event committed...");
    fireTransactionEvent(TransactionState.COMMITTED);
  }

  protected void fireTransactionEvent(TransactionState transactionState) {
    if (stateTransactionListeners == null) {
      return;
    }
    List transactionListeners = stateTransactionListeners.get(transactionState);
    if (transactionListeners == null) {
      return;
    }
    for (TransactionListener transactionListener : transactionListeners) {
      transactionListener.execute(commandContext);
    }
  }

  private DbSqlSession getDbSqlSession() {
    return commandContext.getDbSqlSession();
  }

  public void rollback() {
    try {
      try {
        log.debug("firing event rolling back...");
        fireTransactionEvent(TransactionState.ROLLINGBACK);

      } catch (Throwable exception) {
        log.info("Exception during transaction: {}", exception.getMessage());
        commandContext.exception(exception);
      } finally {
        log.debug("rolling back ibatis sql session...");
        getDbSqlSession().rollback();
      }

    } catch (Throwable exception) {
      log.info("Exception during transaction: {}", exception.getMessage());
      commandContext.exception(exception);

    } finally {
      log.debug("firing event rolled back...");
      fireTransactionEvent(TransactionState.ROLLED_BACK);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy