From 360fec82f1ef25ca83cae405af1130c6e437301e Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Mon, 20 Dec 2021 12:45:15 +0100 Subject: [PATCH] Reset connection pool after changing system TZ. Don't use distinct predicate when comparing primary keys in envers --- .../test/type/AbstractJavaTimeTypeTest.java | 5 ++++ .../test/resources/junit-platform.properties | 1 - .../entities/mapper/id/AbstractIdMapper.java | 23 +++++++++++++++++++ .../internal/entities/mapper/id/IdMapper.java | 13 +++++++++++ .../mapper/relation/ToOneIdMapper.java | 2 +- .../orm/jdbc/TimeZoneConnectionProvider.java | 5 ++++ 6 files changed, 47 insertions(+), 2 deletions(-) delete mode 100644 hibernate-core/src/test/resources/junit-platform.properties diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractJavaTimeTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractJavaTimeTypeTest.java index 84656726dd..918dac32e9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractJavaTimeTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractJavaTimeTypeTest.java @@ -31,6 +31,7 @@ import org.hibernate.dialect.H2Dialect; import org.hibernate.service.ServiceRegistry; import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jdbc.SharedDriverManagerConnectionProviderImpl; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.CustomParameterized; import org.hibernate.testing.orm.junit.DialectContext; @@ -199,6 +200,8 @@ public abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalT protected final void withDefaultTimeZone(Runnable runnable) { TimeZone timeZoneBefore = TimeZone.getDefault(); TimeZone.setDefault( toTimeZone( env.defaultJvmTimeZone ) ); + // Clear the connection pool to avoid issues with drivers that initialize the session TZ to the system TZ + SharedDriverManagerConnectionProviderImpl.getInstance().reset(); /* * Run the code in a new thread, because some libraries (looking at you, h2 JDBC driver) * cache data dependent on the default timezone in thread local variables, @@ -227,6 +230,8 @@ public abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalT } finally { TimeZone.setDefault( timeZoneBefore ); + // Clear the connection pool to avoid issues with drivers that initialize the session TZ to the system TZ + SharedDriverManagerConnectionProviderImpl.getInstance().reset(); } } diff --git a/hibernate-core/src/test/resources/junit-platform.properties b/hibernate-core/src/test/resources/junit-platform.properties deleted file mode 100644 index 54ed866612..0000000000 --- a/hibernate-core/src/test/resources/junit-platform.properties +++ /dev/null @@ -1 +0,0 @@ -junit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer$ClassName \ No newline at end of file diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/AbstractIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/AbstractIdMapper.java index f5e21eb22d..a4430cfb5e 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/AbstractIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/AbstractIdMapper.java @@ -65,6 +65,29 @@ public abstract class AbstractIdMapper extends AbstractMapper implements IdMappe final Parameters parametersToUse = getParametersToUse( parameters, paramDatas1 ); + final Iterator paramDataIter1 = paramDatas1.iterator(); + final Iterator paramDataIter2 = paramDatas2.iterator(); + while ( paramDataIter1.hasNext() ) { + final QueryParameterData paramData1 = paramDataIter1.next(); + final QueryParameterData paramData2 = paramDataIter2.next(); + + parametersToUse.addWhere( + paramData1.getProperty( prefix1 ), + false, + "=", + paramData2.getProperty( prefix2 ), + false + ); + } + } + + @Override + public void addNullableIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2) { + final List paramDatas1 = mapToQueryParametersFromId( null ); + final List paramDatas2 = mapper2.mapToQueryParametersFromId( null ); + + final Parameters parametersToUse = getParametersToUse( parameters, paramDatas1 ); + final Iterator paramDataIter1 = paramDatas1.iterator(); final Iterator paramDataIter2 = paramDatas2.iterator(); while ( paramDataIter1.hasNext() ) { diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/IdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/IdMapper.java index dece33eeb9..98603f8bad 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/IdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/IdMapper.java @@ -83,6 +83,19 @@ public interface IdMapper { */ void addIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2); + /** + * Adds query statements, which contains restrictions, which express the property that the id of the entity + * with alias prefix1, is equal to the id of the entity with alias prefix2 mapped by the second mapper + * (the second mapper must be for the same entity, but it can have, for example, prefixed properties). + * + * @param parameters Parameters, to which to add the statements. + * @param prefix1 First alias of the entity + prefix to add to the properties. + * @param mapper2 Second mapper for the same entity, which will be used to get properties for the right side + * of the equation. + * @param prefix2 Second alias of the entity + prefix to add to the properties. + */ + void addNullableIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2); + /** * Adds query statements, which contains restrictions, which express the property that the id of the entity * with alias prefix, is equal to the given object. diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java index c35a9bd4a7..2e1976611f 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java @@ -193,7 +193,7 @@ public class ToOneIdMapper extends AbstractToOneMapper { String prefix1, String idPrefix2, String prefix2) { - delegate.addIdsEqualToQuery( parameters, prefix1, delegate, prefix2 ); + delegate.addNullableIdsEqualToQuery( parameters, prefix1, delegate, prefix2 ); } // todo: is referenced entity needed any longer? diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/jdbc/TimeZoneConnectionProvider.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/jdbc/TimeZoneConnectionProvider.java index 32ef2e6739..d2373284b1 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/jdbc/TimeZoneConnectionProvider.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/jdbc/TimeZoneConnectionProvider.java @@ -11,6 +11,7 @@ import java.util.TimeZone; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.testing.jdbc.ConnectionProviderDelegate; +import org.hibernate.testing.jdbc.SharedDriverManagerConnectionProviderImpl; /** * This {@link ConnectionProvider} extends any other ConnectionProvider that would be used by default taken the current configuration properties, and it @@ -28,6 +29,8 @@ public class TimeZoneConnectionProvider this.customTimeZone = customTimeZone; this.defaultTimeZone = System.setProperty( "user.timezone", customTimeZone); TimeZone.setDefault(TimeZone.getTimeZone( customTimeZone )); + // Clear the connection pool to avoid issues with drivers that initialize the session TZ to the system TZ + SharedDriverManagerConnectionProviderImpl.getInstance().reset(); } @Override @@ -35,5 +38,7 @@ public class TimeZoneConnectionProvider super.stop(); System.setProperty( "user.timezone", defaultTimeZone); TimeZone.setDefault(TimeZone.getTimeZone( defaultTimeZone )); + // Clear the connection pool to avoid issues with drivers that initialize the session TZ to the system TZ + SharedDriverManagerConnectionProviderImpl.getInstance().reset(); } }