Class BulkChange
- All Implemented Interfaces:
Closeable
,AutoCloseable
Saveable.save()
until the end.
The usage of BulkChange
needs 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 BulkChange
is not used, individual mutator
will perform the save operation, and things will just run somewhat slower.
Cooperation from Saveable
For this class to work as intended, Saveable
implementations 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 implement Saveable
.
- Since:
- 1.249
- Author:
- Kohsuke Kawaguchi
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Saveable
MagicSaveable
instance that can makeBulkChange
veto all the save operations by making thecontains(Saveable)
method return true for everything.final Exception
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Exits the scope ofBulkChange
without saving the changes.void
close()
Alias forabort()
to makeBulkChange
auto-closeable.void
commit()
Saves the accumulated changes.static boolean
Checks if the givenSaveable
is currently in the bulk change.static BulkChange
current()
Gets theBulkChange
instance currently in scope for the current thread.
-
Field Details
-
allocator
-
ALL
MagicSaveable
instance that can makeBulkChange
veto all the save operations by making thecontains(Saveable)
method return true for everything.
-
-
Constructor Details
-
BulkChange
-
-
Method Details
-
commit
Saves the accumulated changes.- Throws:
IOException
-
close
public void close()Alias forabort()
to makeBulkChange
auto-closeable.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
abort
public void abort()Exits the scope ofBulkChange
without 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
BulkChange
can be used naturally in the try/finally block. -
current
Gets theBulkChange
instance currently in scope for the current thread. -
contains
Checks if the givenSaveable
is 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.
-