Java Reference
In-Depth Information
To support transactional behavior, a transaction manager bean must first be applied to
the session factory. Typically, this would be a HibernateTransactionManager in a self-contained
application, or a JtaTransactionManager in an environment in which the container is manag-
ing transactions. Our application uses the Hibernate transaction manager, as declared in
Listing C-14.
Listing C-14. Declaring the HibernateTransactionManager Bean
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
The transaction manager must be notified of the session factory in use so that it can
manage the transactions of the database connection configured in the session factory. If
you want to be able to use nested transactions so that multiple calls to transactional meth-
ods can be made from a method that is itself enclosed in a transaction, you must set the
nestedTransactionAllowed property on the HibernateTransactionManager bean. Note that
Hibernate does not support the use of savepoints within nested transactions because it is
unable to rollback the session cache's state.
The transaction boundaries are applied to a bean by wrapping it in a proxy class that
then honors the original bean's API as declared in its interface(s). Typically, the basis of the
proxy is therefore declared as an abstract bean so that it can be applied to multiple DAO
beans as required. For illustrative purposes, our example application also uses this
approach (see Listing C-15).
Listing C-15. Declaring the Default Transactionality for Our DAO Beans
<bean id="daoTxTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="create*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
<prop key="get*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
</props>
</property>
</bean>
With the transaction manager's template prepared, the declaration of any DAO objects
must be wrapped in a bean declaration derived from this template.
Our wrapped bean is shown in Listing C-16. The lines highlighted in bold are the wrapped
bean's declaration—since it is only used as a property for the enclosing proxy, it does not need
to be assigned an id —instead, the id of the paperDao proxy is used when a DAO reference is
required. The proxy will honor the PaperDao interface declared in Listing C-7.
Search WWH ::




Custom Search