首页 新闻 赞助 找找看

我在spring设置了事务管理,但是对于需要使用事务的方法,如果不手动开启事务,这个方法根本不执行,如session.delete().,也就是数据根本没有删除。加上了手动开启、提交事务就可以。

0
悬赏园豆:20 [待解决问题]

代码如下:  
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager" 


class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 通知 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
  <tx:attributes>


   <tx:method name="save*" propagation="REQUIRED"/>
   <tx:method name="update*" propagation="REQUIRED"/>
   <tx:method name="delete*" propagation="REQUIRED"/>
   <tx:method name="*" read-only="true"/>
  </tx:attributes>
</tx:advice>


<aop:config>
  <aop:pointcut id="all" expression="execution(* com.cstp.service.*.*


(..))"/>
  <!-- 加入切入点,和通知配置 -->
  <aop:advisor advice-ref="txadvice" pointcut-ref="all"/>
</aop:config>
<!-- dao -->
<bean id="user1DAO" class="com.cstp.impl.User1Impl">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- service -->
<bean id="user1Service" class="com.cstp.service.User1ServiceImpl">
  <property name="user1DAO" ref="user1DAO"/>
</bean>

<!-- action -->
<bean id="user1Action" class="com.cstp.action.User1Action">
  <property name="user1Service">
   <ref bean="user1Service"/>
  </property>
</bean>
</beans>



public class User1ServiceImpl implements User1Service{
//xml文件中的依赖注入,property="user1DAO"
private User1DAO user1DAO;

public boolean deleteUser(String userid) {
  return user1DAO.deleteUser(userid);
}
}

public class User1Impl extends HibernateDaoSupport implements User1DAO{

public boolean deleteUser(String userid){
  Session session=null;
  //Transaction tr=null;
  try {
session=this.getHibernateTemplate().getSessionFactory().openSession();
   //tr=session.beginTransaction();


   User1 user=(User1) session.get(User1.class, userid);
   session.delete(user);
   //tr.commit();
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   /*if(tr!=null){
    tr.rollback();
   }*/
   return false;
  }finally{
   if(session!=null){
    session.close();
   }
  }
}


省略了接口,serivce接口调用实现类,实现类中有调用了user的接口,从而调用到实现类

lovely9862的主页 lovely9862 | 初学一级 | 园豆:174
提问于:2013-01-31 23:23
< >
分享
所有回答(2)
0

<aop:pointcut id="all" expression="execution(* com.cstp.service.*.* (..))"/>,感觉问题就出在那句。你试试这句

<aop:pointcut id="all" expression="execution(* com.cstp.impl..*.* (..))"/>

cel | 园豆:380 (菜鸟二级) | 2013-05-09 22:54
0

<aop:pointcut id="all" expression="execution(* com.cstp.service..*.* 

(..))"/> 

应该是少了个点吧

hu_ge | 园豆:202 (菜鸟二级) | 2013-05-11 14:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册