Search in sources :

Example 1 with OTransactionOptimisticProxy

use of com.orientechnologies.orient.server.tx.OTransactionOptimisticProxy in project orientdb by orientechnologies.

the class ONetworkProtocolBinary method commit.

protected void commit(OClientConnection connection) throws IOException {
    setDataCommandInfo(connection, "Transaction commit");
    if (!isConnectionAlive(connection))
        return;
    final OTransactionOptimisticProxy tx = new OTransactionOptimisticProxy(connection, this);
    try {
        try {
            connection.getDatabase().begin(tx);
        } catch (final ORecordNotFoundException e) {
            sendShutdown();
            throw e.getCause() instanceof OOfflineClusterException ? (OOfflineClusterException) e.getCause() : e;
        }
        try {
            try {
                connection.getDatabase().commit();
            } catch (final ORecordNotFoundException e) {
                throw e.getCause() instanceof OOfflineClusterException ? (OOfflineClusterException) e.getCause() : e;
            }
            beginResponse();
            try {
                sendOk(connection, clientTxId);
                // SEND BACK ALL THE RECORD IDS FOR THE CREATED RECORDS
                channel.writeInt(tx.getCreatedRecords().size());
                for (Entry<ORecordId, ORecord> entry : tx.getCreatedRecords().entrySet()) {
                    channel.writeRID(entry.getKey());
                    channel.writeRID(entry.getValue().getIdentity());
                    // IF THE NEW OBJECT HAS VERSION > 0 MEANS THAT HAS BEEN UPDATED IN THE SAME TX. THIS HAPPENS FOR GRAPHS
                    if (entry.getValue().getVersion() > 0)
                        tx.getUpdatedRecords().put((ORecordId) entry.getValue().getIdentity(), entry.getValue());
                }
                // SEND BACK ALL THE NEW VERSIONS FOR THE UPDATED RECORDS
                channel.writeInt(tx.getUpdatedRecords().size());
                for (Entry<ORecordId, ORecord> entry : tx.getUpdatedRecords().entrySet()) {
                    channel.writeRID(entry.getKey());
                    channel.writeVersion(entry.getValue().getVersion());
                }
                if (connection.getData().protocolVersion >= 20)
                    sendCollectionChanges(connection);
            } finally {
                endResponse(connection);
            }
        } catch (Exception e) {
            if (connection != null && connection.getDatabase() != null) {
                if (connection.getDatabase().getTransaction().isActive())
                    connection.getDatabase().rollback(true);
                final OSBTreeCollectionManager collectionManager = connection.getDatabase().getSbTreeCollectionManager();
                if (collectionManager != null)
                    collectionManager.clearChangedIds();
            }
            sendErrorOrDropConnection(connection, clientTxId, e);
        }
    } catch (OTransactionAbortedException e) {
    // TX ABORTED BY THE CLIENT
    } catch (Exception e) {
        // Error during TX initialization, possibly index constraints violation.
        if (tx.isActive())
            tx.rollback(true, -1);
        sendErrorOrDropConnection(connection, clientTxId, e);
    }
}
Also used : OTransactionOptimisticProxy(com.orientechnologies.orient.server.tx.OTransactionOptimisticProxy) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OLockException(com.orientechnologies.common.concur.lock.OLockException) OException(com.orientechnologies.common.exception.OException) SocketException(java.net.SocketException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException) IOException(java.io.IOException) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Aggregations

OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)1 OLockException (com.orientechnologies.common.concur.lock.OLockException)1 OException (com.orientechnologies.common.exception.OException)1 OIOException (com.orientechnologies.common.io.OIOException)1 OSBTreeCollectionManager (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)1 OTransactionOptimisticProxy (com.orientechnologies.orient.server.tx.OTransactionOptimisticProxy)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1