applicationContext.xml 内配置:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
<!-- 事务拦截器 -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
        <property name="transactionManager"> 
            <ref bean="transactionManager" /> 
        </property>  
        <!-- 配置事务属性 --> 
        <property name="transactionAttributes"> 
           <props> 
              <prop key="delete*">PROPAGATION_REQUIRED</prop> 
              <prop key="add*">PROPAGATION_REQUIRED</prop> 
              <prop key="update*">PROPAGATION_REQUIRED</prop> 
              <prop key="save*">PROPAGATION_REQUIRED</prop> 
              <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
              <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
  <prop key="set*">PROPAGATION_REQUIRED</prop> 
          </props> 
       </property> 
     </bean> 
     <!-- 自动代理 -->
     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
        <property name="beanNames"> 
           <list> 
              <value>BizFlRecVouch</value> 
           </list> 
        </property> 
        <property name="interceptorNames"> 
           <list> 
              <value>transactionInterceptor</value> 
           </list> 
        </property> 
     </bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--自动生成表结构 <prop key="hibernate.hbm2ddl.auto">create</prop>-->
<!-- 是否在日志中输出的SQL 语句格式化成易读形式 -->
<prop key="hibernate.format_sql">true</prop> 
<!-- 是否显示统计形式,一般在测试阶段使用 -->
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/flsoft/hgjlserver/module/Code.hbm.xml</value>
<value>com/flsoft/hgjlserver/module/Customer.hbm.xml</value>
.........
</list>
</property>
</bean>
<bean id="BizFlRecVouch" class="com.flsoft.hgjlserver.serverImpl.BizFlRecVouchImpl">

<property name="isyssetDao">
<ref bean="FlSysSetDAO"/>
</property>
<property name="itaskDao">
<ref bean="TaskDAO"/>
</property>
</bean>interface IBizFlRecVouch :
public boolean test();
BizFlRecVouchImpl 实现:
public boolean test() {
Task task = new Task("9", "9");
itaskDao.save(task);
FlSysSet flsysSet = new FlSysSet();// 此处是错误的
isyssetDao.save(flsysSet);
return true;
}
//结果是 Task 仍然保存了,FlSysSet 错误,并没有回滚!
DAO: 两个DAO是一样的public class TaskDaoImpl extends HibernateDaoSupport implements ITaskDao {
public boolean save(Task task) {
// TODO Auto-generated method stub
//加tyr catch 又去掉了..
log.debug("saving Task instance");
Session session = getSession();
session.beginTransaction();
getHibernateTemplate().save(task);
session.beginTransaction().commit();
session.close();
log.debug("save successful");
return true;
}
}

解决方案 »

  1.   


    <bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory"> 
    <ref bean="sessionFactory" /> 
    </property> 
    </bean> 
     <bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
               abstract="true"> 
             <property name="transactionManager" ref="transactionManager"/> 
             <property name="proxyTargetClass" value="true"/> 
             <property name="transactionAttributes"> 
                 <props> 
                     <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> 
                     <prop key="count*">PROPAGATION_REQUIRED,readOnly</prop> 
                     <prop key="execute**">PROPAGATION_REQUIRED,readOnly</prop> 
                     <prop key="search*">PROPAGATION_REQUIRED,readOnly</prop> 
                     <prop key="preAdd*">PROPAGATION_REQUIRED,readOnly</prop> 
                     <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="listDel*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="app*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="check*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="tostack*">PROPAGATION_REQUIRED,-Exception</prop> 
                     <prop key="cancel*">PROPAGATION_REQUIRED,-Exception</prop> 
                 </props> 
             </property> 
         </bean> 
    <!-- 公司 -->
    <bean name="companyService" parent="baseTransaction">
    <property name="target">
    <ref bean="companyServiceTarget" />
    </property>
    </bean>
    <bean id="companyServiceTarget"
    class="com.service.impl.basic.CompanyServiceImpl"
    abstract="false" lazy-init="default" autowire="default"
    dependency-check="default">
    <property name="commonDao">
    <ref bean="commonDao" />
    </property>
    </bean>
      

  2.   

            <!-- 配置事务属性 --> 
            <property name="transactionAttributes"> 
               <props> 
                  <prop key="delete*">PROPAGATION_REQUIRED</prop> 
                  <prop key="add*">PROPAGATION_REQUIRED</prop> 
                  <prop key="update*">PROPAGATION_REQUIRED</prop> 
                  <prop key="save*">PROPAGATION_REQUIRED</prop> 
                  <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                  <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                  <prop key="set*">PROPAGATION_REQUIRED</prop> 
              </props> 
           </property> 
         </bean> 
    这里你配置事务回滚的时候就没有设定你以test开头能回滚。他切的是你的service方法,把你的Service方法改成addTask吧,要不再加个<prop key="*">PROPAGATION_REQUIRED</prop>,好像是这样,我也忘记了,你自己查查吧!
      

  3.   

    你既然用了spring transaction manager为什么还要手动管理transaction阿
      

  4.   


    这样试试吧
    <bean name="BizFlRecVouch" parent="transactionInterceptor">
            <property name="target">
                <ref bean="BizFlRecVouchTarget" />
            </property>
        </bean><bean id="BizFlRecVouchTarget" class="com.flsoft.hgjlserver.serverImpl.BizFlRecVouchImpl" abstract="false" lazy-init="default" autowire="default"
            dependency-check="default">>
            
            <property name="isyssetDao">
                <ref bean="FlSysSetDAO"/>
            </property>        
            <property name="itaskDao">
                <ref bean="TaskDAO"/>
            </property>
        </bean>
      

  5.   

    session.beginTransaction().commit();这里都提交了还回滚个啥啊
      

  6.   

    有这些session.beginTransaction().commit();它会报一些session不能打开还是关闭啊...
    把这儿的去掉,下面的异常它不回滚,如:itaskDao.update(new Task()); 操作一个空的对象时不回滚,
    用的不熟啊!!
    该怎么样设置,只要有异常就回滚,不管是什么异常;如果有什么异常还可以做自已的处理,还能加try catch 吗,在dao层?org.springframework.dao.InvalidDataAccessApiUsageException: The given object has a null identifier: com.cj.transaction.hibernate.Task; nested exception is org.hibernate.TransientObjectException: The given object has a null identifier: com.cj.transaction.hibernate.Task
    Caused by: org.hibernate.TransientObjectException: The given object has a null identifier: com.cj.transaction.hibernate.Task
      

  7.   

    1.事物控制我们一般在service曾进行处理。2. 在dao曾,当我们程序出错时,我们一般会抛出异常。3.如果要手动启动事物应该为public boolean save(Task task) {
                 String fullname="Dao::save"
            // TODO Auto-generated method stub
    //加tyr catch 又去掉了..
            log.debug("saving Task instance");
         try{
                Session session = getSession();
                session.beginTransaction();
                getHibernateTemplate().save(task);
                session.beginTransaction().commit();
                session.close();
                log.debug("save successful");
                return true;
           }catch(Exception e)
                {
                       log.error(fullname+e);
                       throw new DaoException(""+e);
                     session.beginTransaction().rollback();
                 }finally
                  {
                        if(session!=null)
                        {
                              session.close();
                        }
                   }
    {

        }
    }
      

  8.   

    没定义aop吧
     总要定义个 事物通知者