package io.ebeaninternal.server.transaction;
import io.ebeaninternal.api.SpiTransaction;
import javax.persistence.PersistenceException;
import java.util.HashMap;
import java.util.Map;
public class TransactionMap {
private final Map<String, State> map = new HashMap<>();
@Override
public String toString() {
return map.toString();
}
public boolean isEmpty() {
return map.isEmpty();
}
public State getState(String serverName) {
return map.get(serverName);
}
public State getStateWithCreate(String serverName) {
return map.computeIfAbsent(serverName, k -> new State());
}
public State removeState(String serverName) {
return map.remove(serverName);
}
public static class State {
SpiTransaction transaction;
@Override
public String toString() {
return "txn[" + transaction + "]";
}
public SpiTransaction get() {
return transaction;
}
public void set(SpiTransaction trans) {
if (transaction != null && transaction.isActive()) {
throw new PersistenceException("The existing transaction is still active?");
}
transaction = trans;
}
public void commit() {
transaction.commit();
transaction = null;
}
public void rollback() {
transaction.rollback();
transaction = null;
}
public void end() {
if (transaction != null) {
transaction.end();
transaction = null;
}
}
public void replace(SpiTransaction trans) {
transaction = trans;
}
}
}