Spring framework transaction control

這邊只記設定的重點以 JDBC 為例,Hibernate 的設定也很類似, 主要是 tx:annotation-driven。DAO 加上 annotation 指定要做 transaction 管理,注意 rollbackFor 的設定,一旦 method catch 到 exception,method 內的異動都會 rollback。

applicationContext.xml 設定

<tx:annotation-driven transaction-manager=”myManager”/>

<bean id=”myManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”myDS”/>
</bean>

<bean id=”myDS” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
<property name=”driverClassName” value=”${myds.driver}”/>
<property name=”url” value=”${myds.url}”/>
<property name=”username” value=”${myds.username}”/>
<property name=”password” value=”${myds.password}”/>
</bean>

myDao.java (DAO interface 範例)
@Transactional(rollbackFor=Exception.class)
public interface myDao {

 

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...