0

我有很多机器,都是一样的。都在运行 Tomcat 8.5.31 和 java 1.8.0_171-b11

我有一个创建实体管理器的数据库类。我在所有机器上从 CDI 调用该类,这有效。

该方法调用 createEntityManagerFactory("jdbc/mydatabase")

然后我使用 web.xml 中定义的侦听器调用相同的方法,它再次工作,但仅在某些机器上,完全相同的 .war

我能找到的唯一区别是,它不能在运行 Bitdefender 的机器上运行?

在它不起作用的机器上,我得到以下空指针,

java.lang.NullPointerException
    at javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver.getProviderNames(PersistenceProviderResolverHolder.java:197)
    at javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver.getPersistenceProviders(PersistenceProviderResolverHolder.java:112)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:76)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.tillsystem.managers.DatabaseManager.getEntityManageriTill(DatabaseManager.java:55)
    at com.tillsystem.managers.DatabaseManager.getTableTransactionsIds(DatabaseManager.java:245)
    at com.tillsystem.sync.SyncService.downloadTransactions(SyncService.java:249)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
4

1 回答 1

1

我通过在类的构造函数中手动创建 PersistenceProviderResolver 来解决这个问题

PersistenceProviderResolverHolder.setPersistenceProviderResolver(new PersistenceProviderResolver() {
        private List<PersistenceProvider> providers = new ArrayList<PersistenceProvider>();

        @Override
        public List<PersistenceProvider> getPersistenceProviders() {
            org.eclipse.persistence.jpa.PersistenceProvider provider = new org.eclipse.persistence.jpa.PersistenceProvider();
            providers.add(provider);
            return providers;
        }

        @Override
        public void clearCachedProviders() {
            providers.clear();
        }
    });
    properties.put(PersistenceUnitProperties.CLASSLOADER, getClass()
            .getClassLoader());
于 2018-06-18T11:24:59.227 回答