Class BulkChange
- java.lang.Object
-
- hudson.BulkChange
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class BulkChange extends Object implements Closeable
Transaction-like object that can be used to make a bunch of changes to an object, and defer theSaveable.save()until the end.The usage of
BulkChangeneeds to follow a specific closure-like pattern, namely:try (BulkChange bc = new BulkChange(someObject)) { ... make changes to 'someObject' bc.commit(); }Use of this method is optional. If
BulkChangeis not used, individual mutator will perform the save operation, and things will just run somewhat slower.Cooperation from
SaveableFor this class to work as intended,
Saveableimplementations need to co-operate. Namely,-
Mutator methods should invoke
this.save()so that if the method is called outside aBulkChange, the result will be saved immediately. -
In the
save()method implementation, usecontains(Saveable)and only perform the actual I/O operation when this method returns false.
See
Jenkins.save()as an example if you are not sure how to implementSaveable.- Since:
- 1.249
- Author:
- Kohsuke Kawaguchi
-
-
Field Summary
Fields Modifier and Type Field Description static SaveableALLMagicSaveableinstance that can makeBulkChangeveto all the save operations by making thecontains(Saveable)method return true for everything.Exceptionallocator
-
Constructor Summary
Constructors Constructor Description BulkChange(Saveable saveable)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort()Exits the scope ofBulkChangewithout saving the changes.voidclose()Alias forabort()to makeBulkChangeauto-closeable.voidcommit()Saves the accumulated changes.static booleancontains(Saveable s)Checks if the givenSaveableis currently in the bulk change.static BulkChangecurrent()Gets theBulkChangeinstance currently in scope for the current thread.
-
-
-
Field Detail
-
allocator
public final Exception allocator
-
ALL
public static final Saveable ALL
MagicSaveableinstance that can makeBulkChangeveto all the save operations by making thecontains(Saveable)method return true for everything.
-
-
Constructor Detail
-
BulkChange
public BulkChange(Saveable saveable)
-
-
Method Detail
-
commit
public void commit() throws IOExceptionSaves the accumulated changes.- Throws:
IOException
-
close
public void close()
Alias forabort()to makeBulkChangeauto-closeable.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
abort
public void abort()
Exits the scope ofBulkChangewithout saving the changes.This can be used when a bulk change fails in the middle. Note that unlike a real transaction, this will not roll back the state of the object.
The abort method can be called after the commit method, in which case this method does nothing. This is so that
BulkChangecan be used naturally in the try/finally block.
-
current
public static BulkChange current()
Gets theBulkChangeinstance currently in scope for the current thread.
-
contains
public static boolean contains(Saveable s)
Checks if the givenSaveableis currently in the bulk change.The expected usage is from the
Saveable.save()implementation to check if the actual persistence should happen now or not.
-
-