Java Reference
In-Depth Information
So where do we stop and start transactions then? Ideally, you would start the
transaction nowhere. In other words, you'd let the container do it. So declara-
tively you would configure your application such that the transactions were
demarcated by the container. Whether you're using an application server with
stateless session beans, or a lightweight container like the Spring Framework, you
can configure transactions declaratively. The container will ensure that the trans-
actions are started, committed, and ended appropriately. i BATIS has a unified pro-
gramming model for transactions, meaning you can and should still use
startTransaction() , commitTransaction() , and endTransaction() within your
code, even if the container itself will demarcate the real transaction. This allows
you to port your persistence code outside of a container and still have a relatively
clean transaction story. When config-
ured for EXTERNAL transaction man-
agement, i BATIS will let the container
handle the transactions.
If you're not in a position to allow
for container-managed transactions,
you can manage them yourself. With
a layered architecture, you have a
number of options for starting and
ending your transactions. Regardless
of what you choose, it's important
that you remain consistent. Referring
to figure 7.3, which shows a diagram
of a layered architecture, you could
conceivably demarcate your transac-
tions at the presentation layer, the
business logic layer, or the persis-
tence layer.
Let's take a close look at how you
would demarcate transactions in each
of these three layers.
Presentation
Business Object
Model
Business Logic
Persistence
Database
Figure 7.3
Layered architecture
7.6.1
Demarcating transactions at the presentation layer
Demarcating transactions at the presentation layer will create very large, wide-
scoped, long-lived transactions. However, such transactions are also very safe,
provide the best data integrity, and are easy to implement. You can use a Servlet filter
or a plug-in for your presentation framework to start, commit, and end your
Search WWH ::




Custom Search