Java Reference
In-Depth Information
You may apply the
@Transactional
annotation at the method level or the class level. When applying
this annotation to a class, all of the public methods within this class will be defined as transactional.
Although you can apply
@Transactional
to interfaces or method declarations in an interface, it's not
recommended because it may not work properly with class-based proxies (i.e., CGLIB proxies).
In the bean configuration file, you only have to enable the
<tx:annotation-driven>
element and
specify a transaction manager for it. That's all you need to make it work. Spring will advise methods with
@Transactional
, or methods in a class with
@Transactional
, from beans declared in the IoC container. As
a result, Spring can manage transactions for these methods.
<beans xmlns=
"
http://www.springframework.org/schema/beans
"
xmlns:xsi=
"
http://www.w3.org/2001/XMLSchema-instance
"
xmlns:tx=
"
http://www.springframework.org/schema/tx
"
xsi:schemaLocation=
"
http://www.springframework.org/schema/beans
<tx:annotation-driven transaction-manager=
"
transactionManager
"
/>
...
<bean id=
"
transactionManager
"
class=
"
org.springframework.jdbc.datasource.DataSourceTransactionManager
"
>
<property name=
"
dataSource
"
ref=
"
dataSource
"
/>
</bean>
<bean id=
"
bookShop
"
class=
"
com.apress.springenterpriserecipes.bookshop.spring.JdbcBookShop
"
>
<property name=
"
dataSource
"
ref=
"
dataSource
"
/>
</bean>
</beans>
In fact, you can omit the
transaction-manager
attribute in the
<tx:annotation-driven>
element if
your transaction manager has the name
transactionManager
. This element will automatically detect a
transaction manager with this name. You have to specify a transaction manager only when it has a
different name.
<beans ...>
<tx:annotation-driven />
...
</beans>
4-7. Setting the Propagation Transaction Attribute
Problem
When a transactional method is called by another method, it is necessary to specify how the transaction
should be propagated. For example, the method may continue to run within the existing transaction, or
it may start a new transaction and run within its own transaction.
Search WWH ::
Custom Search