Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Add Comment
Please, Sign In to add comment