0

Our client force us to use JdbcTemplate instead of Spring Data Jpa for the development of the Spring project. However, the application is not critical in terms of speed and delivery of responses (it is an internal web application for the client's end-users).We would like to use Spring Data Jpa.

Question: Is there some objective reason to use JdbcTemplate because the speed of the application? From my point of view there will be different bottlenecks.

jnemecz
  • 3,171
  • 8
  • 41
  • 77

1 Answers1

6

The question, what is faster is impossible to answer without specifying an exact use case.

JdbcTemplate will most likely be faster when talking about pure query execution, because a JPA implementation will do more stuff:

  • Parse JPQL (assuming you are using that)
  • creating a SQL query out of that
  • executing it
  • converting the result into objects

While the template will (almost) just:

  • execute the query
  • hand you the result as a call to ResultSetMapper or similar.

Of course JPA does all of this for a reason.

  • it offers a reasonable amount of database independence.
  • it tracks your changes and generates the right update statements for persisting them.
  • it allows for lazy loading so you don't have to think about what to load before hand (of course you still have to do that if you care about performance).

And those things have costs beyond performance.

The abstraction JPA offers is actually really complex and leaky and it is not properly understood by most developers using it. While I think it is completely reasonable to use JPA in many contexts, I can also relate to people banning it from their projects. Talking about performance is absolutely too limited in order to make a well-educated decision on this.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • You are right that Spring Data JPA needs to do extra things compared to plain JdbcTemplate, however most of the time spent will most likely be in I/O (communicating with the DB) and waiting for the DB to execute the query, so the extra stuff that JPA does is most likely not significant. – Jesper Nov 06 '19 at 14:59