WrapCallableInTransaction.java :  » REST » propidle » com » googlecode » propidle » Java Open Source

Java Open Source » REST » propidle 
propidle » com » googlecode » propidle » WrapCallableInTransaction.java
package com.googlecode.propidle;

import com.googlecode.totallylazy.records.Transaction;
import org.apache.lucene.index.IndexWriter;

import java.util.concurrent.Callable;

public class WrapCallableInTransaction implements Callable {
    private final IndexWriter indexWriter;
    private final Transaction transaction;
    private final Callable callable;

    public WrapCallableInTransaction(Transaction transaction, Callable callable) {
        this(null, transaction,  callable);
    }
    public WrapCallableInTransaction(IndexWriter indexWriter, Transaction transaction, Callable callable) {
        this.indexWriter = indexWriter;
        this.transaction = transaction;
        this.callable = callable;
    }

    public Object call() throws Exception {
        Object result;
        try {
            result = callable.call();
            if(indexWriter!=null) indexWriter.commit();
        } catch (Exception e) {
            transaction.rollback();
            throw new RuntimeException("Did not commit transaction", e);
        }
        transaction.commit();
        return result;

    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.