Java Reference
In-Depth Information
} finally {
So, now that you know how to carry out either local or global transactions, how do
you decide which to use? Read on for help making that decision.
Do I need a global transaction?
The answer to this question is: “probably not.” In general, there is a great deal of
overhead involved with global transactions. This is mostly due to the fact that they
are distributed and that they typically require more network traffic and state man-
agement than local transactions. If there were no cost, we probably would just use
global transactions for everything. In addition to the lost performance, global
transactions are harder to set up. They need more infrastructure, more software,
and more resources. So even if you're using container-managed transactions,
ensure that you're only using global transactions if you're absolutely certain you
need them. Most good application servers have very simple configuration options
to enable or disable distributed transactions.
If none of the options we have looked at will meet your needs, more options
are available.
7.5 Custom transactions
As you've already seen, i BATIS allows you to manage transactions in various ways. If
none of the provided transaction management approaches works for you, then
there are a couple of options for managing transactions yourself. The first is by writ-
ing your own transaction manager using the i BATIS interfaces and then plugging it
into the SQL Map configuration file. This approach is discussed in chapter 12. The
second approach is to simply pass i BATIS a JDBC Connection instance to work with,
thereby allowing you full control over the connection and transaction. There are
two ways to pass a Connection instance to a SqlMapClient . The first is setUserCon-
nection(Connection) , which is shown in listing 7.6.
Listing 7.6
Custom transaction control with setUserTransaction()
public void runStatementsUsingSetUserConnection() {
SqlMapClient sqlMapClient =
Connection conn = null;
Search WWH ::

Custom Search