下面是我的spring 事务的配置 <!-- AOP事务管理 -->
<bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager" >  
        <property name="sessionFactory">  
           <ref bean="sessionFactory" />  
        </property>  
    </bean><!-- 拦截事务Beans设置--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" lazy-init="true">
        <property name="transactionManager" ref="transactionManager" /> 
        <property name="transactionAttributes">                 
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="exec*">PROPAGATION_REQUIRED,-Exception</prop>
         <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> 
</props>
        </property>  
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>friendMger</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>   
///////////////////////////////////////////////////////////////////////////////////////////////////
 <bean id="friendMger" class="com.friend.service.impl.FriendManagerImpl" >
<property name="friendDao" ref="friendDao"></property>
</bean>
<bean id="friendDao" class="com.oooo3d.hizone.friend.dao.impl.FriendDaoImpl">
     <property name="hibernateTemplate" ref="hibernateTemplate"></property>
     <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>现在我在service中
public class FriendManagerImpl implements FriendManager {
 
private FriendDao friendDao;
public FriendDao getFriendDao() {
return friendDao;
}
public void setFriendDao(FriendDao friendDao) {
this.friendDao = friendDao;
}
public void addFollowingUsers(String currentusrID, String accnt) {

Friend friendtest=new Friend();
friendtest.setFollowSerial(11);
friendDao.addUsers(friendtest);
                  friendDao.updateUsers(friendtest);
//现在这个保存和更新操作是要同时操作的,如果有一个失败 要做回滚。但是现在我保存方法成功了,更新操作不成功,但是数据持久化到数据库中了。请问是什么问题?
}

解决方案 »

  1.   

    楼主,你的问题出在自动代理处,没写对,或者叫你的service没有与此处的配置对应上<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
    <value>friendMger</value>  
    </property>
    <property name="interceptorNames">
    <list>
    <value>transactionInterceptor</value>
    </list>
    </property>
    </bean>   
     上面红色颜色处导致你的处理不能属于同一个事务;
    <value>friendMger</value>  这个地方一般都会配置成*XXXX,
    然后写的service或service接口就要对应*XXXX 这种形式;
    如:我配置<value>*Service</value>,那我写的所有service接口,都以这中格式命名,
     如 FriendManagerService,这样, 这个service才能被spring管理到,
    然后上面的那些什么<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>才会被应用上。楼主试试吧,多看点资料!