當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring @transactional annotation 事务使用详解
生活随笔
收集整理的這篇文章主要介紹了
Spring @transactional annotation 事务使用详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
來源:http://www.yihaomen.com/article/java/412.htm
annotation 方式寫程序越來越稱謂主流了,以前用hibernate 也用 xml 一大堆配置文件。spring beans 管理也是一大堆xml 配置文件,但現在的趨勢是 annotation ,這種方式寫程序更方便,很少配置文件,維護起來也比較方便。這幾天重新看 spring 的文檔,仔細看了下 annotation 方式下事務的管理方式.
1. 配置 <context:annotation-config/>: 告訴spring 去讀 @Transactional 標注
3. 初始化 Datasource TransactionManager bean.
?程序代碼
<context:annotation-config/>
<!-- Add this tag to enable annotations transactions -->
<tx:annotation-driven??transaction-manager="transactionManager"/>
<bean id="transactionManager"
??class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
??<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
??<property name="url" value="jdbc:mysql://localhost:3306/apu"></property>
??<property name="username" value="root"></property>
??<property name="password" value=""></property>
??<!--改成你的密碼-->
</bean>
????
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
??<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao"?? class="springjdbc.transactions.declarative.annotations.AnnotatedUserDao">
??<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
如果 @Transactional 標注在 Class 上面, 那么將會對這個 Class 里面所有的 public 方法都包裝事務方法. 它有幾個屬性是可以配置的??readOnly, isolation, propagation,rollbackFor, noRollbackFor 。如果標記 readOnly=true, 那么就只能選擇了,因為只有查詢語句才能執行,如果是insert,update,delete 等,應該是readOnly=false, 不過默認是false的。rollbackFor 和 noRollbackFor 也是比較重要的兩個屬性. 默認情況下在有異常 RuntimeException??拋出或者 unchecked 異常拋出時,會回滾.
借用官方的例子:
?程序代碼
@Transactional
public class AnnotatedUserDao implements IUserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
??this.jdbcTemplate = jdbcTemplate;
}
????
public void deleteUser(int uid) {
??String delQuery = "delete from users where id = ?";
??jdbcTemplate.update(delQuery, new Object[] { uid });
}
public int insertUser(User user) {
??String inserQuery = "insert into users (username, password, enabled , id) values (?, ?, ?, ?) ";
??Object[] params = new Object[] { user.getUserName(),
????????????????user.getPassword(), user.isEnabled(), user.getId() };
??int[] types = new int[] { Types.VARCHAR, Types.VARCHAR, Types.BIT,
????????????????Types.INTEGER };
??int number = jdbcTemplate.update(inserQuery, params, types);
??return number;
}
// override the class level transactional behaviour for select method
@Transactional(readOnly = true)
public User selectUser(int uid) {
// for all the RuntimeExceptions the transactions will be automatically
// rolled back
??throw new RuntimeException("A runtime exception");
}
public int updateUser(User user) throws Exception {
??throw new Exception("A checked exception");
}
selectUser 會回滾,因為拋出了 RuntimeException 異常,而 updateUser 會執行下去,并不回滾,因為拋出的是 A checked exception .?
原理:??其實就是利用 AOP , spring 生成了一個代理類 這個代理類加入了事務的控制來實現。
總結
以上是生活随笔為你收集整理的Spring @transactional annotation 事务使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring的annotation-dr
- 下一篇: atx主板指的是什么意思