问题描述:

According to the Play! docs, it automatically creates a transaction for every request whether you want it or not:

Play will automatically manage transactions for you. It will start a transaction for each HTTP request and commit it when the HTTP response is sent. If your code throws an exception, the transaction will automatically rollback.

https://www.playframework.com/documentation/1.3.x/jpa

Given that, what is the point of the @Transactional annotation? It doesn't turn on a transaction, as the docs indicate transactions are already on.

The docs for the later Play! framework (2.5) seem to contradict this statement, saying that without @Transactional your JPA method calls will violate the requirement that they all occur within a transaction:

Every JPA call must be done in a transaction so, to enable JPA for a particular action, annotate it with @play.db.jpa.Transactional.

https://www.playframework.com/documentation/2.5.x/JavaJPA

@Transactional

public Result index() {

return ok("A Transactional action");

}

Which is it? Does your method, if called from an HTTP context not run in a transaction unless that annotation is used?

And if you change the transaction isolation level (in conf), does that mean every single call will be in that level transaction, or only @Transactional methods?

网友答案:

You are mixing two very different Play versions (like Angular 1 and 2). For example, Play 1.x is built on Java while Play 2.x is built on Scala and provides Scala+Java API's. So there is no contradictions in the docs, I think.

In Play 1:

  • Transactions are managed automatically
  • You can use annotations to change this: ex @play.db.jpa.NoTransaction to disable the transaction

In Play 2:

  • No "automatic" transactions;
  • You have to use @Transactional to get "automatic" transactions (the default behavior in Play 1)

Regarding the "transaction isolation level (in conf)", I can't say anything because I have never used it (can you show an example?), but I guess it will apply that isolation level to every action annotated with @Transactional or other JPA methods (ex JPAApi.withTransaction)

相关阅读:
Top