我正在尝试使用RMI将工作负载发送到几个远程服务器的原型。完成工作后,远程服务器应该将结果写回数据库。我使用JPA和Hibernate作为提供者。但是我很难要求远程服务器这样做。
以下是部件的代码将通过RMI发送到远程服务器
public class UpdateDB implements ServerRun<Boolean>, Serializable {
private static final long serialVersionUID = 2683781993159094346L;
@Override
public Boolean execute() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "abc" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( new User( "First user!", new Date() ) );
entityManager.persist( new User( "Second user", new Date() ) );
entityManager.getTransaction().commit();
entityManager.close();
entityManager.close();
entityManagerFactory.close();
return true;
}
}
以下是与上述代码相同级别的resources / META-INF中的persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="abc" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>data.persist.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.205.103:3306/event_grp" />
<property name="javax.persistence.jdbc.user" value="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
如果我在服务器上运行JPA它可以运行,其他任何东西都可以用于RMI功能。但是EntityManagerFactory不会启动,这是错误:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named abc
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at data.persist.UpdateDB.execute(UpdateDB.java:58)
at data.persist.UpdateDB.execute(UpdateDB.java:1)
at 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(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy12.executeTask(Unknown Source)
at client.JobSubmitter.main(JobSubmitter.java:94)
因此,似乎RMI无法在META-INF中找到或加载persistence.xml。我也有类似log4j的经验,但我通过在代码中定义所有内容而不是使用log4j.properties文件来解决这个问题。
编辑: 要发送到远程服务器的代码的包看起来像,这是我放置persistence.xml的唯一地方:
-src/main/java/data/persist/UpdataDB.java
|
src/main/resources/META-INF/persistence.xml
根据用户1888440的问题,我在此处设置了安全权限配置。客户端和远程服务器都使用以下权限:
grant {
permission java.security.AllPermission;
};
如何让远程服务器初始化EntityManagerFactory以便它可以回写到数据库?如果你能提供一些简单的代码,我将非常感激。
答案 0 :(得分:0)
远程服务器需要与客户端相同的资源访问权限。你想让谁真正触动数据库?任何人?如果是这样,那些参与者中的每一个都需要一份persistence.xml副本,以便他们可以创建与数据库的连接。