0

I have a web application that has to add data to a database and I have to do this for 1000 entities. Everything goes well until the 10th iteration and then when entering the method that does the persist I get cannot acquire JDBC connection at the line which opens the transaction. I will add the method below. Does anyone know what I should do?

@PersistenceUnit(unitName = "current_project")
private EntityManagerFactory entityManagerFactory;

public long persistAccountObject(AccountObject accountObject) {

    long pdAccmAccountManagerId = 0;

    try {
        logger.debug("Start Persisting...");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(accountObject);
        entityManager.getTransaction().commit();
        // unique ID
        accountId= accountObject.getId();
        logger.debug("Persisting OK...");

    }
    catch (PersistenceException persistenceException) {
        logger.error("PersistenceException occur", persistenceException);
    }
    catch (Exception e) {
        logger.error("Exception occurs", e);
    }

    return accountId;
}

The exception occurs at the line with entityManager.getTransaction().begin() at the 10th iteration every time. Until then, I get the objects persisted in the database.

I tried to add the hibernate-c3p0 dependency in the pom and set some properties for it in the persistence.xml, but if I do that, my program gets blocked and does not even start running.

What should I do?

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
Maria1995
  • 439
  • 1
  • 5
  • 21
  • How did you come up with this code? You should (most likely) be using [@PersistenceContext](https://stackoverflow.com/questions/21038706/persistenceunit-vs-persistencecontext) instead. – Kayaman Nov 04 '19 at 10:27
  • May be till 10 iteration may be no connection is free in connection pool. Can you check your connection are releasing properly after each transaction? – vivekdubey Nov 04 '19 at 10:29
  • @Kayaman it did not work for me in a different way, my objects did not get persisted...this is why I have this approach – Maria1995 Nov 04 '19 at 10:31
  • @Maria1995 well, you would need to close the `EntityManager` after use. But this code is bad, you shouldn't be injecting an `EntityManagerFactory`. How did you come up with this code? – Kayaman Nov 04 '19 at 10:33
  • @Kayaman At first, I tried with entityManager only and I got errors saying that there is no transaction running...this is the workaround I found and it works and I have data in the database. Any suggestion with how should I use entityManager? – Maria1995 Nov 04 '19 at 11:24
  • @Maria1995 you should use `@Transactional` to denote transaction boundaries. This is complicated stuff that you can't just "hack together". Copy pasting code snippets until something "kind of works" will result in bad code, you not understanding what you're doing and basically a lot of wasted time. Better start with [this](https://docs.oracle.com/javaee/7/tutorial/) or [this](https://javaee.github.io/tutorial/). – Kayaman Nov 04 '19 at 11:29
  • You need to close the entity manager when you're done with it if it is unmanaged. – Mark Rotteveel Nov 05 '19 at 08:27

0 Answers0