Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
AWare
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h2>Overview</h2> <p>The <strong>Unit Of Work</strong> pattern is a common pattern in enterprise application architectures. </p> <p>It can be used for implementing:</p> <ul> <li>transparent persistence</li> <li>replication</li> <li>clustering/caching</li> <li>transaction demarcation</li> <li>etc.</li> </ul> <p>It implements the concept of an "application transaction" that keeps track of:</p> <ul> <li><em>dirty</em> objects</li> <li><em>new</em> objects</li> <li><em>removed</em> objects</li> </ul> <p>It records the state of the objects, by tracking field changes, as it is before the transaction is started and will then rollback the state in memory upon rollback of the transaction. It allows adding additional behavior at <em>commit</em>, <em>rollback</em> etc., for example store all new, update all dirty and remove all removed objects in a persistent storage.</p> <p>Fowler defines the Unit Of Work pattern like this in his book <a href="http://www.martinfowler.com/eaaCatalog/unitOfWork.html">Patterns of Enterprise Application Architecture</a>:</p> <blockquote><p>"Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems."</p></blockquote> <p>This implementation implements the following transaction semantics:</p> <ul> <li><em>TX SUPPORTS</em></li> <li><em>TX MANDATORY</em></li> <li><em>TX NEVER</em></li> <li><em>TX REQUIRES</em></li> <li><em>TX REQUIRES NEW</em></li> </ul> <h2>UnitOfWorkListener</h2> <p>The user can implement listeners that allows him to trigger additional behaviour on specific events, such as <em>begin</em>, <em>commit</em>, <em>rollback</em>, <em>dispose</em> etc.</p> <p>All listeners must implement that <code>org.codehaus.aware.unitofwork.UnitOfWorkListener</code> interface. </p> <p>The Unit Of Work will not commit until all listeners have agreed on that. If for example on listener have set the transaction to <em>rollBackOnly</em> then the whole Unit Of Work transaction will rollback.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public interface UnitOfWorkListener { /** * Initializes the UnitOfWork listener by passing in the UnitOfWork for this listener. * * @param unitOfWork the UnitOfWork instance */ void initialize(UnitOfWork unitOfWork); /** * Checks if the UnitOfWork is set to rollback only * * @return boolean */ boolean isRollbackOnly(); /** * Is invoked when the UnitOfWork is started. */ void doBegin(); /** * Is invoked when the UnitOfWork is committed. */ void doCommit(); /** * Is invoked right before the UnitOfWork is committed. */ void doPreCommit(); /** * Is invoked right after the UnitOfWork is committed. */ void doPostCommit(); /** * Is invoked when the UnitOfWork is aborted. */ void doRollback(); /** * Is invoked when the UnitOfWork is aborted. */ void doDispose(); /** * Adds an object to the ID map. * * @param obj the object to add to the ID map */ void addToIdMap(Object obj); /** * Removes an object from the ID map. * * @param obj the object to remove from the ID map */ void removeFromIdMap(Object obj); } </pre></td></tr></table> <p>We also provide a convenience abstract adapter class that you can use if you are only interested in implementing a couple of methods and don't want to have empty method definitions. Just extend the <code>org.codehaus.aware.unitofwork.UnitOfWorkAdapter</code> class.</p> <h2>Definition</h2> <p>The listeners are added to the Unit Of Work in the <code>initialize</code> method:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> UnitOfWork.initialize(new Class[]{HibernateUnitOfWorkListener.class, JtaUnitOfWorkListener.class}); </pre></td></tr></table> <p>But in practice it is usually best to define this information outside the code, in a configuration file. Such as in the <a class="confluence-link" href="/display/AWARE/HibernateUnitOfWorkListener" data-linked-resource-id="3420" data-linked-resource-type="page" data-linked-resource-default-alias="HibernateUnitOfWorkListener" data-base-url="http://docs.codehaus.org">HibernateUnitOfWorkListener</a> implementation, which uses the <a class="confluence-link" href="/display/AWARE/SpringAspectContainer" data-linked-resource-id="3419" data-linked-resource-type="page" data-linked-resource-default-alias="SpringAspectContainer" data-base-url="http://docs.codehaus.org">SpringAspectContainer</a> to configure the UnitOfWork.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced