7

When i tried to connect my Cloud SQL via JPA the following error is generated :

2012-10-25 10:07:38.439
Error for /jpatest
java.lang.NoClassDefFoundError: Could not initialize class com.my.jpa.EMF
    at com.my.jpa.ContactService.createContact(ContactService.java:20)
    at com.my.jpa.JPATestServlet.doGet(JPATestServlet.java:14)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

2012-10-25 10:07:38.440
Uncaught exception from servlet
java.lang.NoClassDefFoundError: Could not initialize class com.my.jpa.EMF
    at com.my.jpa.ContactService.createContact(ContactService.java:20)
    at com.my.jpa.JPATestServlet.doGet(JPATestServlet.java:14)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

My EMF Class is

public final class EMF {
    private static final EntityManagerFactory emfInstance = Persistence
            .createEntityManagerFactory("JPATest");

    private EMF() {
    }

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

EMF initialising portion is

 public class ContactService {
        private static Logger logger = Logger.getLogger(ContactService.class
                .getName());

        public void createContact(Contact c) {
            logger.info("Entering createContact: [" + c.getFirstName() + ","
                    + c.getLastName() + "]");
            EntityManager mgr = EMF.get().createEntityManager();
            try {
                mgr.getTransaction().begin();
                mgr.persist(c);
                mgr.getTransaction().commit();
            } finally {
                mgr.close();
            }
            logger.info("Exiting createContact");
        }
}

My Servlet is :

public class JPATestServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {        
        ContactService service = new ContactService();
        service.createContact(new Contact("Manu", "Mohan", "686019", "TVM"));
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world");
    }
}

web.xml is

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
        <servlet-name>JPATest</servlet-name>
        <servlet-class>com.my.jpa.JPATestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JPATest</servlet-name>
        <url-pattern>/jpatest</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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">
    <persistence-unit name="JPATest">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.my.jpa.Contact</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.google.cloud.sql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms://instance-name/stock" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
        </properties>
    </persistence-unit>
</persistence
Master Mind
  • 2,386
  • 3
  • 31
  • 48
  • did you configure `EMF`in web.xml? could you post `web.xml` configuration? – Zaw Than oo Oct 25 '12 at 06:00
  • @CycDemo look the modified qn.. – Master Mind Oct 25 '12 at 06:27
  • As per your question of yesterday, you haven't quoted your persistence.xml, and a message like "No available StoreManager found for the datastore URL key "jdbc"" means you have still not set the persistence "provider" in persistence.xml. – DataNucleus Oct 25 '12 at 11:01
  • @DataNucleus i gave it,please look on it which i added in the above question – Master Mind Oct 25 '12 at 11:32
  • You still haven't quoted it in your question. If there are MULTIPLE JPA implementations in the CLASSPATH then you MUST specify it, and it will only find the one that is appropriate. As I said yesterday you can remove all except the one you want from the CLASSPATH, or do this. – DataNucleus Oct 25 '12 at 11:34

1 Answers1

2

Do you need to use final for EntityManagerFactory in EMF. Try to use Singleton Design Pattern for EMF. EntityManagerFactory class is thread-safe.

EMF.java

public final class EMF {
    private EntityManagerFactory emfInstance;

    private static EMF emf;

    private EMF() {
    }

    public EntityManagerFactory get() {
        if(emfInstance == null) {
            emfInstance = Persistence.createEntityManagerFactory("JPATest");
        }
        return emfInstance;
    }

    public static EMF getInstance() {
        if(emf == null) {
            emf = new EMF();
        }
        return emf;
    }
}

// usages
EntityManagerFactory emf = Emf.getInstance().get();

Here better way to use EntityManagerFactory in web applicaiton.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • this error is solved then another error emerged..jpatest javax.jdo.JDOFatalUserException: No available StoreManager found for the datastore URL key "jdbc". Please make sure you have all relevant plugins in the CLASSPATH (e.g datanucleus-rdbms?, datanucleus-db4o?), and consider setting the persistence property "datanucleus.storeManagerType" to the type of store you are using e.g rdbms, db4o – Master Mind Oct 25 '12 at 08:14