Class SecureTransaction

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public final class SecureTransaction
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Manage transactions by making sure that the correct SecurityContext is applied.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <E extends java.lang.Exception>
      void
      accept​(SecureTransaction.TxConsumer<E> block)
      Run some code within a new Transaction under the managed SecurityContext.
      <T,​E extends java.lang.Exception>
      T
      apply​(SecureTransaction.TxFunction<T,​E> block)
      Run some code within a new Transaction under the managed SecurityContext.
      void close()
      Closing this SecureTransaction only has an effect if it owns a top-level Transaction, in which case Transaction.close() will be called, without calling Transaction.commit().
      org.neo4j.graphdb.GraphDatabaseService db()
      Returns the GraphDatabaseService provided to this instance.
      SecureTransaction fork()
      Return a new SecureTransaction that owns a newly created top-level Transaction.
      static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db)
      Creates a new SecureTransaction with AccessMode.Static.FULL access.
      static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db, @Nullable org.neo4j.graphdb.Transaction top)
      Creates a new SecureTransaction with the same SecurityContext as the provided Transaction.
      static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db, @Nullable org.neo4j.graphdb.Transaction transaction, org.neo4j.internal.kernel.api.security.SecurityContext securityContext)
      Creates a new SecureTransaction with the given SecurityContext, ignoring the context within the Transaction.
      static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db, org.neo4j.internal.kernel.api.security.SecurityContext securityContext)
      Creates a new SecureTransaction with the given SecurityContext.
      java.util.Optional<org.neo4j.kernel.api.KernelTransaction> topLevelKernelTransaction()
      Returns the KernelTransaction for the provided top-level Transaction.
      SecureTransaction withRestrictedAccess​(org.neo4j.internal.kernel.api.security.AccessMode accessMode)
      Returns a new SecureTransaction restricted by the provided AccessMode.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • of

        public static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db)
        Creates a new SecureTransaction with AccessMode.Static.FULL access. This mirrors the default behavior of creating new transactions with GraphDatabaseService.beginTx().
      • of

        public static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db,
                                           @Nullable
                                           @Nullable org.neo4j.graphdb.Transaction top)
        Creates a new SecureTransaction with the same SecurityContext as the provided Transaction. If this instance is closed, the supplied transaction will be closed as well.

        The Transaction can be null, in which case this method behaves the same as of(GraphDatabaseService).

      • of

        public static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db,
                                           org.neo4j.internal.kernel.api.security.SecurityContext securityContext)
        Creates a new SecureTransaction with the given SecurityContext.
      • of

        public static SecureTransaction of​(org.neo4j.graphdb.GraphDatabaseService db,
                                           @Nullable
                                           @Nullable org.neo4j.graphdb.Transaction transaction,
                                           org.neo4j.internal.kernel.api.security.SecurityContext securityContext)
        Creates a new SecureTransaction with the given SecurityContext, ignoring the context within the Transaction. If this instance is closed, the supplied transaction will be closed as well.

        The Transaction can be null, in which case this method behaves the same as of(GraphDatabaseService, SecurityContext).

      • apply

        public <T,​E extends java.lang.Exception> T apply​(SecureTransaction.TxFunction<T,​E> block)
                                                        throws E extends java.lang.Exception
        Run some code within a new Transaction under the managed SecurityContext. The new transaction is closed afterwards and any resource that is tied to the lifecycle of that transaction will throw a NotInTransactionException upon access.
        Throws:
        E extends java.lang.Exception
      • accept

        public <E extends java.lang.Exception> void accept​(SecureTransaction.TxConsumer<E> block)
                                                    throws E extends java.lang.Exception
        Run some code within a new Transaction under the managed SecurityContext. The new transaction is closed afterwards.
        Throws:
        E extends java.lang.Exception
      • db

        public org.neo4j.graphdb.GraphDatabaseService db()
        Returns the GraphDatabaseService provided to this instance.
      • withRestrictedAccess

        public SecureTransaction withRestrictedAccess​(org.neo4j.internal.kernel.api.security.AccessMode accessMode)
        Returns a new SecureTransaction restricted by the provided AccessMode. The mode only restricts does not override the given SecurityContext, i.e. you cannot grant more access.

        One use-case is to restrict the access to AccessMode.Static.READ to make sure that only read-only queries can be executed.

        A new instance is returned, this instance remains untouched. However, the new instance has ownership of the same top-level Transaction within this instance. Closing it would also close this instance's top-level transaction. To decouple the transaction, call fork() on the returned instance.

      • fork

        public SecureTransaction fork()
        Return a new SecureTransaction that owns a newly created top-level Transaction. The returned instance will always own a transaction, regardless of whether this instance owns one. The returned instance will operate under the same SecurityContext as this instance.

        This is intended for when you need to keep track of a new transaction for a longer time, in which case you can use topLevelKernelTransaction() to get the underlying transaction. That method will never return an empty Optional when it's called on the returned instance.

      • close

        public void close()
        Closing this SecureTransaction only has an effect if it owns a top-level Transaction, in which case Transaction.close() will be called, without calling Transaction.commit().
        Specified by:
        close in interface java.lang.AutoCloseable