日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring @transactional annotation 事务使用详解

發布時間:2023/12/15 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 標注

如果你聲明了 context:component-scan ,Spring會默認<context:annotation-config/> 2. <tx:annotation-driven/>:??自動包裝代碼,生產事務管理
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 事务使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。