user2670364
user2670364

Reputation: 9

Use Google Data Store and Google App Engine in same application

I have an application where i was storing all my data in Google Cloud SQL . But now i think that some the tables can be moved to Google datastore . So my question is how can use both Data Store and Cloud SQL using same application . I tried checking both the option for datastore and cloud SQL but its only persisting in Cloud SQL .

This is my persistence.xml file .

<?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>

Here is my two EMF beans:

For Datastore:

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;
    }
}

Here is EMF bean for cloud sql:

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;
    }
}

Here's the servlet for inserting component in datastore:

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();
    }
}

}

I just want user,role bean to be persisted in cloud sql and component in datastore.

When i try to insert a component in datastore its throws this exception:

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)

Am I missing some thing . Please suggest .

Upvotes: 0

Views: 1135

Answers (1)

DataNucleus
DataNucleus

Reputation: 15577

You have no "provider" specified for one persistence-unit - which JPA provider are you going to use for cloud-sql? So specify it.

You have an invalid "provider" for the other persistence-unit. GAE JPA v2 is "org.datanucleus.api.jpa.PersistenceProviderImpl" as the message says, yet you seem to want to use some (very old, unsupported) GAE JPA 1 variant. Make sure you have the right one in the CLASSPATH (and not the other one)

Which "JPA API" jar is in the CLASSPATH? (this is NOT the JPA implementation). If you are using GAE JPA v2 then you need JPA API v2 (geronimo-jpa_2.0_spec) and NOT JPA API v1 (geronimo-jpa_3.0_spec). If one of the JPA providers being used is JPA2 then the other has to be too.

Upvotes: 1

Related Questions