Top Description Fields Constructors Methods
org.springframework.transaction.support

public abstract Class ResourceHolderSynchronization<H extends ResourceHolder, K>

extends Object
implements TransactionSynchronization
Class Inheritance
All Implemented Interfaces
org.springframework.transaction.support.TransactionSynchronization, java.io.Flushable, org.springframework.core.Ordered
Type Parameters
<H>
the resource holder type
<K>
the resource key type

TransactionSynchronization implementation that manages a ResourceHolder bound through TransactionSynchronizationManager.
Author
Juergen Hoeller
Since
2.5.5

Field Summary

Modifier and TypeField and Description
private volatile boolean
private final H
private final K

Constructor Summary

AccessConstructor and Description
public
ResourceHolderSynchronization(H
the ResourceHolder to manage
resourceHolder
,
K
the key to bind the ResourceHolder for
resourceKey
)

Create a new ResourceHolderSynchronization for the given holder.

Method Summary

Modifier and TypeMethod and Description
public void
public void
afterCompletion(int
completion status according to the STATUS_* constants
status
)

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback.

public void
beforeCommit(boolean
whether the transaction is defined as read-only transaction
readOnly
)

Overrides default org.springframework.transaction.support.TransactionSynchronization.beforeCommit.

Invoked before transaction commit (before "beforeCompletion").

public void
beforeCompletion()

Overrides default org.springframework.transaction.support.TransactionSynchronization.beforeCompletion.

Invoked before transaction commit/rollback.

protected void
cleanupResource(H
the resource holder to process
resourceHolder
,
K
the key that the ResourceHolder was bound for
resourceKey
,
boolean
whether the transaction has committed (true) or rolled back (false)
committed
)

Perform a cleanup on the given resource (which is left bound to the thread).

public void
flush()

Overrides default org.springframework.transaction.support.TransactionSynchronization.flush.

Flush the underlying session to the datastore, if applicable: for example, a Hibernate/JPA session.

protected void
flushResource(H
the resource holder to flush
resourceHolder
)

Flush callback for the given resource holder.

protected void
processResourceAfterCommit(H
the resource holder to process
resourceHolder
)

After-commit callback for the given resource holder.

protected void
releaseResource(H
the resource holder to process
resourceHolder
,
K
the key that the ResourceHolder was bound for
resourceKey
)

Release the given resource (after it has been unbound from the thread).

public void
protected boolean
shouldReleaseAfterCompletion(H resourceHolder)

Return whether this holder's resource should be released after transaction completion (true).

protected boolean
shouldReleaseBeforeCompletion()

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

protected boolean
shouldUnbindAtCompletion()

Return whether this holder should be unbound at completion (or should rather be left bound to the thread after the transaction).

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

holderActiveback to summary
private volatile boolean holderActive
resourceHolderback to summary
private final H resourceHolder
resourceKeyback to summary
private final K resourceKey

Constructor Detail

ResourceHolderSynchronizationback to summary
public ResourceHolderSynchronization(H resourceHolder, K resourceKey)

Create a new ResourceHolderSynchronization for the given holder.

Parameters
resourceHolder:H

the ResourceHolder to manage

resourceKey:K

the key to bind the ResourceHolder for

See Also
TransactionSynchronizationManager#bindResource

Method Detail

afterCommitback to summary
public void afterCommit()

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCommit.

Doc from org.springframework.transaction.support.TransactionSynchronization.afterCommit.

Invoked after transaction commit. Can perform further operations right after the main transaction has successfully committed.

Can e.g. commit further operations that are supposed to follow on a successful commit of the main transaction, like confirmation messages or emails.

Note

The transaction will have been committed already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Annotations
@Override
afterCompletionback to summary
public void afterCompletion(int status)

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Doc from org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion.

Note

The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Parameters
status:int

completion status according to the STATUS_* constants

Annotations
@Override
beforeCommitback to summary
public void beforeCommit(boolean readOnly)

