gpt4 book ai didi

java - createEntityManagerFactory 在一台服务器上工作,但在另一台服务器上不起作用

转载 作者:行者123 更新时间:2023-12-02 11:06:44 24 4
gpt4 key购买 nike

我有很多机器,完全相同。所有都运行 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)

最佳答案

我通过在类的构造函数中手动创建 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());

关于java - createEntityManagerFactory 在一台服务器上工作,但在另一台服务器上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com