简体   繁体   English

为什么Spring的@Transactional有帮助表现

[英]Why Does Spring's @Transactional Help Performance

I have a method that can potentially make a lot of calls to a database when validating a list of entities using Hibernate's default traversable resolver. 我有一种方法,当使用Hibernate的默认可遍历解析器验证实体列表时,可以潜在地对数据库进行大量调用。 If I annotate it @Transactional, it runs nearly four times faster. 如果我对它@Transactional进行注释,它的运行速度将提高近四倍。 A few questions: 几个问题:

1) Why is it faster? 1)为什么速度更快?

2) How can I replicate that speed increase without making the method transactional? 2)如何在不使方法事务化的情况下复制速度的提高? I have absolutely no need to wrap this method in a transaction, and making it transactional is at odds with the real intent of the method. 我绝对没有必要将此方法包装在事务中,并且使其具有事务性与该方法的真实意图不符。

I believe what's going on is that your database logic requires a transaction. 我相信正在发生的事情是您的数据库逻辑需要事务。 If there is a transaction in progress, for example due to Spring creating one based on your annotation, these operation would join it. 如果正在进行某笔交易,例如由于Spring根据您的注释创建了一笔交易,则这些操作会将其加入。 If there is no transaction in progress, they will have to go through the process of getting connection, starting transaction and, likely, setting up a Hibernate session on each call. 如果没有正在进行的事务,则他们将必须经历建立连接,开始事务以及可能在每个呼叫上建立Hibernate会话的过程。

How is this at odds with the intent of your method? 这与您的方法意图有何不同?

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

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