我有一个应用程序,我将所有数据存储在 Google Cloud SQL 中。但现在我认为一些表可以移动到谷歌数据存储区。所以我的问题是如何使用相同的应用程序同时使用 Data Store 和 Cloud SQL。我尝试检查 datastore 和 cloud SQL 的选项,但它只保留在 Cloud SQL 中。
这是我的 persistence.xml 文件。
<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd"
version="1.0">
<persistence-unit name="transactions-optional1">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<class>com.sample.poc.Component</class>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
<property name="datanucleus.singletonEMFForName" value="true"/>
</properties>
</persistence-unit>
<persistence-unit name="transactions-optional2"
transaction-type="RESOURCE_LOCAL">
<provider> org.datanucleus.jpa.PersistenceProviderImpl</provider>
<class>com.sample.poc.User</class>
<class>com.sample.poc.Role</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.google.appengine.api.rdbms.AppEngineDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:google:rdbms://span-test-app:testinstance/pocDB" />
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
这是我的两个 EMF bean:
对于数据存储:
package com.sample.poc;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class EMFDS {
private static final EntityManagerFactory emfInstance = Persistence
.createEntityManagerFactory("transactions-optional1");
private EMFDS() {
}
public static EntityManagerFactory get() {
return emfInstance;
}
}
这是云 sql 的 EMF bean:
package com.sample.poc;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class EMFRDBMS {
private static final EntityManagerFactory emfInstance = Persistence
.createEntityManagerFactory("transactions-optional");
private EMFRDBMS() {
}
public static EntityManagerFactory get() {
return emfInstance;
}
}
这是在数据存储区中插入组件的 servlet:
package com.sample.poc;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ComponentInsert extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String CompId = request.getParameter("CompId");
String CompName= request.getParameter("CompName");
response.setContentType("text/html");
EntityManager e = EMFDS.get().createEntityManager();
try {
e.getTransaction().begin();
Component ob = new Component();
ob.setCompId(Integer.parseInt(CompId));
ob.setCompName(CompName);
e.persist(ob);
e.getTransaction().commit();
} catch (Exception ex) {
e.getTransaction().rollback();
ex.printStackTrace();
}
}
}
我只希望用户、角色 bean 保留在云 sql 和数据存储中的组件中。
当我尝试在数据存储中插入组件时,它会抛出此异常:
Caused by: javax.persistence.PersistenceException: No persistence providers available for "transactions-optional1" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:180)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
at com.sample.poc.EMFDS.<clinit>(EMFDS.java:9)
... 40 more
12 Aug, 2013 9:48:24 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Nested in java.lang.ExceptionInInitializerError:
javax.persistence.PersistenceException: No persistence providers available for "transactions-optional1" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:180)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
我错过了什么吗。请建议。