Skip to content

<data-source> in persistence.xml#865

Open
gavinking wants to merge 11 commits intojakartaee:mainfrom
gavinking:datasource-persistence-xml
Open

<data-source> in persistence.xml#865
gavinking wants to merge 11 commits intojakartaee:mainfrom
gavinking:datasource-persistence-xml

Conversation

@gavinking
Copy link
Copy Markdown
Member

@gavinking gavinking commented Dec 28, 2025

This changeset realizes the idea proposed in #864, namely, that it should be possible to declare and configure a DataSource in the persistence xml file.

For example:

    <data-source name="jdbc/library">
        <description>My example persistence datasource</description>
        <class-name>org.postgresql.Driver</class-name>
        <url>jdbc:postgresql://localhost/library</url>
        <user>gavin</user>
        <password>p0ny</password>
        <isolation-level>TRANSACTION_READ_COMMITTED</isolation-level>
        <max-pool-size>100</max-pool-size>
        <max-idle-time>10000</max-idle-time>
    </data-source>

Or:

    <persistence-unit name="Library" transaction-type="JTA">
        <description>My example persistence unit</description>
        <data-source>
            <description>My example persistence datasource</description>
            <class-name>org.postgresql.Driver</class-name>
            <url>jdbc:postgresql://localhost/library</url>
            <user>gavin</user>
            <password>p0ny</password>
            <isolation-level>TRANSACTION_READ_COMMITTED</isolation-level>
            <max-pool-size>100</max-pool-size>
            <max-idle-time>10000</max-idle-time>
        </data-source>
        <class>org.example.Book</class>
        <class>org.example.Author</class>
        <class>org.example.Publisher</class>
        <exclude-unlisted-classes/>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    </persistence-unit>

@gavinking gavinking linked an issue Dec 29, 2025 that may be closed by this pull request
@gavinking gavinking changed the title datasource definitions in persistence xml <data-source> in persistence xml Dec 29, 2025
@gavinking gavinking changed the title <data-source> in persistence xml <data-source> in persistence.xml Dec 29, 2025
@gavinking gavinking added the candidate-for-4 Good candidate for JPA 4 label Dec 30, 2025
@gavinking gavinking force-pushed the datasource-persistence-xml branch from 2c14f0a to 9939b57 Compare January 2, 2026 18:20
Copy link
Copy Markdown
Member

@marko-bekhta marko-bekhta left a comment

Choose a reason for hiding this comment

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

added a few questions inline 🙂

Comment thread api/src/main/java/jakarta/persistence/PersistenceConfiguration.java Outdated
Comment thread api/src/main/java/jakarta/persistence/PersistenceConfiguration.java Outdated
Comment thread api/src/main/java/jakarta/persistence/PersistenceConfiguration.java Outdated
Comment on lines +446 to +450
Declares a container DataSource to be registered with JNDI. The
DataSource may be configured by setting the child elements for
commonly used DataSource properties. Additional standard and
vendor-specific properties may be specified using the properties
element.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what would happen if one passes any of the "predefined" properties through the properties element instead ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The exact same thing that happens as if you used @DataSrouceDefinition.

Comment on lines +623 to +625
If a DataSource property is specified in the properties element
and the annotation element for the property is also specified,
the annotation element value takes precedence.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh right, so we have some precedence expectations here, should we add the case from above for the predefined property ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As I said: I am reallly trying hard not to diverge from existing semantics of things that already exist.

I am aware that the existing semantics probably kinda blow. But it would be worse to deviate from them.

A JDBC URL. If the url annotation element contains a DataSource
property that was also specified using the corresponding
annotation element, the precedence order is undefined and
implementation specific.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

implementation vs vendor specific -- if I understand you correctly here, implementation is about the Jakarta Persistence implementation and vendor is about the database behind the datasource, so maybe it's worth to be more specific here so there's no possible confusion 🙂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

if I understand you correctly here

It's not me you're trying to understand. I copy/pasted all that text verbatim.

Comment on lines +118 to +121
It may contain:

- any number of child `persistence-unit` elements, along with
- any number of child `data-source` elements.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the way the persistence element is defined (a sequence) it would mean that it's any number of datasources followed by any number of persistence units (since sequence does not allow random order of elements)

Copy link
Copy Markdown
Member Author

@gavinking gavinking Jan 8, 2026

Choose a reason for hiding this comment

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

Yeah actually I dislike how everything in persistence.xml has a defined order, even though no such natural order exists. The order is not semantically meaningful nor relevant.

<xsd:complexType name="data-source-type">
<xsd:annotation>
<xsd:documentation>
Declares a container DataSource to be registered with JNDI. The
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this complex type is reused for top level and for the PU child element, and for the PU we later have

datasource may be declared as a child of the persistence-unit element, in
which case it may or may not be bound to JNDI

soo should we be saying that DataSource to be registered with JNDI ?

Comment thread spec/src/main/asciidoc/ch08-entity-packaging.adoc Outdated
@gavinking gavinking force-pushed the datasource-persistence-xml branch from cf74f08 to 5e8de8b Compare January 9, 2026 07:16
@gavinking
Copy link
Copy Markdown
Member Author

For reasons explained in the issue, I have removed the proposal for a DataSourceConfiguration object from this proposal.

@gavinking
Copy link
Copy Markdown
Member Author

@scottmarlow @starksm64 @bstansberry I know we have already discussed this via email, but is there anything you would like to add here, or should I just move forward with this?

@njr-11 Anything to say from the Liberty side?

@njr-11
Copy link
Copy Markdown
Member

njr-11 commented Feb 5, 2026

@njr-11 Anything to say from the Liberty side?

I'm not in favor of duplicating <data-source> into persistence.xml when we already have it in web.xml and application.xml. I don't believe it adds value on the Jakarta EE app server side, and it introduces complexity there with how this merges or overrides definitions (and annotations) of the same name that are found in the existing locations. I can also see it being a likely outcome that Jakarta EE will de-emphasize/deprecate <data-source> in favor of something better once we have Jakarta Config.

@gavinking
Copy link
Copy Markdown
Member Author

I'm not in favor of duplicating <data-source> into persistence.xml when we already have it in web.xml and application.xml. I don't believe it adds value on the Jakarta EE app server side

If we consider only Jakarta EE in isolation then I agree.

The problem is that web.xml and application.xml aren't available in the Java SE standalone case, and that creates various kinds of friction for testing and other scenarios where people are using the same persistence unit both in EE and SE.

I can also see it being a likely outcome that Jakarta EE will de-emphasize/deprecate <data-source> in favor of something better once we have Jakarta Config.

That's entirely possible, but there's no guarantee that Jakarta Config—if and when it arrives—will actually address Java SE programs or be something that people will embrace for usage outside of the EE platform. (And it also implies an additional dependency.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

candidate-for-4 Good candidate for JPA 4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

declare DataSources in persistence.xml

3 participants