aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/transaction/AbstractTransaction.java
blob: 150d29982ea15f6ada3a59f0a1fc2ad4ac2e5b26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.transaction;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * A convenience base transaction class for multi-operation transactions
 * which maintains the ordered list of operations to commit and provides a default
 * implementation of rollbackOrLog which logs a SEVERE message.
 * 
 * @author bratseth
 */
public abstract class AbstractTransaction implements Transaction {

    private static final Logger log = Logger.getLogger(AbstractTransaction.class.getName());
    private final List<Operation> operations = new ArrayList<>();

    protected AbstractTransaction() { }

    @Override
    public Transaction add(Operation operation) {
        this.operations.add(operation);
        return this;
    }

    @Override
    public Transaction add(List<Operation> operations) {
        this.operations.addAll(operations);
        return this;
    }

    @Override
    public List<Operation> operations() { return operations; }

    /** Default implementations which logs a severe message. Operations should implement toString to use this. */
    @Override
    public void rollbackOrLog() {
        log.severe("The following operations were incorrectly committed and probably require " +
                   "manual correction: " + operations());
    }

    /** Default implementation which only clears operations */
    @Override
    public void close() {
        operations.clear();
    }

}