Java Reference
In-Depth Information
Solution
The timeout transaction attribute (an integer that describes seconds) indicates how long your
transaction can survive before it is forced to roll back. This can prevent a long transaction from tying up
resources. The read-only attribute indicates that this transaction will only read but not update data. The
read-only flag is just a hint to enable a resource to optimize the transaction, and a resource might not
necessarily cause a failure if a write is attempted.
How It Works
The timeout and read-only transaction attributes can be defined in the @Transactional annotation. Note
that timeout is measured in seconds.
package com.apress.springenterpriserecipes.bookshop.spring;
...
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
public class JdbcBookShop extends JdbcDaoSupport implements BookShop {
...
@Transactional(
isolation = Isolation.REPEATABLE_READ,
timeout = 30,
readOnly = true)
public int checkStock(String isbn) {
...
}
}
In a Spring 2.0 transactional advice, the timeout and read-only transaction attributes can be
specified in the <tx:method> element.
<tx:advice ...>
<tx:attributes>
<tx:method name= " checkStock "
timeout= " 30 "
read-only= " true " />
</tx:attributes>
</tx:advice>
In classic Spring AOP, the timeout and read-only transaction attributes can be specified in the
transaction attributes of TransactionInterceptor and TransactionProxyFactoryBean .
<property name= " transactionAttributes " >
<props>
<prop key= " ... " >
PROPAGATION_REQUIRED, timeout_30, readOnly
</prop>
</props>
</property>
Search WWH ::




Custom Search