如果方法不保留任何实体,那么制作 TransactionAttributeType.NOT_SUPPORTED 是否好

Is it good to make TransactionAttributeType.NOT_SUPPORTED if method doesn't persist any entity

如果我有仅从数据库中获取数据的嵌套 bean 方法。 (即 GET API)。那么将所有 bean 方法标记为 TransactionAttributeType.NOT_SUPPORTED 是否有益?它是否有助于提高性能,因为 JTA 不为此管理任何事务?

这正是使用 NOT_SUPPORTED 的目的,以提高性能。事实上,如 Oracle 所述:

NotSupported Attribute

If the client is running within a transaction and invokes the enterprise bean’s method, the container suspends the client’s transaction before invoking the method. After the method has completed, the container resumes the client’s transaction.

If the client is not associated with a transaction, the container does not start a new transaction before running the method.

Use the NotSupported attribute for methods that don’t need transactions. Because transactions involve overhead, this attribute may improve performance.

因此,它非常适合所有 selectfind 业务方法,其目的可能是在屏幕上填写数据table。

NOT_SUPPORTED 如果使用事务上下文调用会导致异常的处理,则很有用。例如,在 XA 处理的上下文中调用包含 DDL 代码的存储过程将导致异常发生。如果更改存储过程不是一个选项,请使用 NOT_SUPPORTED 属性作为解决方法,并在调用包含有问题的存储过程的方法之前暂停事务。

如果在只读事务中允许事务回滚使用 SUPPORTS,如果在只读事务中不允许事务回滚使用 NOT_SUPPORTED.

这篇文章说:"No, it doesn't make sense to use the NOT_SUPPORTED transaction propagation for read-only queries"。作者是 JPA 专家 Vlad Mihalcea。