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

org.deephacks.graphene.TransactionManager Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package org.deephacks.graphene;

import com.sleepycat.je.Environment;
import com.sleepycat.je.Transaction;

import java.util.Stack;

public class TransactionManager {
    private static final ThreadLocal> threadLocal = new ThreadLocal<>();
    private final Environment environment;

    public TransactionManager(Environment environment) {
        this.environment = environment;
    }

    public Transaction beginTransaction() {
        Transaction tx = environment.beginTransaction(null, null);
        push(tx);
        return tx;
    }

    public void commit() {
        Transaction tx = pop();
        if (tx == null) {
            return;
        }
        tx.commit();
    }

    public void rollback() {
        Transaction tx = pop();
        if (tx == null) {
            return;
        }
        tx.abort();
    }

    public void push(Transaction value) {
        Stack stack = threadLocal.get();
        if (stack == null) {
            stack = new Stack<>();
        }
        stack.push(value);
        threadLocal.set(stack);
    }

    public Transaction peek() {
        Stack stack = threadLocal.get();
        if (stack == null || stack.isEmpty()) {
            return null;
        }
        return stack.peek();
    }

    public Transaction pop() {
        Stack stack = threadLocal.get();
        if (stack == null || stack.isEmpty()) {
            return null;
        }
        return stack.pop();
    }

    public void clear() {
        Stack stack = threadLocal.get();
        stack.clear();
        threadLocal.set(null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy