Created
March 10, 2011 14:30
-
-
Save jnorthrup/864168 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.talkwheel.server.rfactory.ioc; | |
import java.util.Properties; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Inject; | |
import com.google.inject.*; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: jim | |
* Date: 3/10/11 | |
* Time: 2:54 AM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class DbModule extends AbstractModule { | |
public static Properties properties = new Properties() {{ | |
put("openjpa.RuntimeUnenhancedClasses", "supported"); | |
put("openjpa.jdbc.DBDictionary", "org.apache.openjpa.jdbc.sql.H2Dictionary"); | |
put("openjpa.ConnectionDriverName", "org.h2.Driver"); | |
put("openjpa.ConnectionURL", "jdbc:h2:mem:twroo1001;DB_CLOSE_DELAY=-1"); | |
put("openjpa.ConnectionUserName", "sa"); | |
put("openjpa.ConnectionPassword", ""); | |
put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); | |
put("openjpa.Log", "SQL=TRACE"); | |
put("openjpa.jdbc.SchemaFactory", "native(ForeignKeys=true)"); | |
put("openjpa.jdbc.MappingDefaults", "ForeignKeyDeleteAction=restrict"); | |
put("openjpa.DynamicEnhancementAgent", "true"); | |
put("openjpa.DetachState", "all(DetachedStateField=transient)"); | |
put("openjpa.DynamicDataStructs", "true"); | |
}}; | |
private static final ThreadLocal<EntityManager> ENTITY_MANAGER_CACHE | |
= new ThreadLocal<EntityManager>(); | |
public void configure() { | |
bind(EntityManagerFactory.class).toProvider(EMFP.class).asEagerSingleton(); | |
bind(EntityManager.class).toProvider(EMP.class); | |
} | |
static class EMFP implements Provider<EntityManagerFactory> { | |
@Override | |
public EntityManagerFactory get() { | |
return Persistence.createEntityManagerFactory(null); | |
} | |
} | |
static class EMP implements Provider<EntityManager> { | |
@Inject | |
public EntityManagerFactory entityManagerFactory; | |
@Override | |
public EntityManager get() { | |
EntityManager entityManager = ENTITY_MANAGER_CACHE.get(); | |
if (entityManager == null) { | |
ENTITY_MANAGER_CACHE.set(entityManager = entityManagerFactory.createEntityManager()); | |
} | |
return entityManager; | |
} | |
} | |
} | |
[...] in server/KernelImpl.java: | |
public static void initAll() { | |
if (KernelLifecycle.STARTED != KernelLifecycle.current.get()) { | |
synchronized (KernelLifecycle.class) { | |
if (KernelLifecycle.STARTED != KernelLifecycle.current.get()) { | |
new ApplicationInitializer(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment