Skip to content

Commit

Permalink
[4.x] Switches default JPA CDI portable extension to PersistenceExten…
Browse files Browse the repository at this point in the history
…sion from JpaExtension (helidon-io#7719)

* Switches default JPA CDI portable extension to PersistenceExtension from JpaExtension

Signed-off-by: Laird Nelson <[email protected]>
  • Loading branch information
ljnelson authored and dalexandrov committed Oct 17, 2023
1 parent 2c8123b commit 2933196
Show file tree
Hide file tree
Showing 28 changed files with 385 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,15 +15,23 @@
*/
package io.helidon.integrations.cdi.hibernate;

import java.lang.System.Logger;
import java.util.Objects;
import java.util.Properties;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.persistence.spi.PersistenceUnitInfo;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.UserTransaction;
import org.hibernate.engine.jndi.spi.JndiService;
import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;

import static java.lang.System.Logger.Level.DEBUG;
import static org.hibernate.cfg.AvailableSettings.CONNECTION_HANDLING;
import static org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION;

/**
* An {@link AbstractJtaPlatform} that is an {@link ApplicationScoped}
* CDI managed bean that supplies {@link TransactionManager} and
Expand All @@ -35,74 +43,119 @@
*/
@ApplicationScoped
public class CDISEJtaPlatform extends AbstractJtaPlatform {
private final TransactionManager transactionManager;

private final UserTransaction userTransaction;

private static final long serialVersionUID = 1L;

/**
* Creates a new {@link CDISEJtaPlatform}.
*
* @param transactionManager the {@link TransactionManager} to use;
* must not be {@code null}
*
* @param userTransaction the {@link UserTransaction} to use; must
* not be {@code null}
*
* @exception NullPointerException if either {@code
* transactionManager} or {@code userTransaction} is {@code null}
*/
@Inject
public CDISEJtaPlatform(final TransactionManager transactionManager,
final UserTransaction userTransaction) {
super();
this.transactionManager = Objects.requireNonNull(transactionManager);
this.userTransaction = Objects.requireNonNull(userTransaction);
}

/**
* Throws an {@link UnsupportedOperationException} when invoked.
*
* @return (not applicable)
*
* @exception UnsupportedOperationException when invoked
*/
@Override
protected JndiService jndiService() {
throw new UnsupportedOperationException();
}

/**
* Returns the {@link UserTransaction} instance supplied at
* {@linkplain #CDISEJtaPlatform(TransactionManager,
* UserTransaction) construction time}.
*
* <p>This method never returns {@code null}.</p>
*
* @return a non-{@code null} {@link UserTransaction}
*
* @see #CDISEJtaPlatform(TransactionManager, UserTransaction)
*/
@Override
protected UserTransaction locateUserTransaction() {
return this.userTransaction;
}

/**
* Returns the {@link TransactionManager} instance supplied at
* {@linkplain #CDISEJtaPlatform(TransactionManager,
* UserTransaction) construction time}.
*
* <p>This method never returns {@code null}.</p>
*
* @return a non-{@code null} {@link TransactionManager}
*
* @see #CDISEJtaPlatform(TransactionManager, UserTransaction)
*/
@Override
protected TransactionManager locateTransactionManager() {
return this.transactionManager;
}

private static final Logger LOGGER = System.getLogger(CDISEJtaPlatform.class.getName());

private static final long serialVersionUID = 1L;

private transient TransactionManager transactionManager;

private transient UserTransaction userTransaction;

/**
* Creates a new {@link CDISEJtaPlatform}.
*
* @deprecated <a href="https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0#unproxyable">Required by the
* CDI specification</a> and not intended for end-user use.
*/
@Deprecated
CDISEJtaPlatform() {
super();
this.transactionManager = null;
this.userTransaction = null;
}

/**
* Creates a new {@link CDISEJtaPlatform}.
*
* @param transactionManager the {@link TransactionManager} to use;
* must not be {@code null}
*
* @param userTransaction the {@link UserTransaction} to use; must
* not be {@code null}
*
* @exception NullPointerException if either {@code
* transactionManager} or {@code userTransaction} is {@code null}
*/
@Inject
public CDISEJtaPlatform(final TransactionManager transactionManager,
final UserTransaction userTransaction) {
super();
this.transactionManager = Objects.requireNonNull(transactionManager);
this.userTransaction = Objects.requireNonNull(userTransaction);
}

@Override
protected boolean canCacheUserTransaction() {
return true;
}

/**
* Throws an {@link UnsupportedOperationException} when invoked.
*
* @return (not applicable)
*
* @exception UnsupportedOperationException when invoked
*/
@Override
protected JndiService jndiService() {
throw new UnsupportedOperationException();
}

/**
* Returns the {@link UserTransaction} instance supplied at
* {@linkplain #CDISEJtaPlatform(TransactionManager,
* UserTransaction) construction time}.
*
* <p>This method never returns {@code null}.</p>
*
* @return a non-{@code null} {@link UserTransaction}
*
* @see #CDISEJtaPlatform(TransactionManager, UserTransaction)
*/
@Override
protected UserTransaction locateUserTransaction() {
return this.userTransaction;
}

/**
* Returns the {@link TransactionManager} instance supplied at
* {@linkplain #CDISEJtaPlatform(TransactionManager,
* UserTransaction) construction time}.
*
* <p>This method never returns {@code null}.</p>
*
* @return a non-{@code null} {@link TransactionManager}
*
* @see #CDISEJtaPlatform(TransactionManager, UserTransaction)
*/
@Override
protected TransactionManager locateTransactionManager() {
return this.transactionManager;
}

/**
* Customizes the supplied {@link PersistenceUnitInfo}, when it is fired as a CDI event by, for example, the {@code
* io.helidon.integrations.cdi.jpa.PersistenceExtension} portable extension, by ensuring that certain important
* Hibernate properties are always set on the persistence unit.
*
* @param pui the {@link PersistenceUnitInfo} to customize; must not be {@code null}
*
* @exception NullPointerException if {@code pui} is {@code null}
*
* @see
* org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode#DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
*/
private static void customizePersistenceUnitInfo(@Observes PersistenceUnitInfo pui) {
Properties p = pui.getProperties();
if (p != null && p.getProperty(CONNECTION_HANDLING) == null && p.get(CONNECTION_HANDLING) == null) {
if (LOGGER.isLoggable(DEBUG)) {
LOGGER.log(DEBUG, "Setting " + CONNECTION_HANDLING + " property to "
+ DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
+ " on persistence unit " + pui.getPersistenceUnitName());
}
p.setProperty(CONNECTION_HANDLING, DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION.toString());
}
}

}
10 changes: 5 additions & 5 deletions integrations/cdi/hibernate-cdi/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
@Aot(description = "Experimental support, tested on limited use cases")
@SuppressWarnings({ "requires-automatic", "requires-transitive-automatic" })
module io.helidon.integrations.cdi.hibernate {
requires jakarta.cdi;
requires jakarta.inject;
requires java.sql;

requires static io.helidon.common.features.api;

requires transitive jakarta.cdi;
requires transitive jakarta.inject;
requires jakarta.persistence;
requires transitive jakarta.transaction;
requires transitive org.hibernate.orm.core;

requires static io.helidon.common.features.api;

exports io.helidon.integrations.cdi.hibernate;

provides org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,8 +34,12 @@
* @see PersistenceUnitInfoBean.DataSourceProvider
*
* @see JtaDataSourceProvider
*
* @deprecated This is an internal class used by the now-deprecated {@link JpaExtension} class. Its replacement is an
* internal class, {@link JtaAbsentDataSourceProvider}, used by the {@link PersistenceExtension} class.
*/
@ApplicationScoped
@Deprecated(since = "4.0")
class BeanManagerBackedDataSourceProvider implements PersistenceUnitInfoBean.DataSourceProvider {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,10 @@
*
* <p>This qualifier must not be combined with {@link Extended},
* {@link JpaTransactionScoped} or {@link NonTransactional}.</p>
*
* @deprecated No replacement.
*/
@Deprecated(since = "4.0")
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({}) // can only be programmatically added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,7 +50,11 @@
* this class are not safe for concurrent use by multiple threads.</p>
*
* @see JpaExtension
*
* @deprecated This is an internal class used by the now-deprecated {@link JpaExtension} class. Its replacement is an
* internal detail of the {@link PersistenceExtension} class.
*/
@Deprecated(since = "4.0")
@Vetoed
class CdiTransactionScopedEntityManager extends DelegatingEntityManager {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;

@Deprecated(since = "4.0")
final class ClearingQuery extends DelegatingQuery {

private final EntityManager entityManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.StoredProcedureQuery;

@Deprecated(since = "4.0")
final class ClearingStoredProcedureQuery extends DelegatingStoredProcedureQuery {

private final EntityManager entityManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;

@Deprecated(since = "4.0")
final class ClearingTypedQuery<X> extends DelegatingTypedQuery<X> {

private final EntityManager entityManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,7 +46,10 @@
*
* @see PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo,
* Map)
*
* @deprecated This is an internal class used only by the now-deprecated {@link JpaExtension} class.
*/
@Deprecated(since = "4.0")
final class EntityManagerFactories {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,7 +38,10 @@
* requirements of the JPA specification.
*
* @see #createContainerManagedEntityManager(Instance, Set)
*
* @deprecated This is an internal class used only by the now-deprecated {@link JpaExtension} class.
*/
@Deprecated(since = "4.0")
final class EntityManagers {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
/**
* A {@link DelegatingEntityManager} created to support extended
* persistence contexts.
*
* @deprecated This is an internal class used by the now-deprecated {@link JpaExtension} class.
*/
@Deprecated(since = "4.0")
class ExtendedEntityManager extends DelegatingEntityManager {


Expand Down
Loading

0 comments on commit 2933196

Please sign in to comment.