配置事务管理器,配置代理DAO,然后加入事务(对相应方法)。

解决方案 »

  1.   

    能否详细点。我的意思是同一段代码混合使用了hibernate与jdbc,如何配置事务管理器和dao?
    比如下段代码:
    testDAO.update(table1);//No1
    sql="UPDATE table2 SET filed1='1' WHERE type='U'";
    statement.executeUpdate(sql);//No2
    如果No2处理失败,该如何把No1的数据也回滚?
      

  2.   

    一样的,因为它是针对整个方法,给个简单的例子看看
    http://www.blogjava.net/oksonic/archive/2005/11/06/18370.html
      

  3.   

    谢谢zhh1981(**的猪头) 可为什么会出现这个错误?Bean property 'transactionManager' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
      

  4.   

    你的
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
       <ref local="sessionFactory" />
      </property>
     </bean>
      <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
      <property name="target">
       <ref local="userDAO" />
      </property>
      <property name="transactionAttributes">
       <props>
        <prop key="insert*">PROPAGATION_REQUIRED</prop>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
      </property>
    类似以上的是怎么定义的,你的
    <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
    对应的注入类型不正确
      

  5.   

    如上配置,故意写个错的sql语句,可始终无法让这个错误sql语句之前的事务也回滚。
      

  6.   

    public void fun(){
    testDAO.update(table1);//No1
    ....
    Connection conn=...;
    sql="UPDATE table2 SET filed1='1' WHERE type='U'";
    try{
    statement.executeUpdate(sql);//No2
    conn.commit();
    }catch(SQLException e){
     conn.rollback();
     throw new RuntimeException(e);
    }
    }
      

  7.   

    或者这样更好public void fun(){
     testDAO.update(table1);//No1
     SessionFactory sf=..;
     sql="UPDATE table2 SET filed1='1' WHERE type='U'";
     new HibernateTemplate(sf).execute(new HibernateCallback(){
        public Object doInHibernate(Session session) {
           Connection conn=session.connection();
           //你的sql语句
        }
    });