Java NullPointerException at jpa.EntityManagerImpl

2019-08-28 15:59发布

问题:

I am developing an application using Eclipse IDE, Eclipse-Link, JPA, and MySQL. During a find operation in the main application I get the following exception from the Data Access layer:

Exception in thread "Thread-1" java.lang.NullPointerException 
       at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getActivePersistenceContext(EntityManagerImpl.java:1931)
       at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.begin(EntityTransactionImpl.java:93)
       at database.dao.DAO.find(DAO.java:41)   
       at database.facade.FacadeImpl.find(FacadeImpl.java:32)
[...]

The Data Access Layer is implemented by ServerServiceImpl, which has a serverFacade that delegates the operation to DAO object:

public class ServerServiceImpl extends ServerService {

        private Facade<ServerModelSim> serverFacade;

        public ServerServiceImpl(){
            FacadeFactory facadeFactory = new FacadeFactory();
            serverFacade = facadeFactory.createFacade(ServerModelSim.class);
        }

    @Override
    public ServerModel getById(int id) throws ServiceCenterAccessException {
        return serverFacade.find(id);
    }
}

The DAO find() is implemented as follows:

public T find(int entityID) {
        em.getTransaction().begin();
        T entity =  em.find(className, entityID);
        em.getTransaction().commit();
        return entity;

    }

The Entity Manager (em) object from DAO is created here:

public class FacadeFactory {

    private EntityManager entityManager;


    public FacadeFactory() {
        this.entityManager = DBConnection.connect();
    }

     public <T> Facade<T> createFacade(Class<T> c) {
        return new FacadeImpl<T>(new DAO<T>(entityManager, c));
    }

    public void closeConnection() {
        entityManager.close();
    }

    @Override
    protected void finalize() throws Throwable {
        entityManager.close();
    }
}


public class DBConnection {
    private static final String PERSISTENCE_UNIT_NAME = "todos";
    private static EntityManagerFactory factory = Persistence
            .createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    private static EntityManager em = null;

    public static EntityManager connect() {
        System.out.println("DBConn Sim");
        if (em == null)
            em = factory.createEntityManager();
        return em;
    }
}

I would appreciate any help in solving this issue. Thanks!

UPDATE Changing DBConnection class to return a new EntityManager each time seems to solve the problem:

public class DBConnection {
    private static final String PERSISTENCE_UNIT_NAME = "todos2";
    private static EntityManagerFactory factory;

    public static EntityManager connect() {
        System.out.println("CONNECT");
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();
        return em;
    }
}