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

com.scalar.db.sql.SqlTwoPhaseCommitTransactionSessionStrategy Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql;

import com.scalar.db.sql.metadata.Metadata;
import com.scalar.db.sql.statement.Statement;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

@NotThreadSafe
public class SqlTwoPhaseCommitTransactionSessionStrategy implements SqlSessionStrategy {

  private final SqlTwoPhaseCommitTransactionManager sqlTwoPhaseCommitTransactionManager;

  @Nullable private SqlTwoPhaseCommitTransaction sqlTwoPhaseCommitTransaction;

  SqlTwoPhaseCommitTransactionSessionStrategy(
      SqlTwoPhaseCommitTransactionManager sqlTwoPhaseCommitTransactionManager) {
    this.sqlTwoPhaseCommitTransactionManager =
        Objects.requireNonNull(sqlTwoPhaseCommitTransactionManager);
  }

  @Override
  public Optional getTransactionId() {
    if (sqlTwoPhaseCommitTransaction == null) {
      return Optional.empty();
    }
    return Optional.of(sqlTwoPhaseCommitTransaction.getId());
  }

  @Override
  public String begin() {
    assert sqlTwoPhaseCommitTransaction == null;
    sqlTwoPhaseCommitTransaction = sqlTwoPhaseCommitTransactionManager.begin();
    return sqlTwoPhaseCommitTransaction.getId();
  }

  @Override
  public String start() {
    assert sqlTwoPhaseCommitTransaction == null;
    sqlTwoPhaseCommitTransaction = sqlTwoPhaseCommitTransactionManager.start();
    return sqlTwoPhaseCommitTransaction.getId();
  }

  @Override
  public void join(String transactionId) {
    assert sqlTwoPhaseCommitTransaction == null;
    sqlTwoPhaseCommitTransaction = sqlTwoPhaseCommitTransactionManager.join(transactionId);
  }

  @Override
  public void suspend() {
    sqlTwoPhaseCommitTransaction = null;
  }

  @Override
  public void resume(String transactionId) {
    assert sqlTwoPhaseCommitTransaction == null;
    sqlTwoPhaseCommitTransaction = sqlTwoPhaseCommitTransactionManager.resume(transactionId);
  }

  @Override
  public ResultSet execute(
      String sql, @Nullable List positionalValues, @Nullable String defaultNamespaceName) {
    return getSqlStatementExecutable().execute(sql, positionalValues, defaultNamespaceName);
  }

  @Override
  public ResultSet execute(
      String sql, @Nullable Map namedValues, @Nullable String defaultNamespaceName) {
    return getSqlStatementExecutable().execute(sql, namedValues, defaultNamespaceName);
  }

  @Override
  public ResultSet execute(
      Statement statement,
      @Nullable List positionalValues,
      @Nullable String defaultNamespaceName) {
    return getSqlStatementExecutable().execute(statement, positionalValues, defaultNamespaceName);
  }

  @Override
  public ResultSet execute(
      Statement statement,
      @Nullable Map namedValues,
      @Nullable String defaultNamespaceName) {
    return getSqlStatementExecutable().execute(statement, namedValues, defaultNamespaceName);
  }

  private SqlStatementExecutable getSqlStatementExecutable() {
    return sqlTwoPhaseCommitTransaction != null
        ? sqlTwoPhaseCommitTransaction
        : sqlTwoPhaseCommitTransactionManager;
  }

  @Override
  public void prepare() {
    assert sqlTwoPhaseCommitTransaction != null;
    sqlTwoPhaseCommitTransaction.prepare();
  }

  @Override
  public void validate() {
    assert sqlTwoPhaseCommitTransaction != null;
    sqlTwoPhaseCommitTransaction.validate();
  }

  @Override
  public void commit() {
    assert sqlTwoPhaseCommitTransaction != null;
    sqlTwoPhaseCommitTransaction.commit();
    sqlTwoPhaseCommitTransaction = null;
  }

  @Override
  public void rollback() {
    assert sqlTwoPhaseCommitTransaction != null;
    try {
      sqlTwoPhaseCommitTransaction.rollback();
    } finally {
      sqlTwoPhaseCommitTransaction = null;
    }
  }

  @Override
  public void abort() {
    assert sqlTwoPhaseCommitTransaction != null;
    try {
      sqlTwoPhaseCommitTransaction.abort();
    } finally {
      sqlTwoPhaseCommitTransaction = null;
    }
  }

  @Override
  public boolean isTransactionInProgress() {
    return sqlTwoPhaseCommitTransaction != null;
  }

  @Override
  public Metadata getMetadata() {
    return sqlTwoPhaseCommitTransactionManager.getMetadata();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy