浏览 9734 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-01-16
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref bean="transactionManager"/></property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="personManager" parent="txProxyTemplate"> <property name="target"> <bean class="com.jsgm.jsqts.equipment.service.impl.PersonManagerImp"> <property name="personDAO"><ref bean="personDAO"/></property> </bean> </property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> 此时是不是personManager这个实例所执行的save开头的方法都带有事务特性呢?测试过好像不行。只能在DAO里面这样写,测试出事务的回滚。究竟Spring+Hibernate 怎么做事务呢???非常疑惑。 public void savePersonTest(){ List obj = (List) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException,SQLException { Person person=new Person(); person.setIdcard("AAAAAAA"); person.setPersonNm("adsfasdf"); person.setAreaId("302000"); person.setOpStatus("1"); person.setUpTime(new Date()); person.setExchgFlag("1"); person.setMtUnit("3"); person.setInsertUnit("3"); Person person1=new Person(); person1.setIdcard("BBB"); person1.setPersonNm("adsfasdf"); person1.setAreaId("302000"); person1.setOpStatus("1"); person1.setUpTime(new Date()); person1.setExchgFlag("1"); person1.setMtUnit("3"); person1.setInsertUnit("3"); session.save(person); if (1==1){ //强行抛出错误,让事务回滚 throw new SQLException(); } session.save(person1); return null; } }); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2005-01-17
这是我写的一篇文章
http://blog.csdn.net/yzhz/archive/2005/01/17/springqa.aspx 第六个问题的答案可以解答你 |
|
返回顶楼 | |
发表时间:2005-01-22
session.save(person);
session.flush(); |
|
返回顶楼 | |
发表时间:2005-02-25
你的这种事务应用的方法是错误的
1、你把事务管理放到了dao层是个错误,dao层不涉及事务方面的东东,你现在的做法的结果是Spring管理的是你的personDAO的事务,在你的测试用例里Spring是对personDAO的save方法进行事务管理,你添加一个person,如果不抛出runtimeexception,事务就会提交,你调用两次save方法就是两次事务,两者是分开的,当然不会回滚 2、事务管理应该放在服务层或业务层,比如说在服务层有个save方法,这个方法里面可以调用多个dao操作,事务管理应该加在服务层对象上。 3、另外要注意的是:你在调用服务层方法时,注入的参数bean应该是动态代理bean |
|
返回顶楼 | |