2

I have a simple Java SE Application which uses JPA(EclipseLink). By default I have one 'default' connection pool which allows up to 32 connections.

Suppose I do something like this:

EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("myPU");

//read
EntityManager em = emFactory.createEntityManager();

TypedQuery<Entity.class> q = em.createQuery("Select e from Entity e", Entity.class);
List<Entity> entities = q.getResultList();

em.close();

//write
em = emFactory.createEntityManager();

Entity e = new Entity();
em.getTransaction().begin();
em.persist(e);
em.getTransaction().commit();

em.close();
  • When are connections actually requested from the connection pool and when are they released again?
  • Can one query request multiple connections from the pool?
  • Would there be a difference if I set the maximum number of connections from 32 to 1?
D-rk
  • 5,513
  • 1
  • 37
  • 55

1 Answers1

2

Well, i do not know how the EclipseLink works, but in Hibernate you can set connection release strategy to release connection to pool after: statement, transaction, entity_close, auto. link But i can't find such a thing for EclipseLink so I assume EclipseLink release connection strategy is after entity_close.

So, it would be like this:

EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("myPU");

//read
EntityManager em = emFactory.createEntityManager();

TypedQuery<Entity.class> q = em.createQuery("Select e from Entity e", Entity.class);
List<Entity> entities = q.getResultList(); << TAKE FROM POOL

em.close(); << RELEASE

//write
em = emFactory.createEntityManager();

Entity e = new Entity();
em.getTransaction().begin(); << TAKE FROM POOL
em.persist(e);
em.getTransaction().commit();

em.close(); << RELEASE
Milan Baran
  • 4,133
  • 2
  • 32
  • 49
  • 2
    connections are lazily obtained in EclipseLink by default, though it can be configured as well. Using default configuration, Read connections are released back to the pool immediately, so getResultList would both obtain and release the connection. For writes, they are lazily obtained as well, so the commit operation would both obtain and release the write connection - I believe begin() lets EclipseLink know a transaction might be needed, but doesn't necessarily start one right away. – Chris Aug 20 '13 at 16:47