找到你要的答案

Q:Spring Transactional rollbackFor not working

Q:春季交易rollbackfor不工作

I have a method to add a item to table and to test exception My item is stored into table. Exception is raised, but Transaction was not rollback My method

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, 
        rollbackFor={java.lang.Exception.class, java.lang.RuntimeException.class})
public MyBean addItem(MyBean bean, Integer iduser) throws Exception  {

    NalogBean nb = txTemplate.execute(new TransactionCallback<MyBean>() {

        @Override
        public MyBean doInTransaction(TransactionStatus txStatus) {
            MyBean nbl = null;
            try {
                sessionFactory.getCurrentSession().saveOrUpdate(bean);
                throw new RuntimeException("Exception throwed!");

            } catch (Exception e) {
                txStatus.setRollbackOnly();
            }
            return nbl;
        }
    });

    return nb;          
}

My servlet-context is standard

<tx:annotation-driven transaction-manager="transactionManager"/>

<beans:bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="${database.driver}" />
    <beans:property name="url" value="${database.url}" />
    <beans:property name="username" value="${database.user}" />
    <beans:property name="password" value="${database.password}" />
</beans:bean>


<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"></beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
        </beans:props>
    </beans:property>
    <beans:property name="packagesToScan" value="rs.co"></beans:property>


    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>rs.co.mfp.klijent.finansije.model.NalogModel</beans:value>
        </beans:list>
    </beans:property>

</beans:bean>


<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>

and when I call addItem it store bean into table. Also I try without txTemplate, just sessionFactory.getCurrentSession().saveOrUpdate(bean); throw new RuntimeException("Exception throwed!"); but the same result. Item was stored into table. Any idea?

I have a method to add a item to table and to test exception My item is stored into table. Exception is raised, but Transaction was not rollback My method

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, 
        rollbackFor={java.lang.Exception.class, java.lang.RuntimeException.class})
public MyBean addItem(MyBean bean, Integer iduser) throws Exception  {

    NalogBean nb = txTemplate.execute(new TransactionCallback<MyBean>() {

        @Override
        public MyBean doInTransaction(TransactionStatus txStatus) {
            MyBean nbl = null;
            try {
                sessionFactory.getCurrentSession().saveOrUpdate(bean);
                throw new RuntimeException("Exception throwed!");

            } catch (Exception e) {
                txStatus.setRollbackOnly();
            }
            return nbl;
        }
    });

    return nb;          
}

我是标准的servlet上下文

<tx:annotation-driven transaction-manager="transactionManager"/>

<beans:bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="${database.driver}" />
    <beans:property name="url" value="${database.url}" />
    <beans:property name="username" value="${database.user}" />
    <beans:property name="password" value="${database.password}" />
</beans:bean>


<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"></beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
        </beans:props>
    </beans:property>
    <beans:property name="packagesToScan" value="rs.co"></beans:property>


    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>rs.co.mfp.klijent.finansije.model.NalogModel</beans:value>
        </beans:list>
    </beans:property>

</beans:bean>


<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>

and when I call addItem it store bean into table. Also I try without txTemplate, just sessionFactory.getCurrentSession().saveOrUpdate(bean); throw new RuntimeException("Exception throwed!"); but the same result. Item was stored into table. Any idea?

answer1: 回答1:

when you use Transaction annotation in above of method,dont need to using txStatus.

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, 
        rollbackFor={java.lang.Exception.class, java.lang.RuntimeException.class})
public MyBeanId addItem(MyBean bean, Integer iduser) throws Exception  {

               return sessionFactory.getCurrentSession().saveOrUpdate(bean);

}

when be raised exception ,rollback transaction automatically i hope to help you

当你使用以上交易标注的方法,不需要使用txstatus。

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, 
        rollbackFor={java.lang.Exception.class, java.lang.RuntimeException.class})
public MyBeanId addItem(MyBean bean, Integer iduser) throws Exception  {

               return sessionFactory.getCurrentSession().saveOrUpdate(bean);

}

when be raised exception ,rollback transaction automatically i hope to help you

spring  spring-mvc  transactional