Skip to content

Commit

Permalink
Align startup exception wrapping in Hibernate Reactive on Hibernate ORM
Browse files Browse the repository at this point in the history
* applies c857b76 to HR
* applies 989ce28 to HR
* wraps exceptions thrown by build(), but not those thrown when creating
  the builder. Just like ORM.
  • Loading branch information
yrodiere committed Jan 17, 2024
1 parent b0cac4f commit 0f7ccfa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourceSupport;
import io.quarkus.hibernate.orm.runtime.BuildTimeSettings;
import io.quarkus.hibernate.orm.runtime.FastBootHibernatePersistenceProvider;
import io.quarkus.hibernate.orm.runtime.HibernateOrmRuntimeConfig;
import io.quarkus.hibernate.orm.runtime.HibernateOrmRuntimeConfigPersistenceUnit;
import io.quarkus.hibernate.orm.runtime.IntegrationSettings;
import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.hibernate.orm.runtime.PersistenceUnitsHolder;
import io.quarkus.hibernate.orm.runtime.RuntimeSettings;
import io.quarkus.hibernate.orm.runtime.RuntimeSettings.Builder;
Expand Down Expand Up @@ -76,31 +79,25 @@ public FastBootHibernateReactivePersistenceProvider(HibernateOrmRuntimeConfig hi
public EntityManagerFactory createEntityManagerFactory(String emName, Map properties) {
if (properties == null)
properties = new HashMap<Object, Object>();
try {
// These are pre-parsed during image generation:
final List<RuntimePersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

for (PersistenceUnitDescriptor unit : units) {
//if the provider is not set, don't use it as people might want to use Hibernate ORM
if (IMPLEMENTATION_NAME.equalsIgnoreCase(unit.getProviderClassName()) ||
unit.getProviderClassName() == null) {
EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull(emName, properties);
if (builder == null) {
log.trace("Could not obtain matching EntityManagerFactoryBuilder, returning null");
return null;
} else {
return builder.build();
}
// These are pre-parsed during image generation:
final List<RuntimePersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

for (PersistenceUnitDescriptor unit : units) {
//if the provider is not set, don't use it as people might want to use Hibernate ORM
if (IMPLEMENTATION_NAME.equalsIgnoreCase(unit.getProviderClassName()) ||
unit.getProviderClassName() == null) {
EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull(emName, properties);
if (builder == null) {
log.trace("Could not obtain matching EntityManagerFactoryBuilder, returning null");
return null;
} else {
return builder.build();
}
}

//not the right provider
return null;
} catch (PersistenceException pe) {
throw pe;
} catch (Exception e) {
throw new PersistenceException("Unable to build EntityManagerFactory", e);
}

//not the right provider
return null;
}

private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName,
Expand Down Expand Up @@ -286,12 +283,22 @@ private void registerVertxAndPool(String persistenceUnitName,
}

// for now, we only support one pool but this will change
InstanceHandle<Pool> poolHandle = Arc.container().instance(Pool.class);
if (!poolHandle.isAvailable()) {
throw new IllegalStateException("No pool has been defined for persistence unit " + persistenceUnitName);
String datasourceName = DataSourceUtil.DEFAULT_DATASOURCE_NAME;
Pool pool;
try {
if (Arc.container().instance(DataSourceSupport.class).get().getInactiveNames().contains(datasourceName)) {
throw DataSourceUtil.dataSourceInactive(datasourceName);
}
InstanceHandle<Pool> poolHandle = Arc.container().instance(Pool.class);
if (!poolHandle.isAvailable()) {
throw new IllegalStateException("No pool has been defined for persistence unit " + persistenceUnitName);
}
pool = poolHandle.get();
} catch (RuntimeException e) {
throw PersistenceUnitUtil.unableToFindDataSource(persistenceUnitName, datasourceName, e);
}

serviceRegistry.addInitiator(new QuarkusReactiveConnectionPoolInitiator(poolHandle.get()));
serviceRegistry.addInitiator(new QuarkusReactiveConnectionPoolInitiator(pool));

InstanceHandle<Vertx> vertxHandle = Arc.container().instance(Vertx.class);
if (!vertxHandle.isAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public FastBootReactiveEntityManagerFactoryBuilder(PrevalidatedQuarkusMetadata m

@Override
public EntityManagerFactory build() {
final SessionFactoryOptionsBuilder optionsBuilder = metadata.buildSessionFactoryOptionsBuilder();
optionsBuilder.enableCollectionInDefaultFetchGroup(true);
populate(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, optionsBuilder, standardServiceRegistry);
SessionFactoryOptions options = optionsBuilder.buildOptions();
return new ReactiveSessionFactoryImpl(metadata, options, metadata.getBootstrapContext());
try {
final SessionFactoryOptionsBuilder optionsBuilder = metadata.buildSessionFactoryOptionsBuilder();
optionsBuilder.enableCollectionInDefaultFetchGroup(true);
populate(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, optionsBuilder, standardServiceRegistry);
SessionFactoryOptions options = optionsBuilder.buildOptions();
return new ReactiveSessionFactoryImpl(metadata, options, metadata.getBootstrapContext());
} catch (Exception e) {
throw persistenceException("Unable to build Hibernate Reactive SessionFactory", e);
}
}
}

0 comments on commit 0f7ccfa

Please sign in to comment.