com.scalar.db.sql.SqlTransactionSessionStrategy Maven / Gradle / Ivy
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 SqlTransactionSessionStrategy implements SqlSessionStrategy {
private final SqlTransactionManager sqlTransactionManager;
@Nullable private SqlTransaction sqlTransaction;
SqlTransactionSessionStrategy(SqlTransactionManager sqlTransactionManager) {
this.sqlTransactionManager = Objects.requireNonNull(sqlTransactionManager);
}
@Override
public Optional getTransactionId() {
if (sqlTransaction == null) {
return Optional.empty();
}
return Optional.of(sqlTransaction.getId());
}
@Override
public String begin() {
assert sqlTransaction == null;
sqlTransaction = sqlTransactionManager.begin();
return sqlTransaction.getId();
}
@Override
public String start() {
assert sqlTransaction == null;
sqlTransaction = sqlTransactionManager.start();
return sqlTransaction.getId();
}
@Override
public void join(String transactionId) {
assert sqlTransaction == null;
sqlTransaction = sqlTransactionManager.join(transactionId);
}
@Override
public void suspend() {
sqlTransaction = null;
}
@Override
public void resume(String transactionId) {
assert sqlTransaction == null;
sqlTransaction = sqlTransactionManager.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 sqlTransaction != null ? sqlTransaction : sqlTransactionManager;
}
@Override
public void prepare() {
throw new AssertionError("Should never be called");
}
@Override
public void validate() {
throw new AssertionError("Should never be called");
}
@Override
public void commit() {
assert sqlTransaction != null;
sqlTransaction.commit();
sqlTransaction = null;
}
@Override
public void rollback() {
assert sqlTransaction != null;
try {
sqlTransaction.rollback();
} finally {
sqlTransaction = null;
}
}
@Override
public void abort() {
assert sqlTransaction != null;
try {
sqlTransaction.abort();
} finally {
sqlTransaction = null;
}
}
@Override
public boolean isTransactionInProgress() {
return sqlTransaction != null;
}
@Override
public Metadata getMetadata() {
return sqlTransactionManager.getMetadata();
}
}