EntityManagerFactory closed session

0

About this topic: Change in database and update of entity

Resolved, but now gives another EntityManagerFactory is closed error.

I created this class:

package br.com.netsoft.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtility {

    private static final EntityManagerFactory emFactory;
    static {
        emFactory = Persistence.createEntityManagerFactory("desif");
    }

    public static EntityManager getEntityManager() {
        return emFactory.createEntityManager();
    }

    public static void close() {
        emFactory.close();
    }
}

In generic DAO, I created these methods:

protected EntityManager getEntityManager() {
    return JPAUtility.getEntityManager();
}

private void jpaFechar() {
    JPAUtility.close();
}

private void fechar() {
    getEntityManager().clear();
    jpaFechar();
}

Search method

public Set<?> listar(GHEntity entidadeUm, GHEntity entidadeDois,
            Integer primeiroRegistro, Integer totalRegistros,
            String... ordenacao) throws Exception {
    getEntityManager();
    List aux = new ArrayList<>();
    Session sessao = (Session) getEntityManager().getDelegate();
    Criteria crit = sessao.createCriteria(entidadeUm.getClass());
    montaRestricoes(entidadeUm, entidadeDois, crit,
            new DadosDeSuporteAPesquisa());
    contarTodos(entidadeUm, entidadeDois);
    ordenar(crit, ordenacao);
    if (primeiroRegistro >= 0 && totalRegistros > 0) {
        if (!(total.equals(new Long(0)))) {
            crit.setFirstResult(primeiroRegistro);
            crit.setMaxResults(totalRegistros);
        }
    }
    aux = crit.list();
    sessao.evict(entidadeUm);
    jpaFechar();
    return aux;
}

I understood that the problem is that the EntityManagerFactory , closed. But I have not figured out how to adjust

00:04:45,845 ERROR [stderr] (default task-17) java.lang.IllegalStateException: EntityManagerFactory is closed

00:04:45,845 ERROR [stderr] (default task-17)   at org.hibernate.jpa.internal.EntityManagerFactoryImpl.validateNotClosed(EntityManagerFactoryImpl.java:388)

00:04:45,845 ERROR [stderr] (default task-17)   at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:342)

00:04:45,848 ERROR [stderr] (default task-17)   at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)

00:04:45,850 ERROR [stderr] (default task-17)   at br.com.netsoft.dao.JPAUtility.getEntityManager(JPAUtility.java:15)

00:04:45,850 ERROR [stderr] (default task-17)   at br.com.netsoft.dao.GHDAO.getEntityManager(GHDAO.java:71)

00:04:45,850 ERROR [stderr] (default task-17)   at br.com.netsoft.dao.GHDAO.listar(GHDAO.java:106)

00:04:45,850 ERROR [stderr] (default task-17)   at br.com.netsoft.configuracao.auth.JwtUserDetailsService.loadUserByUsername(JwtUserDetailsService.java:28)

00:04:45,850 ERROR [stderr] (default task-17)   at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442)

00:04:45,851 ERROR [stderr] (default task-17)   at br.com.netsoft.configuracao.auth.JwtAuthorizationTokenFilter.doFilterInternal(JwtAuthorizationTokenFilter.java:74)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)

00:04:45,851 ERROR [stderr] (default task-17)   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)

00:04:45,852 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)

00:04:45,862 ERROR [stderr] (default task-17)   at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)

00:04:45,862 ERROR [stderr] (default task-17)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

00:04:45,862 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)

00:04:45,862 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)

00:04:45,862 ERROR [stderr] (default task-17)   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)

00:04:45,862 ERROR [stderr] (default task-17)   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)

00:04:45,863 ERROR [stderr] (default task-17)   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)

00:04:45,863 ERROR [stderr] (default task-17)   at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)

00:04:45,863 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

00:04:45,864 ERROR [stderr] (default task-17)   at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:248)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:77)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:167)

00:04:45,864 ERROR [stderr] (default task-17)   at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)

00:04:45,865 ERROR [stderr] (default task-17)   at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:761)

00:04:45,865 ERROR [stderr] (default task-17)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

00:04:45,865 ERROR [stderr] (default task-17)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

00:04:45,865 ERROR [stderr] (default task-17)   at java.lang.Thread.run(Thread.java:748)

00:04:45,865 ERROR [io.undertow.request] (default task-17) UT005023: Exception handling request to /des-if-web/admin/usuarioEmpresaRecurso/buscar: java.lang.NullPointerException
    at br.com.netsoft.configuracao.auth.JwtUserFactory.mapToGrantedAuthorities(JwtUserFactory.java:28) [classes:]
    at br.com.netsoft.configuracao.auth.JwtUserFactory.create(JwtUserFactory.java:21) [classes:]
    at br.com.netsoft.configuracao.auth.JwtUserDetailsService.loadUserByUsername(JwtUserDetailsService.java:38) [classes:]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) [spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at br.com.netsoft.configuracao.auth.JwtAuthorizationTokenFilter.doFilterInternal(JwtAuthorizationTokenFilter.java:74) [classes:]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) [spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:248) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:77) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:167) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:761) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_131]
    at java.lang.Thread.run(Thread.java:748) [rt.jar:1.8.0_131]
    
asked by anonymous 17.05.2018 / 05:15

1 answer

0

I was able to resolve it as follows:

public Set<?> listar(GHEntity entidadeUm, GHEntity entidadeDois,
            Integer primeiroRegistro, Integer totalRegistros,
            String... ordenacao) throws Exception {
        EntityManager em = JPAUtility.getEntityManager();
        RecordDataSet<GHEntity> linkedHashSet;
        try {
            List aux = new ArrayList<>();
            Session sessao = (Session) em.getDelegate();
            Criteria crit = sessao.createCriteria(entidadeUm.getClass());
            montaRestricoes(entidadeUm, entidadeDois, crit,
                    new DadosDeSuporteAPesquisa());
            contarTodos(entidadeUm, entidadeDois);
            ordenar(crit, ordenacao);
            if (primeiroRegistro >= 0 && totalRegistros > 0) {
                if (!(total.equals(new Long(0)))) {
                    crit.setFirstResult(primeiroRegistro);
                    crit.setMaxResults(totalRegistros);
                }
            }
            aux = crit.list();
            linkedHashSet = new RecordDataSet<GHEntity>(aux);
            linkedHashSet.setTotal(total);
            sessao.evict(entidadeUm);
        } finally {
            em.close();
        }
        return linkedHashSet;
    }

Class JPAUtility

package br.com.netsoft.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtility {

    private static EntityManagerFactory emFactory = null;

    private static void cria() {
        emFactory = Persistence.createEntityManagerFactory("desif");
    }

    public static EntityManager getEntityManager() {
        if (emFactory == null) {
            cria();
        }
        return emFactory.createEntityManager();
    }

    public static void close() {
        emFactory.close();
    }
}
    
18.05.2018 / 16:33