java 板


LINE

最近想搞懂 Spring 怎麽管理 transaction 的 先从程式可以开始跑开始,照书上设定了几个范例 目前在试 @Transactional,遇到无法 rollback 的问题,想请教 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <context:property-placeholder location="classpath:idv/sql/jdbc.properties" /> <context:component-scan base-package="idv.spring.tx.dao, idv.spring.tx.ch02_spring.e_annotation.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> </beans> Service.java @Service("spring_annotation_memberService") public class MemberService { @Autowired @Qualifier("jdbc_tx_memberDao") private MemberDao memberDao; @Autowired @Qualifier("jdbc_tx_memberDetailDao") private MemberDetailDao memberDetailDao; @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveMember(Member member, MemberDetail memberDetail) { try { memberDao.addMember(member); memberDetailDao.addMemberDetail(memberDetail); } catch (Exception ex) { ex.printStackTrace(); } } } Main.java public class SpringTxMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:idv/spring/tx/ch02_spring/e_annotation/applicationContext.xml"); Member member = new Member(); member.setId("xxxxx"); member.setAccount("xxxxx"); member.setNickName("Xxxxx"); member.setStatus(Status.ACTIVE); MemberDetail memberDetail = new MemberDetail(); memberDetail.setId("00001"); // memberDetail.setMemberId(member.getId()); memberDetail.setMemberId(member.getId() + "xxxxxxxxxxxxxxxx"); // this would cause SQLException memberDetail.setFirstName("YYYY"); memberDetail.setLastName("ZZZZ"); memberDetail.setIdNum("asdfghjkl"); MemberService memberService = context.getBean("spring_annotation_memberService", MemberService.class); memberService.saveMember(member, memberDetail); ((AbstractApplicationContext) context).close(); } } Dao 的部份应该不用附程式吧,就是直接用 jdbcTemplate 而已 故意在 service 的 memberDetailDao.addMemberDetail(memberDetail); 出错 不过 memberDao.addMember(member); 还是成功写入资料库了 测试的 DB 是 H2 和 MySQL(community-5.7.14.0) 一开始在练习由程式控制 transaction 时 有遇到 org.springframework.jdbc.datasource.DriverManagerDataSource 不能 disable auto-commit,所以一直失败 改为 org.apache.commons.dbcp.BasicDataSource 并设定 <property name="defaultAutoCommit" value="false" /> 就成功 不过接着试了 1. DataSourceTransactionManager 2. TransactionTemplate 都是只用 org.springframework.jdbc.datasource.DriverManagerDataSource 也有成功 rollback 再试 3. TransactionProxyFactoryBean 4. <tx:advice> 也是失败 其中 TransactionProxyFactoryBean 管理 transaction 设定比较复杂 所以先跳过不看 <tx:advice> 则应该是 <aop:pointcut> 的部份设定有错 这要回头去补 AOP 的设定方式,也是後续把 AOP 设定搞懂後再说 现在是试 @Transactional 的方式时也失败了,但是找不到错在哪 想请教先进问题在哪? 另外想请教想知道 Spring 怎麽实做的 比如 AOP,大概只知道 proxy pattern 但想知道诸如 @Aspect,@Before,<aop:config>... 如何爬原始码会比较好 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.36.74.251
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1485767090.A.F35.html
1F:→ ssccg: rollbackFor = Exception.class,但是你把例外全catch了? 01/31 03:03
感谢先进,把ex再抛出去就可以rollback了 之前找询相关的问题时,看到的都是设定上的问题 检查了很多次还是没有头绪 因为在练习 programmatic transaction management 当时的service都有try-catch,也就直接复制来用 没想到是exception被自己挡掉了 TransactionProxyFactoryBean 和 <tx:advice> 看来也是一样的问题 <tx:advice> 里面也是有指定rollback-for可以理解 TransactionProxyFactoryBean就不知道了,因为也只有设定 <property name="transactionAttributes"> <props> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> </props> </property> 自己在学Spring总觉得不是很踏实,很多设定背後如何实做没弄清楚 一有问题有时候就卡很久,有时也只是找到正确的设定方式,还是不知为什麽 Spring MVC是有追一下source code,从 DispatcherServlet 开始看 不过大概也只看了一成而已吧,还有很多待查 Spring AOP就有一种不知从何开始追的感觉 ※ 编辑: jtorngl (114.36.74.251), 01/31/2017 12:01:38







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP