需要抛出RuntimeException spring的事务才能回滚

解决方案 »

  1.   

     我在DAO里面抛了throw new RuntimeException();但事物还是没回滚.下面是DAO里面的save方法
    try{
                connection = getConnection();
                statement = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
                int result = statement.executeUpdate( insertSql );
                if( result == 0 ){
                    id = null;
                }else{
                 throw new RuntimeException();
                }
            }catch( Exception e ){
             e.printStackTrace();
             id = null;
            }
      

  2.   

     int result = statement.executeUpdate( insertSql ); 这不是插一条数据到数据库吗? 然后我去数据库看了下着条数据存在啊
      

  3.   

    <!-- 定义事务拦截器bean --> 中加上试试
    <property name="target">
    <ref bean="xxxDao" />
    </property>
      

  4.   

    你这样是针对一个DAO做申明式事物,我要做的是对所有的DAO做申明式事物.我也试了下加进去了就会出错.
      

  5.   

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <!-- 注册事务管理器 -->
    <bean id="transactionManager" autowire="byName"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager" />

    <tx:advice id="txAdvice" 
    transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*"/>
    <tx:method name="delete*"/>
    <tx:method name="*" read-only="true"/>
    </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="true">
    <aop:advisor advice-ref="txAdvice"
    pointcut="execution(* com.manager.dao.*.*(..))"/>
    </aop:config>



    </beans>
      

  6.   


    <!-- 定义一个BeanPostProcessor Bean --> 
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
    <property name="beanNames"> 
    <value>*DAO </value> 
    </property> 
    <property name="interceptorNames"> 
    <list> 
    <value>transactionInterceptor </value> 
    </list> 
    </property> 
    </bean> 
    修改下
    <!-- 定义一个BeanPostProcessor Bean --> 
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
    <property name="beanNames"> 
    <list>
    <value>*Dao</value>
    <!--   此处可增加其他需要自动创建事务代理的bean-->
    </list>
    </property> 
    <property name="interceptorNames"> 
    <list> 
    <value>transactionInterceptor </value>
    <!-- 此处可增加其他新的Interceptor --> 
    </list> 
    </property> 
    </bean> 还有你所有的DAO 要在XML文件中定义配置 如<!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理-->
    <bean id="xxxxDao" class="pkg.classname">
            <property name="objectnamexx">objectxxxx</property>
    </bean>
     
      

  7.   

    <!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理--> 
    <bean id="xxxxDao" class="pkg.classname"> 
            <property name="objectnamexx">objectxxxx </property> 
    </bean> 请问 <property name="objectnamexx">objectxxxx </property> 这 是做什么用
      

  8.   


    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
    singleton="true">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>
             </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="sessionFactory"> 
        <ref local="sessionFactory"> </ref> 
        </property>  <!-- 定义业务逻辑处理组件 -->
        <bean id="xxxDao" class="class.dao/>
           <propertye name="sessionFactory" ref="sessionFactory">
       </bean>
     <!-- 定义BeanNameAutoProxyCreator -->
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <!-- 指定对满足哪些Bean name的Bean自动生成业务代理 -->
        <property name="beanNames">
                <!-- 下面是所有需要自动创建事务代理的Bean-->
                <list>
    <value>xxxDao</value>
                </list>
             </property>
            <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
            <property name="interceptorNames">
                <list>
                    <value>transactionInterceptor</value>
                 </list>
            </property>
        </bean>                
      

  9.   

      上面的配置 我已经验证了在save方法里面已经切入了事物控制,但就是不回滚事物
      

  10.   

    如果你的SAVE方法能够把数据持久化到数据库主话SPRING的AOP起到作用了,不能回滚可能是其它原因
      

  11.   

      楼上的兄弟我没有用hibernate
      

  12.   

    郁闷 搞了半天原来我没有在save方法里开启事物,connection.setAutoCommit( false ).这样就ok了.不知道是我配错了还是spring也没自动开启事物,
      

  13.   

       郁闷 我上面没用spring开启的事物,不好意思。spring配置了事物该怎么编码呢?
      

  14.   

    我在save方法里面transactionManager.getTransaction( new DefaultTransactionDefinition() ).isNewTransaction();结果是false,这说明spring已经在save方法里面切入了事物吧。
      

  15.   

    个人喜欢使用annotation,
    配置文件如下:<?xml version="1.0" encoding="utf-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <!-- 数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
            <property name="url" value="jdbc:jtds:sqlserver://10.81.64.43:1433/tiannet;charset=gbk"/>
            <property name="username" value="sa"/>
            <property name="password" value="sa"/>
        </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
    <value>
    hibernate.dialect=org.hibernate.dialect.SQLServerDialect
    hibernate.show.sql=true
    </value>
    </property>
    <property name="mappingLocations">
    <list>
    <value>classpath:spring/hibernate/User.hbm.xml</value>
    </list>
    </property>
    </bean>

    <!-- 事务管理 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean> <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

    <bean id="myTarget" class="spring.hibernate.Commons">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    </beans>            
    java代码如下:package spring.hibernate;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.transaction.annotation.Transactional;
    public class Commons  extends HibernateDaoSupport {
           /**
     * 更新某一字段的操作。并使用事务管理。
     */
    @Transactional
    public void updateField() {
    getHibernateTemplate().bulkUpdate("update spring.User set trueName = '管理员4' where userId = 1 ");
    //有意制造异常
    getHibernateTemplate().bulkUpdate("update spring.User2 set trueName = '管理员4' where userId = 1 ");

    }       public static void main(String[] args) {
    // TODO Auto-generated method stub
    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("spring/hibernate/hibernate.xml");
    Commons um = (Commons)appContext.getBean("myTarget");
    um.updateField();

    }
    }
      

  16.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主截止到2008-06-17 20:15:36的汇总数据:
    发帖数:1
    结贴数:0
    结贴率: 0.00%
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html