Package jodd.db.jtx

Source Code of jodd.db.jtx.DbJtxSessionProvider

// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.db.jtx;

import jodd.db.DbSessionProvider;
import jodd.db.DbSession;
import jodd.db.DbSqlException;
import jodd.jtx.JtxTransactionManager;
import jodd.log.Logger;
import jodd.log.LoggerFactory;

/**
* Returns session from the db {@link JtxTransactionManager transaction manager},
* like {@link DbJtxTransactionManager}.
*/
public class DbJtxSessionProvider implements DbSessionProvider {

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

  protected final JtxTransactionManager jtxTxManager;

  /**
   * Creates new JTX session provider.
   */
  public DbJtxSessionProvider(JtxTransactionManager txManager) {
    this.jtxTxManager = txManager;
  }

  /**
   * Returns session from JTX transaction manager and started transaction.
   */
  public DbSession getDbSession() {
    log.debug("Requesting db TX manager session");

    DbJtxTransaction jtx = (DbJtxTransaction) jtxTxManager.getTransaction();

    if (jtx == null) {
      throw new DbSqlException(
          "No transaction is in progress and DbSession can't be provided. " +
          "It seems that transaction manager is not used to begin a transaction.");
    }

    return jtx.requestResource();
  }

}
TOP

Related Classes of jodd.db.jtx.DbJtxSessionProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.