Derek
Derek

Reputation: 749

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. If I annotate it @Transactional, it runs nearly four times faster. A few questions:

1) Why is it faster?

2) How can I replicate that speed increase without making the method transactional? 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.

Upvotes: 1

Views: 2943

Answers (1)

Olaf
Olaf

Reputation: 6289

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. 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.

How is this at odds with the intent of your method?

Upvotes: 1

Related Questions