我正在尝试完成一个通过 RMI 将工作负载发送到多个远程服务器的项目。在工作结束时,每个远程服务器会将结果写回数据库。我在使 JPA(HIBERNATE 是提供者)与此解决方案一起工作时遇到了一些重大困难。
如果我可以使用 UpdateDB 方法将数据写回本地任何远程服务器上的数据库,那么一切正常。但是,如果我通过 RMI 传递 jar/代码/类,它就不会这样做。它引发了异常:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named PersistenceUnit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at cluster.persistence.UpdateDB.execute(UpdateDB.java:148)
at cluster.persistence.UpdateDB.execute(UpdateDB.java:1)
at cms.engine.ComputeEngine.executeTask(ComputeEngine.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at com.sun.proxy.$Proxy0.executeTask(Unknown Source)
at cluster.persistence.UpdateDB.main(UpdateDB.java:93)
如果我尝试访问代码中的 persistence.xml,如下所示:
try {
URL pUrl = UpdateDB.class.getClassLoader().getResource("META-INF/persistence.xml");
System.out.println(pUrl);
BufferedReader stream = null;
InputStream in = UpdateDB.class.getClassLoader().getResourceAsStream(
"META-INF/persistence.xml");
stream = new BufferedReader(new InputStreamReader(in));
String line = null;
line = stream.readLine();
while (line != null) {
line = stream.readLine();
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
persistenc.xml 可以通过正确的位置访问(jar:htp://MYCODEBASE.jar!/META-INF/persistence.xml)。并且可以正确打印出整个persistence.xml。
谁能告诉我如何使它工作,以便远程服务器可以将数据写回 RMI 设置中的数据库?
非常感谢。