gpt4 book ai didi

java - 在同一应用程序中使用 Google Data Store 和 Google App Engine

转载 作者:行者123 更新时间:2023-12-01 14:15:17 26 4
gpt4 key购买 nike

我有一个应用程序,我将所有数据存储在 Google Cloud SQL 中。但现在我认为一些表可以移动到谷歌数据存储。所以我的问题是如何使用同一应用程序同时使用数据存储和 Cloud SQL。我尝试检查数据存储和云 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)

我是不是错过了什么?请提出建议。

最佳答案

您没有为一个持久性单元指定“提供程序” - 您打算为 cloud-sql 使用哪个 JPA 提供程序?所以指定它。

您的另一个持久性单元的“提供者”无效。正如消息所述,GAE JPA v2 是“org.datanucleus.api.jpa.PersistenceProviderImpl”,但您似乎想使用一些(非常旧的、不受支持的)GAE JPA 1 变体。确保 CLASSPATH 中有正确的一个(而不是另一个)

哪个“JPA API”jar 位于 CLASSPATH 中? (这不是 JPA 实现)。如果您使用的是 GAE JPA v2,则需要 JPA API v2 (geronimo-jpa_2.0_spec),而不是 JPA API v1 (geronimo-jpa_3.0_spec)。如果正在使用的 JPA 提供程序之一是 JPA2,那么另一个也必须是。

关于java - 在同一应用程序中使用 Google Data Store 和 Google App Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18161614/

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