问题描述
在每个数据库查找方法上都具有TransactionAttributeType.NOT_SUPPORTED
是否有意义?如果不执行更新,我看不到附加实体的意义.
Does having TransactionAttributeType.NOT_SUPPORTED
on every DB lookup method makes sense? I don't see the point in having the entity attached if it's not going to execute an update.
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
pubic List<AnEntity> getListEntities(){
TypedQuery<AnEntity> query = em.createNamedQuery("AnEntity.myQuery",AnEntity.class);
return query.getResultList();
}
它是否仍会保留在缓存中?
Does it still end up in the cache?
使用REQUIRED
事务传播似乎唯一有用的时间是在需要更新时:
The only time it seems useful to use the REQUIRED
transcation propagation is when an update is required:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void editPersonName(Integer id, String newName){
AnEntity anEntity= em.find(AnEntity.class, id);
anEntity.setName(newName);
}
除此之外,我一点都不明白这一点.
Other than that I don't really see the point.
推荐答案
否,对只读查询使用事务传播没有任何意义,但是使用默认值在很大程度上是有意义的REQUIRED
事务传播.您还需要进行交易以读取数据. 数据库始终使用事务,无论是否您是否指定它.
No, it doesn't make sense to use the NOT_SUPPORTED
transaction propagation for read-only queries, but it makes a lot of sense to use the default REQUIRED
transaction propagation. You need a transaction for reading data too. The database always uses a transaction, no matter if you specify it or not.
使用显式事务可让您在单个事务中对多个语句进行分组,并且,如果您使用的是Hibernate,则可以避免攻击性连接释放开销.
Using an explicit transaction allows you to group several statements in a single transaction, and, if you are using Hibernate, you might avoid the aggressive connection release overhead.
因为JPA允许您无需事务就执行读取查询,但这并不意味着您必须这样做.
Just because JPA allows you to executed read queries without a transaction, it doesn't mean you have to do it this way.
NOT_SUPPORTED
模式在您要在当前事务范围之外执行服务方法时很有用,并且对于根本不需要事务(例如发送电子邮件)的方法很有用,从而避免了开销开始/结束事务上下文的过程.
The NOT_SUPPORTED
mode is useful when you want to execute a service method outside of the current transaction scope and that's useful for methods that don't need a transaction at all (e.g sending an email) so that you avoid the overhead of starting/ending a transaction context.
这篇关于TransactionAttributeType.NOT_SUPPORTED对于检索实体有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!