package org.sculptor.framework.persistence;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class AppEngineEntityManagerFactory {

     private static EntityManagerFactory emfInstance;

    public AppEngineEntityManagerFactory() {
    }

    public EntityManagerFactory entityManagerFactory() {
        try {
            // assuming single threaded startup
            if (emfInstance == null) {
                emfInstance = Persistence.createEntityManagerFactory("transactions-optional");
            }
            return emfInstance;
        } catch (RuntimeException e) {
            Logger log = LoggerFactory.getLogger(getClass());
            log.error("Couldn't create EntityManagerFactory", e);
            throw e;
        } catch (Error e) {
            Logger log = LoggerFactory.getLogger(getClass());
            log.error("Couldn't create EntityManagerFactory", e);
            throw e;
        }
    }
}