Ejemplo n.º 1
0
        public override void CommitAndRestartTx()
        {
            /*
             * This method is use by the Cypher runtime to cater for PERIODIC COMMIT, which allows a single query to
             * periodically, after x number of rows, to commit a transaction and spawn a new one.
             *
             * To still keep track of the running stream after switching transactions, we need to open the new transaction
             * before closing the old one. This way, a query will not disappear and appear when switching transactions.
             *
             * Since our transactions are thread bound, we must first unbind the old transaction from the thread before
             * creating a new one. And then we need to do that thread switching again to close the old transaction.
             */

            CheckNotTerminated();

            CollectTransactionExecutionStatistic();

            // (1) Unbind current transaction
            QueryRegistryOperations oldQueryRegistryOperations = _statement.queryRegistration();
            Statement           oldStatement   = _statement;
            InternalTransaction oldTransaction = _transaction;
            KernelTransaction   oldKernelTx    = _txBridge.getKernelTransactionBoundToThisThread(true);

            _txBridge.unbindTransactionFromCurrentThread();

            // (2) Create, bind, register, and unbind new transaction
            _transaction       = _graph.beginTransaction(TransactionType, SecurityContextConflict);
            _kernelTransaction = _txBridge.getKernelTransactionBoundToThisThread(true);
            _statement         = _kernelTransaction.acquireStatement();
            _statement.queryRegistration().registerExecutingQuery(_executingQuery);
            _txBridge.unbindTransactionFromCurrentThread();

            // (3) Rebind old transaction just to commit and close it (and unregister as a side effect of that)
            _txBridge.bindTransactionToCurrentThread(oldKernelTx);
            oldQueryRegistryOperations.UnregisterExecutingQuery(_executingQuery);
            try
            {
                oldStatement.Close();
                oldTransaction.Success();
                oldTransaction.Close();
            }
            catch (Exception t)
            {
                // Corner case: The old transaction might have been terminated by the user. Now we also need to
                // terminate the new transaction.
                _txBridge.bindTransactionToCurrentThread(_kernelTransaction);
                _transaction.failure();
                _transaction.close();
                _txBridge.unbindTransactionFromCurrentThread();
                throw t;
            }

            // (4) Unbind the now closed old transaction and rebind the new transaction for continued execution
            _txBridge.unbindTransactionFromCurrentThread();
            _txBridge.bindTransactionToCurrentThread(_kernelTransaction);
        }