Skip to content

Instantly share code, notes, and snippets.

Created September 26, 2011 20:49
Show Gist options
  • Select an option

  • Save anonymous/1243361 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1243361 to your computer and use it in GitHub Desktop.
package br.com.germantech.ecf.infraestrutura.persistencia.entityManager;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.config.TargetDatabase;
import org.eclipse.persistence.jpa.osgi.PersistenceProvider;
import org.postgresql.Driver;
import br.com.germantech.ecf.Bootstrap;
public abstract class ECFEntityManager {
private static EntityManagerFactory emf = null;
private static Map<String, Object> properties = new HashMap<String, Object>();
private static final ThreadLocal<EntityManager> ENTITY_MANAGER_CACHE = new ThreadLocal<EntityManager>();
private static void init() {
properties.put(PersistenceUnitProperties.TARGET_DATABASE,
TargetDatabase.PostgreSQL);
properties.put(PersistenceUnitProperties.JDBC_DRIVER,
Driver.class.getCanonicalName());
properties.put(PersistenceUnitProperties.JDBC_URL,
"jdbc:postgresql://127.0.0.1:5432/ecf");
properties.put(PersistenceUnitProperties.JDBC_USER, "postgres");
properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "*****");
properties.put(PersistenceUnitProperties.CONNECTION_POOL_MIN, "5");
properties.put(PersistenceUnitProperties.CONNECTION_POOL_MAX, "10");
properties.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT, "true");
properties.put(PersistenceUnitProperties.CACHE_STATEMENTS, "true");
properties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
properties.put(PersistenceUnitProperties.CLASSLOADER,
Bootstrap.class.getClassLoader());
// Cria tabelas novas (mas não atualiza)
// properties.put(PersistenceUnitProperties.DDL_GENERATION,"create-tables");
properties.put("eclipselink.logging.level", "FINE");
properties.put("eclipselink.logging.timestamp", "true");
properties.put("eclipselink.logging.session", "false");
properties.put("eclipselink.logging.thread", "true");
properties.put("eclipselink.logging.exceptions", "true");
emf = new PersistenceProvider().createEntityManagerFactory("ecf",
properties);
}
public static EntityManager createEntityManager() {
EntityManager entityManager = ENTITY_MANAGER_CACHE.get();
if (entityManager == null) {
init();
ENTITY_MANAGER_CACHE.set(entityManager = emf.createEntityManager());
}
return entityManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment