• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

4.14.1.1 Transactions
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Database
          • Managing (dynamic) predicates
            • Transactions
              • transaction/1
              • transaction/2
              • transaction/3
              • snapshot/1
              • current_transaction/1
              • transaction_updates/1
    • Packages
Availability:built-in
Sourcetransaction(:Goal, :Constraint, +Mutex)
Similar to transaction/1, but allows verifying Constraint during the commit phase. This predicate follows the steps below. Any failure or exception during this process discards the transaction and releases Mutex when applicable. Constraint may modify the database. Such modifications follow the semantics that apply for Goal.

  • Call once(Goal)
  • Lock Mutex
  • Change the visibility to the current global state combined with the changes made by Goal
  • Call once(Constraint)
  • Commit the changes
  • Unlock Mutex.

This predicate is intended to execute multiple transactions with a time consuming Goal in part concurrently. For example, it can be used for a Compare And Swap (CAS) like design. We illustrate this using a simple counter in the code below. Note that the transaction fails if some other thread concurrently updated the counter. This is why we need the repeat/0 and a final !/0. The CAS-style update is in general useful if Goal is expensive and conflicts are rare.

:- dynamic counter/1.

increment_counter(Delta) :-
    repeat,
      transaction(( counter(Value),
                    Value2 is Value+Delta,
                  ),
                  ( retract(counter(Value)),
                    asserta(counter(Value2))
                  ),
                  counter_lock),
    !.
ClioPatria (version V3.1.1-51-ga0b30a5)