Search in sources :

Example 46 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class EmbeddedGraphDbImpl method beginTx.

/**
 * @throws TransactionFailureException if unable to start transaction
 */
public Transaction beginTx() {
    if (graphDbInstance.transactionRunning()) {
        if (placeboTransaction == null) {
            placeboTransaction = new PlaceboTransaction(graphDbInstance.getTransactionManager());
        }
        return placeboTransaction;
    }
    TransactionManager txManager = graphDbInstance.getTransactionManager();
    Transaction result = null;
    try {
        txManager.begin();
        result = new TopLevelTransaction(txManager);
    } catch (Exception e) {
        throw new TransactionFailureException("Unable to begin transaction", e);
    }
    return result;
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransactionManager(javax.transaction.TransactionManager) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) NoSuchElementException(java.util.NoSuchElementException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 47 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class TxManager method init.

public void init(XaDataSourceManager xaDsManagerToUse) {
    this.xaDsManager = xaDsManagerToUse;
    txThreadMap = new ArrayMap<Thread, TransactionImpl>(5, true, true);
    logSwitcherFileName = txLogDir + separator + "active_tx_log";
    txLog1FileName = "tm_tx_log.1";
    txLog2FileName = "tm_tx_log.2";
    try {
        if (new File(logSwitcherFileName).exists()) {
            FileChannel fc = new RandomAccessFile(logSwitcherFileName, "rw").getChannel();
            byte[] fileName = new byte[256];
            ByteBuffer buf = ByteBuffer.wrap(fileName);
            fc.read(buf);
            fc.close();
            String currentTxLog = txLogDir + separator + UTF8.decode(fileName).trim();
            if (!new File(currentTxLog).exists()) {
                throw new TransactionFailureException("Unable to start TM, " + "active tx log file[" + currentTxLog + "] not found.");
            }
            txLog = new TxLog(currentTxLog);
            msgLog.logMessage("TM opening log: " + currentTxLog, true);
        } else {
            if (new File(txLogDir + separator + txLog1FileName).exists() || new File(txLogDir + separator + txLog2FileName).exists()) {
                throw new TransactionFailureException("Unable to start TM, " + "no active tx log file found but found either " + txLog1FileName + " or " + txLog2FileName + " file, please set one of them as active or " + "remove them.");
            }
            ByteBuffer buf = ByteBuffer.wrap(txLog1FileName.getBytes("UTF-8"));
            FileChannel fc = new RandomAccessFile(logSwitcherFileName, "rw").getChannel();
            fc.write(buf);
            txLog = new TxLog(txLogDir + separator + txLog1FileName);
            msgLog.logMessage("TM new log: " + txLog1FileName, true);
            fc.force(true);
            fc.close();
        }
        Iterator<List<TxLog.Record>> danglingRecordList = txLog.getDanglingRecords();
        if (danglingRecordList.hasNext()) {
            log.info("Unresolved transactions found, " + "recovery started ...");
            recover(danglingRecordList);
            log.info("Recovery completed, all transactions have been " + "resolved to a consistent state.");
            msgLog.logMessage("Recovery completed, all transactions have been " + "resolved to a consistent state.");
        }
        getTxLog().truncate();
        tmOk = true;
    } catch (IOException e) {
        log.severe("Unable to start TM");
        throw new TransactionFailureException("Unable to start TM", e);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) RandomAccessFile(java.io.RandomAccessFile) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 48 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class TxModule method registerDataSource.

/**
 * Use this method to add data source that can participate in transactions
 * if you don't want a data source configuration file.
 *
 * @param name
 *            The data source name
 * @param className
 *            The (full) class name of class
 * @param resourceId
 *            The resource id identifying datasource
 * @param params
 *            The configuration map for the datasource
 * @throws LifecycleException
 */
public XaDataSource registerDataSource(String dsName, String className, byte[] resourceId, Map<?, ?> params) {
    XaDataSourceManager xaDsMgr = xaDsManager;
    String name = dsName.toLowerCase();
    if (xaDsMgr.hasDataSource(name)) {
        throw new TransactionFailureException("Data source[" + name + "] has already been registered");
    }
    try {
        XaDataSource dataSource = xaDsMgr.create(className, params);
        xaDsMgr.registerDataSource(name, dataSource, resourceId);
        return dataSource;
    } catch (Exception e) {
        throw new TransactionFailureException("Could not create data source [" + name + "], see nested exception for cause of error", e.getCause());
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Example 49 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class TxModule method registerDataSource.

public XaDataSource registerDataSource(String dsName, String className, byte[] resourceId, Map<?, ?> params, boolean useExisting) {
    XaDataSourceManager xaDsMgr = xaDsManager;
    String name = dsName.toLowerCase();
    if (xaDsMgr.hasDataSource(name)) {
        if (useExisting) {
            return xaDsMgr.getXaDataSource(name);
        }
        throw new TransactionFailureException("Data source[" + name + "] has already been registered");
    }
    try {
        XaDataSource dataSource = xaDsMgr.create(className, params);
        xaDsMgr.registerDataSource(name, dataSource, resourceId);
        return dataSource;
    } catch (Exception e) {
        throw new TransactionFailureException("Could not create data source " + name + "[" + name + "]", e);
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Example 50 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class XaDataSourceManager method getBranchId.

synchronized byte[] getBranchId(XAResource xaResource) {
    if (xaResource instanceof XaResource) {
        byte[] branchId = ((XaResource) xaResource).getBranchId();
        if (branchId != null) {
            return branchId;
        }
    }
    Iterator<Map.Entry<String, XaDataSource>> itr = dataSources.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, XaDataSource> entry = itr.next();
        XaDataSource dataSource = entry.getValue();
        XAResource resource = dataSource.getXaConnection().getXaResource();
        try {
            if (resource.isSameRM(xaResource)) {
                String name = entry.getKey();
                return sourceIdMapping.get(name);
            }
        } catch (XAException e) {
            throw new TransactionFailureException("Unable to check is same resource", e);
        }
    }
    throw new TransactionFailureException("Unable to find mapping for XAResource[" + xaResource + "]");
}
Also used : XAResource(javax.transaction.xa.XAResource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) XAException(javax.transaction.xa.XAException) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)57 Transaction (org.neo4j.graphdb.Transaction)17 NotFoundException (org.neo4j.graphdb.NotFoundException)9 Node (org.neo4j.graphdb.Node)8 Test (org.junit.Test)7 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 LinkedList (java.util.LinkedList)6 SystemException (javax.transaction.SystemException)6 XAException (javax.transaction.xa.XAException)6 XAResource (javax.transaction.xa.XAResource)6 KernelException (org.neo4j.exceptions.KernelException)6 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)6 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)6 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)6 IllegalTokenNameException (org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException)6 TokenCapacityExceededKernelException (org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException)6 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 IOException (java.io.IOException)5