0

我有一个应用程序,我将所有数据存储在 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)

我错过了什么吗。请建议。

4

1 回答 1

1

您没有为一个持久性单元指定“提供者” - 您将为 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,那么另一个也必须是。

于 2013-08-12T13:13:09.020 回答