Overrides default org.springframework.transaction.support.TransactionSynchronization.beforeCommit.

Doc from org.springframework.transaction.support.TransactionSynchronization.beforeCommit.

Invoked before transaction commit (before "beforeCompletion"). Can e.g. flush transactional O/R Mapping sessions to the database.

This callback does not mean that the transaction will actually be committed. A rollback decision can still occur after this method has been called. This callback is rather meant to perform work that's only relevant if a commit still has a chance to happen, such as flushing SQL statements to the database.

Note that exceptions will get propagated to the commit caller and cause a rollback of the transaction.

Parameters
readOnly:boolean

whether the transaction is defined as read-only transaction

Annotations
@Override
beforeCompletionback to summary
public void beforeCompletion()

Overrides default org.springframework.transaction.support.TransactionSynchronization.beforeCompletion.

Doc from org.springframework.transaction.support.TransactionSynchronization.beforeCompletion.

Invoked before transaction commit/rollback. Can perform resource cleanup before transaction completion.

This method will be invoked after beforeCommit, even when beforeCommit threw an exception. This callback allows for closing resources before transaction completion, for any outcome.

Annotations
@Override
cleanupResourceback to summary
protected void cleanupResource(H resourceHolder, K resourceKey, boolean committed)

Perform a cleanup on the given resource (which is left bound to the thread).

Parameters
resourceHolder:H

the resource holder to process

resourceKey:K

the key that the ResourceHolder was bound for

committed:boolean

whether the transaction has committed (true) or rolled back (false)

flushback to summary
public void flush()

Overrides default org.springframework.transaction.support.TransactionSynchronization.flush.

Doc from org.springframework.transaction.support.TransactionSynchronization.flush.

Flush the underlying session to the datastore, if applicable: for example, a Hibernate/JPA session.

Annotations
@Override
flushResourceback to summary
protected void flushResource(H resourceHolder)

Flush callback for the given resource holder.

Parameters
resourceHolder:H

the resource holder to flush

processResourceAfterCommitback to summary
protected void processResourceAfterCommit(H resourceHolder)

After-commit callback for the given resource holder. Only called when the resource hasn't been released yet (shouldReleaseBeforeCompletion()).

Parameters
resourceHolder:H

the resource holder to process

releaseResourceback to summary
protected void releaseResource(H resourceHolder, K resourceKey)

Release the given resource (after it has been unbound from the thread).

Parameters
resourceHolder:H

the resource holder to process

resourceKey:K

the key that the ResourceHolder was bound for

resumeback to summary
public void resume()

Overrides default org.springframework.transaction.support.TransactionSynchronization.resume.

Doc from org.springframework.transaction.support.TransactionSynchronization.resume.

Resume this synchronization. Supposed to rebind resources to TransactionSynchronizationManager if managing any.

Annotations
@Override
shouldReleaseAfterCompletionback to summary
protected boolean shouldReleaseAfterCompletion(H resourceHolder)

Return whether this holder's resource should be released after transaction completion (true).

The default implementation returns !shouldReleaseBeforeCompletion(), releasing after completion if no attempt was made before completion.

See Also
releaseResource
shouldReleaseBeforeCompletionback to summary
protected boolean shouldReleaseBeforeCompletion()

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

Note that resources will only be released when they are unbound from the thread (shouldUnbindAtCompletion()).

The default implementation returns true.

See Also
releaseResource
shouldUnbindAtCompletionback to summary
protected boolean shouldUnbindAtCompletion()

Return whether this holder should be unbound at completion (or should rather be left bound to the thread after the transaction).

The default implementation returns true.

suspendback to summary
public void suspend()

Overrides default org.springframework.transaction.support.TransactionSynchronization.suspend.

Doc from org.springframework.transaction.support.TransactionSynchronization.suspend.

Suspend this synchronization. Supposed to unbind resources from TransactionSynchronizationManager if managing any.

Annotations
@Override