Class HttpRemoteTransaction

java.lang.Object
org.apache.tinkerpop.gremlin.driver.remote.HttpRemoteTransaction
All Implemented Interfaces:
AutoCloseable, RemoteTransaction, RequestSubmitter, Transaction

public class HttpRemoteTransaction extends Object implements RemoteTransaction
A Transaction implementation for HTTP-based remote connections.

This class provides synchronous, sequential request execution within a transaction context. All requests are pinned to a single host and include the transaction ID (after begin()).

Key characteristics:

  • Synchronous API only - no submitAsync() methods
  • Host pinning - all requests go to the same server
  • Sequential execution - requests block until complete

Usage:

 Transaction tx = cluster.transact("g");
 GraphTraversalSource gtx = tx.begin();
 gtx.addV("person").property("name", "alice").iterate();
 tx.commit();
 
This class is NOT thread-safe.
  • Field Details

  • Constructor Details

    • HttpRemoteTransaction

      public HttpRemoteTransaction(Client.PinnedClient pinnedClient, String graphAlias)
      Creates a new HTTP transaction.

      The transaction is not started until begin(Class) is called. A host is selected at creation time and all requests will be pinned to it.

      Parameters:
      pinnedClient - the underlying client for connection access
      graphAlias - the graph/traversal source alias (e.g., "g")
      Throws:
      NoHostAvailableException - if no hosts are available in the cluster
  • Method Details

    • begin

      public <T extends TraversalSource> T begin(Class<T> traversalSourceClass)
      Starts a transaction and returns a traversal source bound to it.

      When this transaction has not yet been started, this method sends g.tx().begin() to the server, which returns the transaction ID that all subsequent requests will include. This method is idempotent: if the transaction is already open, it does not send a second begin to the server and does not throw - it reuses the existing transaction ID and returns a traversal source bound to the same transaction. A remote transaction is single-use, so once it has been closed (via commit(), rollback(), timeout, or a failed begin) it cannot be reused and this method throws.

      Specified by:
      begin in interface Transaction
      Type Parameters:
      T - the type of the traversal source
      Parameters:
      traversalSourceClass - the class of the traversal source to create
      Returns:
      a traversal source bound to this transaction
      Throws:
      IllegalStateException - if the transaction has already been closed
      RuntimeException - if the transaction fails to begin
    • commit

      public void commit()
      Commits the transaction.

      Sends g.tx().commit() to the server and closes the transaction.

      Specified by:
      commit in interface Transaction
      Throws:
      IllegalStateException - if the transaction is not open
      TransactionException - if the commit fails
    • rollback

      public void rollback()
      Rolls back the transaction.

      Sends g.tx().rollback() to the server and closes the transaction.

      Specified by:
      rollback in interface Transaction
      Throws:
      IllegalStateException - if the transaction is not open
      TransactionException - if the rollback fails
    • getTransactionId

      public String getTransactionId()
      Returns the server-generated transaction ID, or null if the transaction has not yet been started via begin(Class).
      Specified by:
      getTransactionId in interface RemoteTransaction
      Returns:
      the transaction ID, or null if not yet begun
    • isOpen

      public boolean isOpen()
      Description copied from interface: Transaction
      Determines if a transaction is currently open.
      Specified by:
      isOpen in interface Transaction
    • readWrite

      public void readWrite()
      Description copied from interface: Transaction
      An internal function that signals a read or a write has occurred - not meant to be called directly by end users.
      Specified by:
      readWrite in interface Transaction
    • close

      public void close()
      Description copied from interface: Transaction
      Closes the transaction where the default close behavior defined by {Transaction.onClose(Consumer)} will be executed.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Transaction
    • onReadWrite

      public Transaction onReadWrite(Consumer<Transaction> consumer)
      Description copied from interface: Transaction
      Describes how a transaction is started when a read or a write occurs. This value can be set using standard behaviors defined in Transaction.READ_WRITE_BEHAVIOR or a mapper Consumer function.
      Specified by:
      onReadWrite in interface Transaction
    • onClose

      public Transaction onClose(Consumer<Transaction> consumer)
      Description copied from interface: Transaction
      Describes what happens to a transaction on a call to Graph.close(). This value can be set using standard behavior defined in Transaction.CLOSE_BEHAVIOR or a mapper Consumer function.
      Specified by:
      onClose in interface Transaction
    • addTransactionListener

      public void addTransactionListener(Consumer<Transaction.Status> listener)
      Description copied from interface: Transaction
      Adds a listener that is called back with a status when a commit or rollback is successful. It is expected that listeners be bound to the current thread as is standard for transactions. Therefore a listener registered in the current thread will not get callback events from a commit or rollback call in a different thread.
      Specified by:
      addTransactionListener in interface Transaction
    • removeTransactionListener

      public void removeTransactionListener(Consumer<Transaction.Status> listener)
      Description copied from interface: Transaction
      Removes a transaction listener.
      Specified by:
      removeTransactionListener in interface Transaction
    • clearTransactionListeners

      public void clearTransactionListeners()
      Description copied from interface: Transaction
      Removes all transaction listeners.
      Specified by:
      clearTransactionListeners in interface Transaction
    • submit

      public ResultSet submit(String gremlin)
      Description copied from interface: RequestSubmitter
      Submits a Gremlin script and blocks until the response is received.
      Specified by:
      submit in interface RequestSubmitter
      Parameters:
      gremlin - the Gremlin script to execute
      Returns:
      the results of the script execution
    • submit

      public ResultSet submit(String gremlin, Map<String,Object> parameters)
      Description copied from interface: RequestSubmitter
      Submits a Gremlin script with bound parameters and blocks until the response is received.

      Prefer this method over string concatenation when executing scripts with variable arguments, as parameterized scripts perform better.

      Specified by:
      submit in interface RequestSubmitter
      Parameters:
      gremlin - the Gremlin script to execute
      parameters - a map of parameters that will be bound to the script on execution
      Returns:
      the results of the script execution
    • submit

      public ResultSet submit(String gremlin, RequestOptions options)
      Description copied from interface: RequestSubmitter
      Submits a Gremlin script with request options and blocks until the response is received.
      Specified by:
      submit in interface RequestSubmitter
      Parameters:
      gremlin - the Gremlin script to execute
      options - the options to supply for this request
      Returns:
      the results of the script execution