The dsp:demarcateTransaction
tag manages a transaction by starting it, checking it for errors, committing the transaction when no errors exist, and rolling back the transaction when they do. This tag creates an EL variable that is named by the var
attribute. The tag status is indicated by the success
property of the object that is referenced by this variable. Exceptions are held by the exception
(for begin transaction errors) or commitException
(for commit transaction errors) properties. An error might result when the dsp
:demarcateTransaction
tag is executed while a transaction is already in progress.
Attributes
TagVariable:varorid(Required)
You must define an EL variable or scripting variable:
Attribute | Description |
---|---|
| Names an EL variable. When you use |
| Names a scripting variable, which scriptlets and expressions can access at runtime. |
VariableScope:scope
The scope
attribute determines what resources can access the named EL variable. Options include: page
, request
, session
, or application
. Omitting this attribute causes the default, page
, to be used.
Properties
success
The success
property specifies the status of the transaction operation. A call to isSuccess
returns:
true: start and commit operations are successful .
false: Either operation fails.
exception
The exception
property identifies the Throwable
exception object produced when a transaction fails to start properly. A call to getException
returns the exception object.
CommitException
The Commitexception
property identifies the Throwable
exception object produced when a transaction fails to commit properly. A call to getCommitException
returns the exception object.
Example
Assume that currentStudent
is a page parameter that is set as a servlet bean iterates through all students in a class. You can also assume that there is a mechanism for tallying grades that consecutively saves each student’s grade to the currentGrade
page parameter.
<dsp:importbean bean="\atg\samples\Student_01" var="student"/>
<dsp:importbean bean="\atg\samples\Algebra" />
<dsp:demarcateTransaction var="demarcateXA">
<dsp:setvalue bean="Algebra.class.student" value="${student.name}"/>
<dsp:setvalue bean="Algebra.class.grade" value="${student.grade}"/>
<c:if test="${!demarcateXA.success}">
The grade could not be processed. Here's why:
<c:choose>
<c:when value="${!empty demarcateXA.exception}"/>
<c:out value="${demarcateXA.exception}"/>
<c:when value="${!empty demarcateXA.commitException}"/>
<c:out value="${demarcateXA.commitException}"/>
</c:choose>
</c:if>
</dsp:demarcateTransaction>
In this example, a transaction is created to encapsulate the tasks involved in saving the grades provided to students in a class. As a security precaution, the transaction ensures that the student names and grades are added to Algebra.Student_01
at the same time. If the transaction fails, neither name nor grade is saved; instead, the appropriate error (caused during start or commit operation) displays.