Created
October 4, 2013 15:43
-
-
Save lincolnthree/6828031 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Resource transactions | |
| The resources API provides a transaction service on FileResources. If you are familiar with UserTransaction in Java EE applications, this should feel pretty comfortable. | |
| ResourceTransaction transaction = factory.getTransaction(); | |
| try { | |
| // Starts the transaction | |
| transaction.begin(); | |
| FileResource<?> resource = factory.create(...); | |
| // The file won't be updated until commit is performed | |
| resource.setContents("Hello World"); | |
| String contents = resource.getContents(); // Returns "Hello World" | |
| FileResource<?> anotherResource = factory.create(...); | |
| // The file won't be deleted until commit is performed | |
| anotherResource.delete(); | |
| FileResource<?> newResource = factory.create(...); | |
| // The file won't be created until commit is performed | |
| newResource.createNewFile(); | |
| transaction.commit(); | |
| } catch (Exception e){ | |
| // Discard all changes since the beginning of this transaction | |
| transaction.rollback(); | |
| } | |
| Additionally, transactions support change-set inspection, so you can compare file contents before deciding whether or not a given transaction should be committed or rolled back: | |
| ... | |
| Collection<ResourceEvent> changeSet = transaction.getChangeSet(); | |
| ... | |
| transaction.commit(); | |
| According to the above example, this change set contains the following entries: | |
| Collection [ | |
| ResourceModified: <DirectoryResource>, | |
| ResourceCreated: <FileResource<?>>, | |
| ResourceModified: <FileResource<?>>, | |
| ResourceDeleted: <FileResource<?>>, | |
| ResourceCreated: <FileResource<?>> | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment