setDataCommandInfo("Transaction commit");
if (!isConnectionAlive())
return;
final OTransactionOptimisticProxy tx = new OTransactionOptimisticProxy((ODatabaseRecordTx) connection.database.getUnderlying(),
channel, connection.data.protocolVersion, this);
try {
connection.database.begin(tx);
try {
connection.database.commit();
beginResponse();
try {
sendOk(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().getRecordVersion().getCounter() > 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().getRecordVersion());
}
if (connection.data.protocolVersion >= 20)
sendCollectionChanges();
} finally {
endResponse();
}
} catch (Exception e) {
if (connection != null && connection.database != null) {
if (connection.database.getTransaction().isActive())
connection.database.rollback(true);
final OSBTreeCollectionManager collectionManager = connection.database.getSbTreeCollectionManager();
if (collectionManager != null)
collectionManager.clearChangedIds();
}
sendErrorOrDropConnection(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(clientTxId, e);
}
}