Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple Hibernate ORM persistence units via Quarkus configuration #11322

Merged
merged 15 commits into from Aug 21, 2020

Conversation

gsmet
Copy link
Member

@gsmet gsmet commented Aug 11, 2020

/cc @Sanne @machi1990 @michael-schnell

This is the preliminary infrastructure work for the support of multiple persistence units. I would like to merge this PR early (well, as soon as we agree the code is correct) and then do additional follow-ups for tweaking how we do the entity <-> persistence unit mapping and if we allow an entity to belong to several persistence units.

Fixes #2835

@gastaldi gastaldi added the triage/needs-rebase This PR needs to be rebased first because it has merge conflicts label Aug 14, 2020
@gsmet gsmet changed the title Multi persistence units infrastructure (1/n) Multiple persistence units infrastructure Aug 17, 2020
@gsmet gsmet force-pushed the multi-persistence-units branch 2 times, most recently from 4d251f4 to 974f27f Compare August 18, 2020 10:20
@gsmet
Copy link
Member Author

gsmet commented Aug 18, 2020

@machi1990 I had to tweak the config a bit due to a SmallRye Config issue and I end up having another issue with the documentation:

[INFO] asciidoctor: WARN: ed/config/quarkus-hibernate-orm.adoc: line 147: id assigned to anchor already in use: quarkus-hibernate-orm_quarkus.hibernate-orm.dialect
[INFO] asciidoctor: WARN: ed/config/quarkus-hibernate-orm.adoc: line 455: id assigned to anchor already in use: quarkus-hibernate-orm_quarkus.hibernate-orm.-persistence-unit-name-.dialect
[INFO] Converted /data/home/gsmet/git/quarkus/docs/src/main/asciidoc/hibernate-orm.adoc
[ERROR] asciidoctor: WARN: ed/config/quarkus-hibernate-orm.adoc: line 147: id assigned to anchor already in use: quarkus-hibernate-orm_quarkus.hibernate-orm.dialect
[ERROR] asciidoctor: WARN: ed/config/quarkus-hibernate-orm.adoc: line 455: id assigned to anchor already in use: quarkus-hibernate-orm_quarkus.hibernate-orm.-persistence-unit-name-.dialect

Could you have a look?

@gsmet
Copy link
Member Author

gsmet commented Aug 18, 2020

I added a commit that somehow makes it work (removing the @ConfigDocSection). You need to drop this commit to be able to reproduce.

@gsmet gsmet marked this pull request as ready for review August 18, 2020 10:26
@gsmet gsmet removed the triage/needs-rebase This PR needs to be rebased first because it has merge conflicts label Aug 18, 2020
@gsmet gsmet added this to the 1.8.0 - master milestone Aug 18, 2020
@machi1990
Copy link
Member

machi1990 commented Aug 18, 2020

I added a commit that somehow makes it work (removing the @ConfigDocSection). You need to drop this commit to be able to reproduce.

Okay, I'll check it out later today. Thanks for the update.

@machi1990
Copy link
Member

I added a commit that somehow makes it work (removing the @ConfigDocSection). You need to drop this commit to be able to reproduce.

Yeap. This was the issue as the anchor used for the dialect config section and the contained dialect config element was the same since the later inherited the parent name.

An easy fix will be to make the config section anchor unique enough by using the title for example. A more appropriate fix will be to construct the anchor separate from the configuration name e.g simply traversing the object and hyphenating the field name will be enough.

machi1990 and others added 15 commits August 18, 2020 18:03
It has a special meaning and I don't want it to conflict with the user
defined ones.
No code changes, just moving methods around.
In passing, we now make JTA the only case handled.
I will throw a proper error in a follow-up commit when not enabled.
I decided to support multiple PUs there. We will see how it goes in
Panache.
Sanne confirmed that not having JTA around is not a valid configuration.
We used to have some partial support for it but it was buggy at best.

The support was removed in a previous commit, this commit only adds a
proper exception for it.
We don't have a prefix in the other extensions so let's try to be
consistent and see how it goes.
In passing, reorganize the dialect configuration to work around an issue
with SmallRye Config.
@Sanne
Copy link
Member

Sanne commented Aug 20, 2020

@gsmet I checked in ORM and can confirm that hibernate.dialect.storage_engine is only applied to MySQL (and MariaDB as a consequence)

@gsmet
Copy link
Member Author

gsmet commented Aug 20, 2020

OK, I will change the logic once this PR is in, then!

@gavinking
Copy link

@gsmet I checked in ORM and can confirm that hibernate.dialect.storage_engine is only applied to MySQL (and MariaDB as a consequence)

Isn't this kinda legacy? Does anyone really need to generate MyISAM schemas these days?

IMO this is pretty much deprecated functionality.

@Sanne
Copy link
Member

Sanne commented Aug 20, 2020 via email

@@ -666,6 +697,14 @@ private static void producePersistenceUnitDescriptorFromConfig(
// we found one
ParsedPersistenceXmlDescriptor descriptor = new ParsedPersistenceXmlDescriptor(null); //todo URL
descriptor.setName(persistenceUnitName);

descriptor.setExcludeUnlistedClasses(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, did you figure that this was necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary as you forced it in LightPersistence... but... I'm not sure we will keep it that way so wanted to be extra sure it wouldn't break if we change it later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks I was just puzzled.

@@ -126,7 +126,7 @@

public static final String HIBERNATE_ORM_CONFIG_PREFIX = "quarkus.hibernate-orm.";
public static final String NO_SQL_LOAD_SCRIPT_FILE = "no-file";
public static final String DEFAULT_PERSISTENCE_UNIT_NAME = "default";
public static final String DEFAULT_PERSISTENCE_UNIT_NAME = "<default>";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "has special meaning", is it not possible for a user to speficy this name explicitly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to avoid a conflict between the default one and a potential named PU called "default". Thus why I added the special characters. Also, it's consistent with the Agroal extension.

quarkus.hibernate-orm.persistence-units."users".dialect=org.hibernate.dialect.H2Dialect
quarkus.hibernate-orm.persistence-units."users".log.sql=true
quarkus.hibernate-orm.persistence-units."users".database.generation=drop-and-create
quarkus.hibernate-orm.persistence-units."users".datasource=users
Copy link
Member

@Sanne Sanne Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment somewhere to remind why this is an invalid configuration? I guess because it's missing the package names filter packages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

return jtaEnabled.get();
public EntityManagerFactory getEntityManagerFactory(String unitName) {
LazyPersistenceUnit lazyPersistenceUnit = null;
if (unitName == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this condition check useful? I was assuming that unitName would be always defined, at most <default>.

If I'm wrong, we might want to improve the following (nested) if so that the default can still be used/injected even when there's more than one PU defined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always set except when you're explicitly calling null. This was the case somewhere in the code base so I decided to make it null safe just to be on the safe side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it feels a bit smelly but we can clean it up later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it safe not smelly. I'm not sure I understand your problem.

Everything is working fine with the default injection, we have tests for that. The null test is there just if someone calls it programmatically. When dealing with injection, you pass the default unit name (which is not null).

For me passing null is not really valid, I only support it because someone used it (and I changed that call to not use it) and I thought maybe someone else could have.

Copy link
Member

@Sanne Sanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll approve as my minor comments and questions are certainly not blocking, but please have a look at them.

@gsmet gsmet merged commit 9cd6e1b into quarkusio:master Aug 21, 2020
@gsmet gsmet changed the title Multiple persistence units infrastructure Support multiple Hibernate ORM persistence units via Quarkus configuration Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support multiple persistence units
5 participants