基于AOP的事务管理与普通事务管理有什么区别?
2016-03-02 00:00
417 查看
摘要: jdbc事务管理器,声明的方法来实现事物的管理
(一) 用编程的方法来实现,我觉得这种方法比较灵活,控制起来比较方便,但是需要写一些额外的代码
(二)用声明的方法来实现事物的管理,在配置文件中加入下面
(一) 用编程的方法来实现,我觉得这种方法比较灵活,控制起来比较方便,但是需要写一些额外的代码
<!--定义Bean--> <bean id="Test" class="com.test.Test"> <property name="template" ref="jdbcTemplate" /> <property name="transaction" ref="transactionTemplate" /> </bean> <!--事务模板 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref local="transactionManager"/> </property> </bean>
<!-- jdbc事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> public class Test { private JdbcTemplate template; private TransactionTemplate transaction; private static final String TEST_SQL="insert into user(name,date) values(?,?)"; public void setTemplate(JdbcTemplate template) { this.template = template; } public void setTransaction(TransactionTemplate transaction) { this.transaction = transaction; } public void testSQL() throws Exception{ for(int i=0;i<10;i++){ if(i==8)throw new Exception("====error when updaa=======");//到第八条数据时我抛异常 template.update(TEST_SQL,new Object[]{"xugang"+i,"123"}); } } public static void main(String[] args) throws Exception{ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); final Test t=(Test)ctx.getBean("Test"); boolean f=t.transaction.execute(new TransactionCallback<Boolean>() { @Override public Boolean doInTransaction(TransactionStatus status) { try { t.testSQL();//测试SQL return Boolean.TRUE;//插入成功 } catch (Exception e) { // TODO Auto-generated catch block status.setRollbackOnly();//回滚事物 e.printStackTrace(); return Boolean.FALSE; } } }); }
(二)用声明的方法来实现事物的管理,在配置文件中加入下面
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /><!--以get开头的方法中的Connection是readonly的 --> <tx:method name="update*" rollback-for="Throwable"/><!-- 在update开头的方法中遇到异常(Throwable)就回滚--> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.test.Test2.*(..))"<!-- 匹配com.test.Test2这个类下面的所有方法--> id="service" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="service" /> </aop:config>
/** * 测试事物回滚 * @author loki */ public class Test2 { private JdbcTemplate template; private TransactionTemplate transaction; private static final String TEST_SQL="insert into user(name,date) values(?,?)"; public void setTemplate(JdbcTemplate template) { this.template = template; } public void setTransaction(TransactionTemplate transaction) { this.transaction = transaction; } public void testSQL() throws Exception{ for(int i=0;i<10;i++){ if(i==8)throw new Exception("====error when updaa======="); template.update(TEST_SQL,new Object[]{"xugang"+i,"123"}); } } public void getData(){ template.update(TEST_SQL,new Object[]{"xugang","123"}); } public void updateData() throws Exception{ testSQL(); } public static void main(String[] args) throws Exception{ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); final Test2 t=(Test2)ctx.getBean("Test2"); //t.getData();//get开头的方法是readonly的就会抛下面的异常 // java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed t.updateData();//执行玩了以后,数据库里面一条数据都没有的,在第8条的时候回滚了 } }
相关文章推荐
- Linux Mint 13 国内源
- 最全列表: 80 多个 Linux 系统管理员必备的监控工具
- Linux Mint 13 国内源
- Linux(Xubuntu)快速正确地安装 Ruby, Rails 环境
- OpenDNS
- RedHat Linux 9磁盘分区简介
- Ubuntu编译安装搜狗输入法for linux
- 学习linux决心书
- OpenCV
- 4G以上(含4G)内存与32位Linux系统
- 一些Linux软件(杂)
- apache和vmware出现端口冲突地区问题
- 决心书
- tomcat的性能与最大并发(1000)
- linux命令详解之chkconfig命令使用方法
- Linux下高并发socket最大连接数所受的各种限制
- 每天一个linux命令(36):diff 命令
- Centos 7 添加自定义系统服务的一些笔记
- centos搭建Mosquitto的php扩展之二
- hadoop 2.7.2 和 spark1.6 多节点安装