This web container has not yet been started

My Java web application, using Hibernate, published on a GlassFish Server and using MySQL database published in Hostgator is constantly causing the following error:

[2018-01-31T11:51:15.852-0200] [glassfish 4.0] [WARNING] [] [com.mchange.v2.resourcepool.BasicResourcePool] [tid: _ThreadID=264 _ThreadName=C3P0PooledConnectionPoolManager[identityToken->1hge39o9tic1bab1mwszpa|98964cd]-HelperThread-#1] [timeMillis: 1517406675852] [levelValue: 900] [[
  com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@9f55fed -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
java.sql.SQLException: java.lang.IllegalStateException: This web container has not yet been started
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:877)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:873)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:443)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
Caused by: java.lang.IllegalStateException: This web container has not yet been started
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:807)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.GeneratedConstructorAccessor356.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    ... 11 more
]]

I've been trying to find the problem for days, but unfortunately I can't.

Follows below my codes:

  • Hibernate.cfg.xml

        <!--  ****Configurações de Conexão ao Banco de Dados ****  -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/meuBD?zeroDateTimeBehavior=convertToNull</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
    
    
        <!--  **** Propriedades do C3P0 - Pool de Conexão ****  -->
        <property name="hibernate.c3p0.acquireIncrement">3</property>
        <property name="hibernate.c3p0.initialPoolSize">5</property>
        <property name="hibernate.c3p0.minPoolSize">5</property>
        <property name="hibernate.c3p0.maxPoolSize">50</property>
        <property name="hibernate.c3p0.maxIdleTime">5</property>
    
        <property name="hibernate.c3p0.acquireRetryAttempts">50</property>
        <property name="hibernate.c3p0.numHelperThreads">20</property>
        <property name="hibernate.c3p0.checkoutTimeout">0</property>
        <property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
    
    
        <!-- **** SQL Dialect ****  -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    
    
        <!--  **** Gerenciamento do Contexto da Sessão ****  -->
        <property name="current_session_context_class">thread</property>
    
    
        <!--  **** Desabilitando Cache de segundo nível ****  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    
        <!--  **** Mapeamento das Entidades ****  -->
    
  • Hibernatuseful

    private static SessionFactory sessionFactory;
    
    public static SessionFactory getFactory() {
        if (sessionFactory == null) {
            sessionFactory = HibernateUtil.buildSessionFactory();
        }
    
        return sessionFactory;
    }
    
    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());
    
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
            SessionFactory session = configuration.buildSessionFactory(serviceRegistry);
            return session;
    
        } catch (HibernateException ex) {
            System.err.println("Criação da SessionFactory failed." + ex);
    
            throw new ExceptionInInitializerError(ex);
        }
    }
    
  • Class DAO

    private final Class<Entidade> classe;
    private Transaction transaction = null;
    private SessionFactory sessionFactory = null;
    
    @SuppressWarnings("unchecked")
    public GenericDAO() {
        this.classe = (Class<Entidade>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        sessionFactory = HibernateUtil.getFactory();
    }
    
    public void salvar(Entidade entidade) {
        try {
            transaction = sessionFactory.getCurrentSession().beginTransaction();
            sessionFactory.getCurrentSession().save(entidade);
            transaction.commit();
    
        } catch (RuntimeException erro) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw erro;
        }
    }
    
    public void excluir(Entidade entidade) {
        try {
            transaction = sessionFactory.getCurrentSession().beginTransaction();
            sessionFactory.getCurrentSession().delete(entidade);
            transaction.commit();
    
        } catch (RuntimeException erro) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw erro;
        }
    }
    
    public void editar(Entidade entidade) {
        try {
            transaction = sessionFactory.getCurrentSession().beginTransaction();
            sessionFactory.getCurrentSession().update(entidade);
            transaction.commit();
    
        } catch (RuntimeException erro) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw erro;
        }
    }
    
    public List<Entidade> listar() {
        List<Entidade> resultado;
    
        try {
            transaction = sessionFactory.getCurrentSession().beginTransaction();
    
            Criteria consulta = sessionFactory.getCurrentSession().createCriteria(classe);
            resultado = consulta.list();
    
            transaction.commit();
    
        } catch (RuntimeException erro) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw erro;
        }
        return resultado;
    }
    
    @SuppressWarnings("unchecked")
    public Entidade buscar(Integer id) {
        Entidade resultado;
    
        try {
            transaction = sessionFactory.getCurrentSession().beginTransaction();
    
            Criteria consulta = sessionFactory.getCurrentSession().createCriteria(classe);
            consulta.add(Restrictions.idEq(id));
            resultado = (Entidade) consulta.uniqueResult();
    
            transaction.commit();
    
        } catch (RuntimeException erro) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw erro;
        }
        return resultado;
    }
    

As I mentioned above, this is a constant problem and unfortunately I can't figure out the causes of it.

In my development base, this error happens very little, but in my production base, it is recurring.

Please, if anyone can help me.

Thank you very much

Author: EduOK, 2018-01-31

1 answers

The problem is that your EntityManagerFactor has not been closed. Make sure you always close the EntityManagerFactory after a transaction.

Make sure you always close the EntityManagerFactory after a transaction. Also check if you have all your drivers? It may be that your application cannot connect to one of your databases.

Nowhere in your code are you closing your transaction. You can create a method to do this calls on your HibernateUtil.java and calls within your DAO.

Um example.

DAO:

     try{
     TODO:........
     } finally  {
        HibernateUtil.close();
     }

HiberanteUtil:

public static void close() {
        if (session != null) {
            try {
                session.close();
            } catch (HibernateException ex) {
              //print Erro
            }
        }
    }

At a glance at this link:

 0
Author: LR10, 2018-01-31 15:52:46