- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我将我正在开发的应用程序从使用 AspectJ 加载时间编织切换到使用 Spring CGlib 代理,并且在我这样做之后,我开始在代码的许多部分中获得 hibernate 延迟加载异常,而在过去有没有抛出异常。
我已经能够通过将 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
添加到之前没有任何事务属性的公共(public)方法中来解决这些延迟加载异常但调用 spring 存储库从数据库中读取数据。
任何人都知道为什么添加 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
会消除 hibernate 延迟加载异常,以及为什么 AspectJ 加载时间编织不需要这些注释但需要出来了吗?
更新 2我认为删除 AspectJ 不是问题,但问题是我并不真正了解 SUPPORTS 传播的实际行为。特别是 SUPPORTS 如何与 JPA EntityManager 交互,因此我删除了一堆导致延迟加载异常的 SUPPORTS 传播。在阅读了 Spring Transaction Manager 的源代码之后,该做什么就变得很清楚了。 Spring 文档并没有很好地指出的关键思想是 @Transactional 注释用作将 EntityManager 的生命周期与事务方法的开始和结束联系起来的同步点。也强烈推荐http://www.ibm.com/developerworks/java/library/j-ts1/上的这一系列文章和这篇博文 http://doanduyhai.wordpress.com/2011/11/21/spring-persistencecontext-explained/
更新 1
这不是通过 AOP 代理调用私有(private) @Transactional 方法的情况。这些问题发生在从其他服务调用的公共(public)方法中。
这是代码结构的示例,我在其中看到了问题的发生。
@Service
public class FooService
{
@Autowired
private BarService barService;
public void someMethodThatOnlyReads() {
SomeResult result = this.barService.anotherMethodThatOnlyReads()
// the following line blows up with a HibernateLazyLoadingEcxeption
// unless there is a @Transactional supports annotation on this method
result.getEntity().followSomeRelationship();
}
}
@Service
public class BarService
{
@Autowired
private BarRepository barRepo;
public SomeResult anotherMethodThatOnlyReads()
{
SomeEntity entity = this.barRepo.findSomeEntity(1123);
SomeResult result = new SomeResult();
result.setEntity(entity);
return result;
}
}
@Repository
public class BarRepository
{
@PersistenceContext
private EntityManager em;
public SomeEntity findSomeEntity(id Integer)
{
em.find(SomeEntity.class,id);
}
}
最佳答案
我假设您的代码没有使用 OpenSessionInViewFilter
或类似的东西。
没有 @Transactional
注解,Hibernate session 在离开 BarRepository.findSomeEntity()
后关闭方法。
当一个 @Transactional
方法被调用,TransactionalInterceptor
正确绑定(bind)到方法(通过 cglib 代理或您在 Spring 上下文中拥有的任何其他 AOP 配置),然后 Spring 为整个带注释的方法保持打开状态,从而防止任何延迟加载异常。
如果您将日志记录设为 DEBUG
在 org.springframework.transaction
和 org.springframework.orm.hibernate3
(或 hibernate4
如果您使用的是 Hibernate 4)记录器,尤其是 HibernateTransactionManager
类(class)和org.springframework.transaction.support.AbstractPlatformTransactionManager
,您应该确切地看到 Spring 在代码流中的哪些点决定它需要打开和关闭 Hibernate Session。日志还应显示 session 或事务在每个点打开/关闭的原因。
关于java - 为什么 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 修复 Hibernate 延迟加载异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16062893/
我收到多行错误,指出它有重复的类 Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCom
我的项目昨天运行良好,今天却出现以下错误。请帮忙。 出了什么问题:无法解析配置“:app:debugCompileClasspath”的所有文件。 Could not find support-v4.
错误:配置项目“:app”时出现问题。 Could not find support-v4.jar (com.android.support:support-v4:24.0.0). Searched
我正在使用 react-native-webrtc 运行示例应用程序模块。我正在尝试在 android 中运行该应用程序,但出现这样的错误... * What went wrong: A probl
错误:配置项目“:app”时出现问题。 Could not find support-v4.jar (com.android.support:support-v4:24.0.0). Searched
我克隆了我的项目并使用 Android studio 打开,但构建失败并出现以下错误并且同一个项目在其他项目上运行良好。 出了什么问题:无法解析配置“:app:flavorUnsignedReleas
我正在运行示例应用程序......我正在使用 this模块.....但是我收到这样的错误... FAILURE: Build failed with an exception. * What went
我有一个 Ionic 项目,在升级到 OSx Mojave 后,出于某种原因不再构建。 该项目基于 Ionic 3 构建。 我有 Cordova CLI 7.0.0 当我运行时 ionic cordo
我有一个 Ionic 项目,在升级到 OSx Mojave 后,出于某种原因不再构建。 该项目基于 Ionic 3 构建。 我有 Cordova CLI 7.0.0 当我运行时 ionic cordo
添加 Android 平台(ionic cordova platform add android)后,我构建了我的 Ionic 项目(ionic cordova build android),但出现错
这个问题在这里已经有了答案: Manifest merger failed : Attribute application@appComponentFactory - Androidx (14 个答
关于我的 Gradle 文件中的以下“complie”: dependencies { compile 'com.android.support:support-v4:25.3.1' }
有些困惑。 我想更频繁地使用@supports 但是...我不关心浏览器如何看待它。希望您能说清楚,如果可以的话谢谢您。 如果浏览器本身不理解@supports,它如何计算@support 'not'
我刚刚升级到 Dart 2 和最新版本的 Flutter,现在我无法构建我的应用程序。我在互联网上环顾四周,但仍然不明白为什么会发生这种情况。 我得到的错误是: FAILURE: Build fail
我正在学习 Firebase,但由于以下错误而陷入困境: Error:Failed to resolve: com.android.support:customtabs:25.4.0 Error:Fa
我在构建本周时出于某种原因尝试使用Cordova进行项目时遇到问题: cordova build android gradle获取com.android.support:support alpha而不
对应的androidx是什么com.android.support:support-compat 的图书馆实现push notifications ? 他们说here它是 androidx 的一部分,
我正在尝试为我的应用设置插桩单元测试。并且我已根据以下开发者站点链接添加了依赖项。 https://developer.android.com/training/testing/unit-testin
当我想使用 Proguard 规则生成签名的 APK(发布)时,我收到了以下错误消息: Cannot find a version of 'com.android.support:support-an
这个问题在这里已经有了答案: Failed to resolve: com.android.support:appcompat-v7:26.0.0 (14 个答案) Failed to resolv
我是一名优秀的程序员,十分优秀!