Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

java - Running out of Database connections

I have the following singleton class that creates an EntityManagerFactory : -

public class PL4BaseDAO {

    private final static Logger logger = LoggerFactory.getLogger(PL4BaseDAO.class);

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("pl4sms-persistence");

    public static EntityManager getEm() {
        logger.info(emf.toString());
        return emf.createEntityManager();
    }
    
    public static void closeEntityManagerFactory() {
        emf.close();
    }
}

Now the above class is extended by a number of classes such as following : -

@Stateless
public class BillingHistoryDao extends PL4BaseDAO implements Serializable, BillingHistoryInterface {
//  EntityManager em = null;

    private final static Logger logger = LoggerFactory.getLogger(BillingHistoryDao.class);

    public void addbillingHistory(BillingHistory billingHistory) {
        EntityManager em = getEm();
        logger.info(em.toString());
        try {
            em.getTransaction().begin();
            em.persist(billingHistory);
            em.getTransaction().commit();
        } catch (Exception exception) {
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
            // LOG.error(exception.getMessage());

        } finally {

            em.close();
        }

    }
}

The above class is added packaged into a jar and deployed on the wildfly server. Theses DAO layer classes are used by other classes using JNDI lookup.

Now currently I am closing EntityManager after every transaction, but there is no mechanism to close EntityManagerFactory.

When I undeploy the war files and the above jar files from the server, the existing connections get abandoned and new connections are made, after few cycles of such redeployment makes postgres run out connections.

I found out the following answer to close the EntityManagerFactory, If it was in a Web Application, I could close it at destruction of context like the code below

@WebListener
public class EMF implements ServletContextListener {

  private static EntityManagerFactory emf;

  @Override
  public void contextInitialized(ServletContextEvent event) {
    emf = Persistence.createEntityManagerFactory("unitname");
  }

  @Override
  public void contextDestroyed(ServletContextEvent event) {
    emf.close();
  }

  public static EntityManager createEntityManager() {
    if (emf == null) {
      throw new IllegalStateException("Context is not initialized yet.");
    }

    return emf.createEntityManager();
  }
}

But my EntityManagerFactory resides in a jar. How can I close the EntityManagerFactory, When jar is undeployed?

question from:https://stackoverflow.com/questions/65949606/running-out-of-database-connections

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...