1

I am using EntityManger for the retrieval queries in my spring boot application. An example snippet is given below.

@Repository
public class EntityManagerUtil {

  @PersistenceContext
  private EntityManager entityManager;


  public List<Employee> getAll(){

    // Use entityManager here
    
  }
}

I need some clarity on the below questions.

a. Is EntityManger will be created for every call (or) will it be a singleton? (I tried printing entityManager, it is returning the same object for all the calls)

b. Is this approach leads to any connection leaks?

c. Do I need to put @Transactional annotation on reading operations?

I am using spring boot version 2.0.3-release

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • 1. It depends on your config, 2. no, 3. yes. – M. Deinum Oct 07 '20 at 06:47
  • Thanks Denium. for the first question, Can you share some information on what configurastion I required, and signleton entitymanager vs non-singleton...and Why should I add @Transactional annotation on retrieval methods...What are the side affects, if I do not add – Hari Krishna Oct 07 '20 at 07:11
  • Suppose, If I do not add @Transactional annotation on retrieval methods, will it leads to any connection leaks or performance issues... – Hari Krishna Oct 07 '20 at 07:24
  • 3
    It won't but you want to control your transactional boundaries, there will always be an (implicit) transaction so better to control it. The entitymanager is never a singleton (what you see is a proxy that delegates to the thread bound or transaction bound entitymanager). Which is used depends on your config, transaction setup etc. – M. Deinum Oct 07 '20 at 07:44

1 Answers1

0

a) See M Deinum's comments about the injected EntityManager being a proxy. The normal situation for JPA is to have an EntityManager tied to each thread, Spring sets this up for you and provides a proxy to delegate to the actual EntityManager. So this arrangement is safe.

b) Connection leaks happen when you don't return a database connection to the pool. Spring takes care of providing a connection and cleaning up resources so in the normal course of things you shouldn't get connection leaks. If you ignore the facilities Spring gives you like JdbcTemplate and insist on writing your own JDBC code, then you can cause resources to leak. If your code tries to use an EntityManager intended for another thread you will get an exception, it will not fail silently, and it will not (in itself) cause a connection leak.

c) You are using an RDBMS so you will have a transaction. Apparently some people think transactions are optional, but they really aren't. If you don't specify them then Spring will wrap each operation in its own transaction. Not only are you just better off in general being explicit about this, but you can mark the transaction as readonly, which lets Spring perform some optimizations, see Oliver Drotbohm's answer for details. So maybe you can get by without it for some cases (such as, each http request results in only one database operation), but it is in your interest to use it.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276