我正在尝试完成一个通过RMI将工作负载发送到多个远程服务器的项目。在工作结束时,每个远程服务器都会将结果写回数据库。我在使用JPA(HIBERNATE是提供者)使用此解决方案时遇到了一些重大困难。
如果我可以使用UpdateDB方法将数据写回到本地任何远程服务器上的数据库,那么一切正常。但是如果我通过RMI传递jar / code / classes,它就不会这样做。它提出了例外:
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设置中将数据写回数据库吗?
非常感谢。