Class: Synapse::UnitOfWork::NestableUnitOfWork Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/uow/nesting.rb

Overview

This class is abstract.

Partial implementation of a unit of work that can be nested

This implementation provides common actions that will be needed for nestable units of work, such as registration with the unit of work provider and nested commit and rollback operations

Direct Known Subclasses

UnitOfWork

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ undefined

Parameters:



13
14
15
16
17
# File 'lib/synapse/uow/nesting.rb', line 13

def initialize(provider)
  @inner_units = Array.new
  @provider = provider
  @started = false
end

Instance Method Details

#commitundefined

Commits this unit of work

All reigstered aggregates that have not been registered as stored are saved in their respective repositories, buffered events are sent to their respective event buses, and all registered listeners are notified of the commit.

After the commit (successful or not), the unit of work is unregistered and cleans up any resources it acquired. The effectively means that a rollback is done if the unit of work failed to commit.

Returns:

  • (undefined)

Raises:

  • (RuntimeError)

    If unit of work hasn’t been started yet



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/synapse/uow/nesting.rb', line 32

def commit
  unless started?
    raise 'Unit of work has not been started yet'
  end

  begin
    notify_prepare_commit
    store_aggregates

    unless @outer_unit
      perform_commit
      stop
      perform_cleanup
    end
  rescue => cause
    perform_rollback cause
    stop

    unless @outer_unit
      perform_cleanup
    end

    raise cause
  ensure
    clear
  end
end

#rollback(cause = nil) ⇒ undefined

Clears this unit of work of any buffered changes

Any buffered events and registered aggregates are discarded and any registered unit of work listeners are notified of the rollback.

Parameters:

  • cause (Error) (defaults to: nil)

Returns:

  • (undefined)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/synapse/uow/nesting.rb', line 68

def rollback(cause = nil)
  begin
    if started?
      @inner_units.each do |inner_unit|
        @provider.push inner_unit
        inner_unit.rollback cause
      end
      perform_rollback cause
    end
  ensure
    finalize_rollback
  end
end

#startundefined

Starts the unit of work, preparing it for aggregate registration

Returns:

  • (undefined)

Raises:

  • (RuntimeError)

    If unit of work has already been started



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/synapse/uow/nesting.rb', line 87

def start
  if started?
    raise 'Unit of work has already been started'
  end

  perform_start

  if @provider.started?
    # This is a nested unit of work
    @outer_unit = @provider.current

    if NestableUnitOfWork === @outer_unit
      @outer_unit.register_inner_unit self
    else
      # Outer unit is not aware of inner units, hook in with a listener
      @outer_unit.register_listener OuterCommitUnitOfWorkListener.new self, @provider
    end
  end

  @provider.push self
  @started = true
end

#started?Boolean

Returns true if this unit of work has been started

Returns:

  • (Boolean)


114
115
116
# File 'lib/synapse/uow/nesting.rb', line 114

def started?
  @started
end