English 中文(简体)
Annotation @transactional. 如何退缩?
原标题:Annotation @Transactional. How to rollback?

I used this annotation successfully for a Dao class. And rollback works for tests.

But now I need to rollback real code, not just tests. There are special annotations for use in tests. But which annotations are for non-test code? It is a big question for me. I spent a day for that already. The official documentation did not meet my needs.

class MyClass { // this does not make rollback! And record appears in DB.
        EmployeeDaoInterface employeeDao;

        public MyClass() {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "HibernateDaoBeans.xml" });
            employeeDao = (IEmployeeDao) context.getBean("employeeDao");
         }

        @Transactional(rollbackFor={Exception.class})
    public void doInsert( Employee newEmp ) throws Exception {
        employeeDao.insertEmployee(newEmp);
        throw new RuntimeException();
    }
}

employeeDao is

@Transactional
public class EmployeeDao implements IEmployeeDao {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void insertEmployee(Employee emp) {
        sessionFactory.getCurrentSession().save(emp);
    }
}

这里是说明很好地发挥作用的一个考验:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/HibernateDaoBeans.xml" })
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
@Transactional
public class EmployeeDaoTest {

    @Autowired
    EmployeeDaoInterface empDao;

    @Test
    public void insert_record() {
       ...
       assertTrue(empDao.insertEmployee(newEmp));
    }

HibernateDaoBeans.xml

   ...
<bean id="employeeDao" class="Hibernate.EmployeeDao">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
    <tx:annotation-driven transaction-manager="txManager"/>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
   ...



**YES,我转回交易。 我刚刚为这项服务增添了别名。

<bean id="service" class="main.MyClass">
    <property name="employeeDao" ref="employeeDao" />
</bean>

感谢大家, 俄罗斯不会忘记你们!

问题回答

仅从<代码>@Transactional上标出的任何<代码>。

违约时,所有<代码>RuntimeException均进行回扣交易,而核对的例外情况则不发生。 这是EJB遗产。 可以通过使用<条码>反馈(<>>>/代码>和<代码>noRollbackFor()说明参数来加以核对:

@Transactional(rollbackFor=Exception.class)

This will rollback transaction after throwing any exception.

或方案

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

你可以从你希望推出的方法中得出一个不受约束的例外。 将在春天发现这一点,你的交易将只记为滚动。

我假设你在此重新使用春天。 我假设你在测试中提到的说明是基于春季测试的说明。

建议向《春季框架》提供交易基础设施,表明交易工作将停止,其方式是放弃目前在交易中实施的法典的例外。

注意到:

请注意,《春天框架》的交易基础设施代码在违约时只标明交易在时间过长、无节制例外的情况下进行收回;也就是说,如果被抛出的例外情况是持平时视的事例或次等。

让我退席 因为还不够,所以我不得不这样做,并且按预期行事:

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)

我希望这有助于:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签