Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/transaction/TransactionManagerLookupFactory.java
//$Id: TransactionManagerLookupFactory.java 11406 2007-04-15 12:52:46Z [email protected] $
package org.hibernate.transaction;

import java.util.Properties;

import javax.transaction.TransactionManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.util.ReflectHelper;

/**
 @author Gavin King
 */
public final class TransactionManagerLookupFactory {

  private static final Log log = LogFactory.getLog(TransactionManagerLookupFactory.class);

  private TransactionManagerLookupFactory() {}

  public static final TransactionManager getTransactionManager(Properties propsthrows HibernateException {
    log.info("obtaining TransactionManager");
    return getTransactionManagerLookup(props).getTransactionManager(props);
  }

  public static final TransactionManagerLookup getTransactionManagerLookup(Properties propsthrows HibernateException {

    String tmLookupClass = props.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY);
    if (tmLookupClass==null) {
      log.info("No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)");
      return null;
    }
    else {

      log.info("instantiating TransactionManagerLookup: " + tmLookupClass);

      try {
        TransactionManagerLookup lookup = (TransactionManagerLookupReflectHelper.classForName(tmLookupClass).newInstance();
        log.info("instantiated TransactionManagerLookup");
        return lookup;
      }
      catch (Exception e) {
        log.error("Could not instantiate TransactionManagerLookup", e);
        throw new HibernateException("Could not instantiate TransactionManagerLookup '" + tmLookupClass + "'");
      }
    }
  }
}