java - Spring 事务上下文包装?

标签 java spring hibernate

我正在使用 spring/hibernate。我有服务层和 DAO 层。事务放在服务层。 我有 FirstServiceImpl 类。在 FirstServiceImpl 类的一个方法中,它有 500 行代码。我所做的是将所有 500 行代码放在一个单独的类中(编写一个接口(interface)和实现类。在 FirstServiceImpl 类中,我 Autowiring 新创建的接口(interface)以访问其单一方法)。 我将 @Transactional 放在 FirstServiceImpl 类上。

我的问题是“新创建的类逻辑是否也在事务上下文中运行”?

谢谢!

最佳答案

我不确定,但我认为它会使用 transaction propagation .但我可以告诉您确定的方法来测试它是否有效。

在单个事务中通过您的代码执行以下数据库操作。

Insert/update one row into db, try to insert/update second row in the same transaction.

While inserting second row intentionally raise an exception. If you can see "first entry" in database, that means your transaction is not rolled back. And it does not work.

While if there is no first row entry in database, that means your transaction is rolled back. Here you may get UnexpectedRollbackException.

在测试之前确保你的注解是@Transactional(propagation=Propagation.REQUIRED)

关于java - Spring 事务上下文包装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10945810/

相关文章:

java - hibernate -Eclipse

java - 通用工厂方法和 Class<T> 参数

java - 使用反射获取字段的初始化值

mysql - hibernate中带注释的查询

java - 如何在Struts 2中将对象放入ThreadLocal中?

java - 找出由两个 3 位数字的乘积组成的最大回文

java - 如果代理关闭,则无法创建 Bean

hibernate - Grails/hibernate -通过两个子记录获取主数据的条件

eclipse - 如何在 JBoss AS 7.1.3 中添加更新版本的 Hibernate

java - 如何将对象从一个 JpaRepository 转换为另一个 JpaRepository