From 8113c3f05e3c27992cf6e9753b87ce85ee040647 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 14 Jul 2014 15:55:26 +0100 Subject: [PATCH 1/2] Fixes to persistence.xml --- .../aerogear-ups/auth-server/pom.xml | 10 ++++ .../main/resources/META-INF/persistence.xml | 17 ++---- .../main/resources/META-INF/persistence.xml | 11 +--- testsuite/integration/pom.xml | 7 --- .../main/resources/META-INF/persistence.xml | 52 ------------------- 5 files changed, 16 insertions(+), 81 deletions(-) diff --git a/project-integrations/aerogear-ups/auth-server/pom.xml b/project-integrations/aerogear-ups/auth-server/pom.xml index c32789cc08..2ea0015550 100755 --- a/project-integrations/aerogear-ups/auth-server/pom.xml +++ b/project-integrations/aerogear-ups/auth-server/pom.xml @@ -57,6 +57,16 @@ keycloak-model-jpa ${project.version} + + org.keycloak + keycloak-model-sessions-mem + ${project.version} + + + org.keycloak + keycloak-model-sessions-jpa + ${project.version} + org.keycloak keycloak-audit-api diff --git a/project-integrations/aerogear-ups/auth-server/src/main/resources/META-INF/persistence.xml b/project-integrations/aerogear-ups/auth-server/src/main/resources/META-INF/persistence.xml index a7040140f5..294edb463d 100755 --- a/project-integrations/aerogear-ups/auth-server/src/main/resources/META-INF/persistence.xml +++ b/project-integrations/aerogear-ups/auth-server/src/main/resources/META-INF/persistence.xml @@ -14,21 +14,15 @@ org.keycloak.models.jpa.entities.SocialLinkEntity org.keycloak.models.jpa.entities.AuthenticationLinkEntity org.keycloak.models.jpa.entities.UserEntity - org.keycloak.models.jpa.entities.UserSessionEntity - org.keycloak.models.jpa.entities.ClientUserSessionAssociationEntity - org.keycloak.models.jpa.entities.UsernameLoginFailureEntity org.keycloak.models.jpa.entities.UserRoleMappingEntity org.keycloak.models.jpa.entities.ScopeMappingEntity - true + + org.keycloak.models.sessions.jpa.entities.ClientUserSessionAssociationEntity + org.keycloak.models.sessions.jpa.entities.UserSessionEntity + org.keycloak.models.sessions.jpa.entities.UsernameLoginFailureEntity - - - - - - - java:jboss/datasources/ExampleDS + org.keycloak.audit.jpa.EventEntity true @@ -37,5 +31,4 @@ - diff --git a/server/src/main/resources/META-INF/persistence.xml b/server/src/main/resources/META-INF/persistence.xml index 8da00466b2..294edb463d 100755 --- a/server/src/main/resources/META-INF/persistence.xml +++ b/server/src/main/resources/META-INF/persistence.xml @@ -22,15 +22,7 @@ org.keycloak.models.sessions.jpa.entities.UserSessionEntity org.keycloak.models.sessions.jpa.entities.UsernameLoginFailureEntity - true - - - - - - - - java:jboss/datasources/ExampleDS + org.keycloak.audit.jpa.EventEntity true @@ -39,5 +31,4 @@ - diff --git a/testsuite/integration/pom.xml b/testsuite/integration/pom.xml index c18abd5f9f..acb145b71e 100755 --- a/testsuite/integration/pom.xml +++ b/testsuite/integration/pom.xml @@ -504,13 +504,6 @@ ${keycloak.model.mongo.db} ${keycloak.model.mongo.clearOnStartup} ${keycloak.model.mongo.bindIp} - - mongo - ${keycloak.model.mongo.host} - ${keycloak.model.mongo.port} - ${keycloak.model.mongo.db} - ${keycloak.model.mongo.clearOnStartup} - ${keycloak.model.mongo.bindIp} diff --git a/testsuite/integration/src/main/resources/META-INF/persistence.xml b/testsuite/integration/src/main/resources/META-INF/persistence.xml index 8d67da09d6..70544c6a93 100755 --- a/testsuite/integration/src/main/resources/META-INF/persistence.xml +++ b/testsuite/integration/src/main/resources/META-INF/persistence.xml @@ -38,56 +38,4 @@ - - - org.hibernate.ejb.HibernatePersistence - - org.keycloak.audit.jpa.EventEntity - - true - - - - - - - - - - - - - From e3f260c57f05bd7fd8c797e3f26e883d6f00518e Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 14 Jul 2014 16:28:47 +0100 Subject: [PATCH 2/2] Added lazy load to connection factories, to make sure we only try to connect to JPA/Mongo when used --- .../DefaultJpaConnectionProviderFactory.java | 22 +++++-- ...DefaultMongoConnectionFactoryProvider.java | 60 ++++++++++++------- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index 70f3c6dbb6..dbb0751ad6 100644 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -13,10 +13,13 @@ import javax.persistence.Persistence; */ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProviderFactory { - private EntityManagerFactory emf; + private volatile EntityManagerFactory emf; + private String unitName; @Override public JpaConnectionProvider create(KeycloakSession session) { + lazyInit(); + EntityManager em = emf.createEntityManager(); em = PersistenceExceptionConverter.create(em); session.getTransaction().enlist(new JpaKeycloakTransaction(em)); @@ -25,7 +28,9 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide @Override public void close() { - emf.close(); + if (emf != null) { + emf.close(); + } } @Override @@ -35,8 +40,17 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide @Override public void init(Config.Scope config) { - String unitName = config.get("unitName", "jpa-keycloak-identity-store"); - emf = Persistence.createEntityManagerFactory(unitName, JpaUtils.getHibernateProperties()); + unitName = config.get("unitName", "jpa-keycloak-identity-store"); + } + + private void lazyInit() { + if (emf == null) { + synchronized (this) { + if (emf == null) { + emf = Persistence.createEntityManagerFactory(unitName, JpaUtils.getHibernateProperties()); + } + } + } } } diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 9a98e9a8a6..9544263da8 100644 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -10,6 +10,7 @@ import org.keycloak.connections.mongo.api.MongoStore; import org.keycloak.connections.mongo.impl.MongoStoreImpl; import org.keycloak.connections.mongo.impl.context.TransactionMongoStoreInvocationContext; import org.keycloak.models.KeycloakSession; +import org.keycloak.util.JpaUtils; import java.util.Collections; @@ -36,13 +37,16 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); - private MongoClient client; + private volatile MongoClient client; private MongoStore mongoStore; private DB db; + private Config.Scope config; @Override public MongoConnectionProvider create(KeycloakSession session) { + lazyInit(); + TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore); session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext)); return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext); @@ -50,28 +54,38 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro @Override public void init(Config.Scope config) { - try { - String host = config.get("host", ServerAddress.defaultHost()); - int port = config.getInt("port", ServerAddress.defaultPort()); - String dbName = config.get("db", "keycloak"); - boolean clearOnStartup = config.getBoolean("clearOnStartup", false); + this.config = config; + } - String user = config.get("user"); - String password = config.get("password"); - if (user != null && password != null) { - MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); - client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential)); - } else { - client = new MongoClient(host, port); + private void lazyInit() { + if (client == null) { + synchronized (this) { + if (client == null) { + try { + String host = config.get("host", ServerAddress.defaultHost()); + int port = config.getInt("port", ServerAddress.defaultPort()); + String dbName = config.get("db", "keycloak"); + boolean clearOnStartup = config.getBoolean("clearOnStartup", false); + + String user = config.get("user"); + String password = config.get("password"); + if (user != null && password != null) { + MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); + client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential)); + } else { + client = new MongoClient(host, port); + } + + this.db = client.getDB(dbName); + + this.mongoStore = new MongoStoreImpl(db, clearOnStartup, getManagedEntities()); + + logger.infof("Initialized mongo model. host: %s, port: %d, db: %s, clearOnStartup: %b", host, port, dbName, clearOnStartup); + } catch (Exception e) { + throw new RuntimeException(e); + } + } } - - this.db = client.getDB(dbName); - - this.mongoStore = new MongoStoreImpl(db, clearOnStartup, getManagedEntities()); - - logger.infof("Initialized mongo model. host: %s, port: %d, db: %s, clearOnStartup: %b", host, port, dbName, clearOnStartup); - } catch (Exception e) { - throw new RuntimeException(e); } } @@ -85,7 +99,9 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro @Override public void close() { - client.close(); + if (client != null) { + client.close(); + } } @Override