Class Transaction

Transaction class. Each command in a transaction must use the same namespace.

note: By default, open transactions are destroyed when the final client in a process is closed. If you need your transaction to persist after the last client has been closed, provide false for the destroy Transactions argument in Client#close. For more information on memory management, see Transaction.destroyAll.

const Aerospike = require('aerospike')

// INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
var config = {
hosts: '192.168.33.10:3000',
// Timeouts disabled, latency dependent on server location. Configure as needed.
policies: {
write : new Aerospike.WritePolicy({socketTimeout : 0, totalTimeout : 0}),
}
}

let bins = {
int: 123,
double: 3.1415,
string: 'xyz',
bytes: Buffer.from('hello world!'),
list: [1, 2, 3],
map: {num: 123, str: 'abc', list: ['a', 'b', 'c']}
}
let meta = {
ttl: 386400 // 1 day
}
let key = new Aerospike.Key('test', 'demo', 'myKey')

let policy = {
txn: tran
};
;(async () => {
let client = await Aerospike.connect(config)

let tran = new Aerospike.Transaction()



await client.put(key, bins, meta, policy)

let get_result = await client.get(key1, policy)

let result = await client.commit(tran)
await client.close()
})();
const Aerospike = require('aerospike')

// INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
var config = {
hosts: '192.168.33.10:3000',
// Timeouts disabled, latency dependent on server location. Configure as needed.
policies: {
read : new Aerospike.ReadPolicy({socketTimeout : 0, totalTimeout : 0}),
write : new Aerospike.WritePolicy({socketTimeout : 0, totalTimeout : 0}),
}
}

let key1 = new Aerospike.Key('test', 'demo', 'myKey')
let key2 = new Aerospike.Key('test', 'demo', 'myKey')

let record1 = {abc: 123}
let record2 = {def: 456}

;(async () => {
let client = await Aerospike.connect(config)

const policy = {
txn: tran
}

await client.put(key4, record2, meta, policy)

const policyRead = {
txn: tran
}

let get_result = await client.get(key1, policy) // Will reflect the new value recently put.

await client.put(key2, record2, meta, policy)

let result = await client.abort(tran)

get_result = await client.get(key4) // Will reset to the value present before transaction started.

get_result = await client.get(key5) // Will reset to the value present before transaction started.

await client.close()
})();

v6.0.0

Constructors

  • Construct a new Aerospike Transaction instance.

    Parameters

    • Optionalreads_capacity: number
    • Optionalwrites_capacity: number

    Returns Transaction

Properties

abortStatus: {
    ALREADY_ABORTED: 1;
    CLOSE_ABANDONED: 3;
    OK: 0;
    ROLL_BACK_ABANDONED: 2;
}

Transaction abort status code.

Type declaration

  • ALREADY_ABORTED: 1

    Transaction has already been aborted.

  • CLOSE_ABANDONED: 3

    Transaction has been rolled back, but client transaction close was abandoned. Server will eventually close the transaction.

  • OK: 0

    Abort succeeded.

  • ROLL_BACK_ABANDONED: 2

    Client roll back abandoned. Server will eventually abort the transaction.

capacity: {
    READ_DEFAULT: 128;
    WRITE_DEFAULT: 128;
}

Default Transaction capacity values.

Type declaration

  • READ_DEFAULT: 128

    Contains the default reeadDefault for aerospike.Transaction

  • WRITE_DEFAULT: 128

    Contains the default writeCapacity for aerospike.Transaction

commitStatus: {
    ALREADY_COMMITTED: 1;
    CLOSE_ABANDONED: 5;
    MARK_ROLL_FORWARD_ABANDONED: 3;
    OK: 0;
    ROLL_FORWARD_ABANDONED: 4;
    VERIFY_FAILED: 2;
}

Transaction commit status code.

Type declaration

  • ALREADY_COMMITTED: 1

    Transaction has already been committed.

  • CLOSE_ABANDONED: 5

    Transaction has been rolled forward, but client transaction close was abandoned. Server will eventually close the transaction.

  • MARK_ROLL_FORWARD_ABANDONED: 3

    Transaction mark roll forward abandoned. Transaction will be aborted when error is not in doubt. If the error is in doubt (usually timeout), the commit is in doubt.

  • OK: 0

    Commit succeeded.

  • ROLL_FORWARD_ABANDONED: 4

    Client roll forward abandoned. Server will eventually commit the transaction.

  • VERIFY_FAILED: 2

    Transaction verify failed. Transaction will be aborted.

state: {
    ABORTED: 3;
    COMMITTED: 2;
    OPEN: 0;
    VERIFIED: 1;
}

Transaction state enumeration

Type declaration

  • ABORTED: 3

    Transaction was aborted.

  • COMMITTED: 2

    Transaction was commited.

  • OPEN: 0

    Transaction is still open.

  • VERIFIED: 1

    Transaction was verified.

Methods

  • Destroys all open transactions

    Returns void

    Use of this API is only necessary when the client is closed with the destroyTransactions parameter set is set to false. See example below for usage details.

    To avoid using this API, close the final connected client in the process with destroyTransactions set to true (default is true), and the transaction will be destroyed automatically.

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    let tran1 = new Aerospike.Transaction()
    let client = await Aerospike.connect(config)
    client.close(false, true) // `destroyTransactions is true`, tran1 is no longer usable.

    let tran2 = new Aerospike.Transaction()
    client = await Aerospike.connect(config)
    client.close(false, true) // `destroyTransactions is false`, tran2 can still be used.

    // In order to properly manage the memory at this point, do one of two things before the process exits:

    // 1: call destroyAll() to destroy all outstanding transactions from this process.
    tran1.destroyAll()

    // 2: reopen and close the final connected client with destroyTransactions
    // client = await Aerospike.connect(config)
    // client.close() // Default will destory the transactions

    })();

    v6.0.0

  • Get ID for this transaction

    Returns number

    Transaction ID

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    let id = tran.getId()
    })();

    v6.0.0

  • Get inDoubt status for this transaction.

    Returns boolean

    Transaction inDoubt status

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    let inDoubt = tran.getInDoubt()
    })();

    v6.0.0

  • Gets the expected number of record reads in the Transaction. Minimum value is 16.

    Returns number

    number of records reads in the Transaction.

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    let readsCapacity = tran.getReadsCapacity()
    console.log(readsCapacity) // 128
    })();

    v6.0.0

  • Gets the current state of the Transaction.

    Returns number

    Transaction timeout in seconds

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    let state = tran.getState()

    })();
  • Gets the current Transaction timeout value.

    Returns number

    Transaction timeout in seconds

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    let timeout = tran.getTimeout()
    })();

    v6.0.0

  • Gets the expected number of record reads in the tran. Minimum value is 16.

    Returns number

    number of records reads in the tran.

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    let writesCapacity = tran.getWritesCapacity()
    console.log(writesCapacity) // 128
    })();

    v6.0.0

  • Set transaction timeout in seconds. The timer starts when the transaction monitor record is created. This occurs when the first command in the transaction is executed.

    If the timeout is reached before a commit or abort is called, the server will expire and rollback the transaction.

    If the transaction timeout is zero, the server configuration mrt-duration is used. The default mrt-duration is 10 seconds.

    Default Client transaction timeout is 0.

    Parameters

    • timeout: number

      Transaction timeout in seconds

    Returns void

    const Aerospike = require('aerospike')
    const Key = Aerospike.Key

    // INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
    var config = {
    hosts: '192.168.33.10:3000',
    // Timeouts disabled, latency dependent on server location. Configure as needed.
    policies: {
    batch : new Aerospike.BatchPolicy({socketTimeout : 0, totalTimeout : 0}),
    }
    }

    ;(async () => {
    // Establishes a connection to the server
    let tran = new Aerospike.Transaction()
    tran.setTimeout(5) // Set timeout for 5 seconds!

    console.log(tran.getTimeout()) // 5
    })();
MMNEPVFCICPMFPCPTTAAATR