简体   繁体   English

播放 2.5 - @Transactional readOnly 被忽略了?

[英]Play 2.5 - @Transactional readOnly ignored?

I have a Java Play 2.5 application, and I have a transaction marked as readonly (at the Controller level):我有一个 Java Play 2.5 应用程序,并且有一个事务标记为只读(在控制器级别):

    @Transactional(readOnly = true)
    public Result list() { ...

(this is the play.db.jpa.Transactional annotation). (这是 play.db.jpa.Transactional 注释)。

So, if I turn on logging for hibernate transaction:因此,如果我为休眠事务打开日志记录:

 <logger name="org.hibernate.engine.transaction" level="TRACE"/>

Then I see the transaction being made and being committed.然后我看到交易正在进行并被提交。

019-12-03 23:42:23,754 [debug] from org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator in main - No JtaPlatform was specified, checking resolver
2019-12-03 23:42:23,755 [debug] from org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformResolverInitiator in main - No JtaPlatformResolver was specified, using default [org.hibernate.engine.transaction.jta.platform.internal.StandardJtaPlatformResolver]
2019-12-03 23:42:23,766 [debug] from org.hibernate.engine.transaction.jta.platform.internal.StandardJtaPlatformResolver in main - Could not resolve JtaPlatform, using default [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2019-12-03 23:58:46,769 [debug] from org.hibernate.engine.transaction.internal.TransactionImpl in application-akka.actor.default-dispatcher-5 - begin
2019-12-03 23:58:46,878 [error] from application in application-akka.actor.default-dispatcher-5 - Transaction: org.hibernate.engine.transaction.internal.TransactionImpl@3d505ddd[transactionCoordinator=org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl@89a1241,exceptionConverter=org.hibernate.internal.ExceptionConverterImpl@76f0443c,transactionDriverControl=org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl@49f1518a]
2019-12-03 23:59:27,638 [debug] from org.hibernate.engine.transaction.internal.TransactionImpl in application-akka.actor.default-dispatcher-5 - committing

However, I have one problem - it's not read-only!但是,我有一个问题——它不是只读的! I've added some code that modifies an object (obj.setField("changed value")), and it's saved into the database at the end of the transaction!我添加了一些修改对象的代码(obj.setField("changed value")),它在事务结束时保存到数据库中!

I was expecting an exception telling me that the transaction is read-only and that it cannot be committed!我期待一个异常告诉我该事务是只读的并且它不能被提交!

  1. Why it the transaction not read-only?为什么它的事务不是只读的?
  2. Can I somehow see in logs if Hibernate creates the transaction readonly or not?如果 Hibernate 以只读方式创建事务,我可以以某种方式在日志中看到吗?

Other info: Java Play 2.5.10, Hibernate 5 (5.2.6), and PostgreSQL as the DB.其他信息:Java Play 2.5.10、Hibernate 5 (5.2.6) 和 PostgreSQL 作为数据库。

So, it seems that it was because the controller class itself was annotated with @Transactional, and the method with @Transactional(readOnly=true).所以,似乎是因为控制器类本身用@Transactional 注释,而方法用@Transactional(readOnly=true)。

Somehow, the class annotation overrides the method annotation.不知何故,类注释覆盖了方法注释。 This doesn't seem logical to me, but in practice, that seems to be the case: as soon as I've removed the @Transactional from the class level, it worked just fine.这对我来说似乎不合逻辑,但在实践中,情况似乎是这样:一旦我从类级别删除了 @Transactional,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM