When does JPA EntityManager provide performance advantages over plain JDBC?

I am working on a database application for a client. The old application code is completely incompatible with how it accesses the database; in some methods it uses Hibernate objects, in others it creates JDBC connections directly and uses pure SQL, in some it creates Hibernate connections and uses them for JDBC connections. Anyway, we're going to rebuild a lot of the code to make it consistent. Our client heard about JPA and asked us to use it because the client believes it will have a performance advantage.

The problem is that my understanding of the performance benefits of JPA is that the JPA EntityManager is of great benefit as it allows you to persist objects in memory between calls to the database. However, the application we are working on will not be the only application accessing the database and will run in multiple instances in multiple locations. Also, EntityManagers are not thread safe, so we won't even be able to see the changed data in another part of the same application.So it looks like we will need to get a new EntityManager from the EntityManagerFactory in each method and close it when the method is finished. Thus, we would not be able to keep objects in memory for a very long time, because other applications need data that we will change and change the data we need.

The current code is such a mess that moving to JPA will provide great maintainability and consistency benefits anyway. However, if this does not provide a performance benefit, we must inform the client as soon as possible so that they are not disappointed. Are there other ways that JPA provides performance benefits beyond the obvious associated with Persistance contexts?

I've checked other questions on StackOverflow, but the ones I found talk about changes in large batches ( example here ). Our client wants changes made by one application to be visible to other applications as soon as possible, so our application will make a few small changes rather than periodic package changes.

Thank.

+3


source to share


2 answers


JPA doesn't necessarily give you a performance advantage over regular JDBC. This can make a great impression on your system. It all depends on how you use it.

It looks like you are already using Hibernate. JPA itself is a specification that is implemented by a number of vendors or vendors. Hibernate is one of them (and is the JPA2 provider that I use at work).

It may take a while to create the EntityManagerFactory. On the other hand, creating the EntityManger is quick and easy.



If you're experiencing poor performance, you can try profiling your code to see where the performance issues are.

Moving one level of access sounds like a good idea to me, and it improves maintainability and consistency.

+2


source


JPA is a standard for OR / M . There are many reasons to use an ORM framework or persistence product, and many reasons to use JPA in particular.

ORM reasons

  • Eliminates all the hands in Java from SQL ResultSet to Java POJO dramatically reduces the mapping workload.
  • Reduces the work required for the persistence code base by modifying the domain data model and / or relational data.
  • Uses a large persistence library to avoid developing solutions to a problem that others have already solved.
  • Prevents low-level JDBC and SQL code.
  • Uses object-oriented programming and use of the object model.
  • Provides database and schema independence.
  • Most ORM products are free and open source.
  • Many corporate corporations provide support and services for ORM products.
  • Provides high performance features like caching and database and query optimization.

Reasons for JPA



  • It is a standard and part of EJB3 and Java EE.

  • Many free and open source products with enterprise support.

  • Portability of application servers and persistence products (avoids vendor lockdown). A useful and functional specification.

  • Supports both Java EE and Java SE.

ORM can be a hot topic for some people, and there are many ORM camps out there. There are those that support a specific standard or product. There are those who don't believe in ORMs or even objects at all and prefer JDBC. There are those who still think object databases are the way to go. Personally, I would recommend that you use whichever technique you like best, but if you've never used ORM or JPA, maybe give it a try and see if you like it. The list below provides several discussions on why or why not to use JPA and ORM. (From wikibooks)

Here are some useful links: JPA or JDBC, how are they different? , Saving Java / why use JPA or ORM? , The JPA fine but damn slow

+3


source







All Articles