在使用@Transactional注解前需于下配置,两种方式都可以。
xml
<bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect"
        factory-method
="aspectOf">
        
<property name="transactionManager" ref="transactionManager" />
</bean>
JAVA
 1 @Repository
 2 public class TxService {
 3     private DataSourceTransactionManager txManager;
 4     
 5     @Resource(name="dataSource")
 6     public void setDataSource(DataSource dataSource) {
 7         txManager=new DataSourceTransactionManager(dataSource);
 8         // configure the AnnotationTransactionAspect to use it; this must be done before executing any transactional methods
 9         AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); 
10     }
11     
12     public DataSourceTransactionManager getTxManager() {
13         return txManager;
14     }
15 
16 }

@Transactional使用实例
  // these settings have precedence for this method
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void updateFoo(Foo foo) {
// do something
}

 @Transactional 可配置属性

PropertyTypeDescription
value String Optional qualifier specifying the transaction manager to be used.
propagation enum: Propagation Optional propagation setting.
isolation enum: Isolation Optional isolation level.
readOnly boolean Read/write vs. read-only transaction
timeout int (in seconds granularity) Transaction timeout.
rollbackFor Array of Class objects, which must be derived fromThrowable. Optional array of exception classes that must cause rollback.
rollbackForClassname Array of class names. Classes must be derived fromThrowable. Optional array of names of exception classes that must cause rollback.
noRollbackFor Array of Class objects, which must be derived fromThrowable. Optional array of exception classes that must not cause rollback.
noRollbackForClassname Array of String class names, which must be derived fromThrowable. Optional array of names of exception classes that must notcause rollback.


posted on 2010-10-06 20:03 岁月神偷 阅读(4087) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: