java - 在 View 中打开 session 与@Transactional

标签 java spring hibernate transactions open-session-in-view

我一直在我的服务层中使用 @Transactional 注释。但要解决由于延迟加载 View 而导致的错误,我必须使用 Open Session in View Filter。此后,在不使用@Transaction 本身的情况下,将打开 session 并开始事务。那么这是否意味着不需要@Transactions?那么在服务层中将如何处理事务和回滚?

最佳答案

The javadoc解释一下:

This filter makes Hibernate Sessions available via the current thread, which will be autodetected by transaction managers. It is suitable for service layer transactions via HibernateTransactionManager or JtaTransactionManager as well as for non-transactional execution (if configured appropriately).

NOTE: This filter will by default not flush the Hibernate Session, with the flush mode set to FlushMode.NEVER. It assumes to be used in combination with service layer transactions that care for the flushing: The active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction. If you intend to use this filter without transactions, consider changing the default flush mode (through the "flushMode" property).

关于java - 在 View 中打开 session 与@Transactional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16465269/

相关文章:

java - 无法使用 libgdx 框架在 Android 中的 facebook 墙上发布游戏分数

java - mysql 小数(10,2) 提取java

java - 如何在 Android 中获取 volley JSONArray

java - 如何防止单例 bean 被 ApplicationContext 刷新破坏

java - org.springframework.dao.InvalidDataAccessApiUsageException : Write operations are not allowed in read-only mode (FlushMode. 手册)

java - 在 JPA 中,哪种类型的参数最好使用 "positional/named"?

java - 有没有更有效的方法来计算每个连续字母在字符串中出现的次数? (java)

java - Spring——解耦与性能

java - 在 @Controller 方法上使用 @Before 方面与 Spring 不起作用

java - 使用 Hibernate 的高 GC 负载