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

JDBC optimisations: skip warnings reset, entrust the Connection Pool to do it #3264

Open
wants to merge 6 commits into
base: 5.6
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public boolean supportsAggressiveRelease() {
return false;
}

@Override
public boolean connectionWarningsResetCanBeSkippedOnClose() {
return true;
}

@Override
@SuppressWarnings( "rawtypes" )
public boolean isUnwrappableAs(Class unwrapType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.agroal.skipwarnings;

import java.util.Properties;
import javax.persistence.Entity;
import javax.persistence.Id;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.agroal.util.PreparedStatementSpyConnectionProvider;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;

/**
* Testing the impact of org.hibernate.agroal.internal.AgroalConnectionProvider#connectionWarningsResetCanBeSkippedOnClose()
* returning true, and without setting a configuration option explicitly for LOG_JDBC_WARNINGS.
*
* @author Sanne Grinovero
*/
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class)
public class AgroalSkipClearWarningsByDefaultTest extends BaseCoreFunctionalTestCase {

private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(true );

@Override
protected void configure(Configuration configuration) {
Properties properties = configuration.getProperties();
properties.put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider );
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{ City.class };
}

@Test
public void test() {
connectionProvider.clear();
doInHibernate( this::sessionFactory, session -> {
City city = new City();
city.setId( 1L );
city.setName( "London" );
session.persist( city );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
} );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
doInHibernate( this::sessionFactory, session -> {
City city = session.find( City.class, 1L );
assertEquals( "London", city.getName() );
} );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
}

@Entity(name = "City" )
public static class City {

@Id
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.agroal.skipwarnings;

import java.util.Properties;
import javax.persistence.Entity;
import javax.persistence.Id;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.agroal.util.PreparedStatementSpyConnectionProvider;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;

/**
* Testing the impact of org.hibernate.agroal.internal.AgroalConnectionProvider#connectionWarningsResetCanBeSkippedOnClose()
* returning true.
*
* @author Sanne Grinovero
*/
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class)
public class AgroalSkipClearWarningsTest extends BaseCoreFunctionalTestCase {

private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(true );

@Override
protected void configure(Configuration configuration) {
Properties properties = configuration.getProperties();
//Disable logging of JDBC warnings, as that has an impact:
properties.put( AvailableSettings.LOG_JDBC_WARNINGS, false );
properties.put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider );
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{ City.class };
}

@Test
public void test() {
connectionProvider.clear();
doInHibernate( this::sessionFactory, session -> {
City city = new City();
city.setId( 1L );
city.setName( "London" );
session.persist( city );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
} );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
doInHibernate( this::sessionFactory, session -> {
City city = session.find( City.class, 1L );
assertEquals( "London", city.getName() );
} );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
}

@Entity(name = "City" )
public static class City {

@Id
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.agroal.skipwarnings;

import java.util.Properties;
import javax.persistence.Entity;
import javax.persistence.Id;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.agroal.util.PreparedStatementSpyConnectionProvider;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;

/**
* Testing the impact of org.hibernate.agroal.internal.AgroalConnectionProvider#connectionWarningsResetCanBeSkippedOnClose()
* returning false.
*
* @author Sanne Grinovero
*/
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class)
public class SkipClearWarningsDisabledTest extends BaseCoreFunctionalTestCase {

private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( false );

@Override
protected void configure(Configuration configuration) {
Properties properties = configuration.getProperties();
//Disable logging of JDBC warnings, as that has an impact:
properties.put( AvailableSettings.LOG_JDBC_WARNINGS, false );
properties.put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider );
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{ City.class };
}

@Test
public void test() {
connectionProvider.clear();
doInHibernate( this::sessionFactory, session -> {
City city = new City();
city.setId( 1L );
city.setName( "London" );
session.persist( city );
assertEquals( 0, connectionProvider.getCountInvocationsToClearWarnings() );
} );
assertEquals( 1, connectionProvider.getCountInvocationsToClearWarnings() );
doInHibernate( this::sessionFactory, session -> {
City city = session.find( City.class, 1L );
assertEquals( "London", city.getName() );
} );
assertEquals( 2, connectionProvider.getCountInvocationsToClearWarnings() );
}

@Entity(name = "City" )
public static class City {

@Id
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.hibernate.agroal.internal.AgroalConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand All @@ -34,8 +35,15 @@ public class PreparedStatementSpyConnectionProvider extends AgroalConnectionProv

private final List<Connection> acquiredConnections = new ArrayList<>( );
private final List<Connection> releasedConnections = new ArrayList<>( );
private final AtomicInteger clearWarningsCallCounter = new AtomicInteger( 0 );
private final boolean skipWarningsOnClose;

public PreparedStatementSpyConnectionProvider() {
this(true);
}

public PreparedStatementSpyConnectionProvider(boolean skipWarningsOnClose) {
this.skipWarningsOnClose = skipWarningsOnClose;
}

protected Connection actualConnection() throws SQLException {
Expand All @@ -62,6 +70,11 @@ public void stop() {
super.stop();
}

@Override
public boolean connectionWarningsResetCanBeSkippedOnClose() {
return skipWarningsOnClose;
}

private Connection spy(Connection connection) {
if ( MockUtil.isMock( connection ) ) {
return connection;
Expand All @@ -76,6 +89,12 @@ private Connection spy(Connection connection) {
return statementSpy;
} ).when( connectionSpy ).prepareStatement( ArgumentMatchers.anyString() );

Mockito.doAnswer( invocation -> {
invocation.callRealMethod();
clearWarningsCallCounter.incrementAndGet();
return null;
} ).when( connectionSpy ).clearWarnings();

Mockito.doAnswer( invocation -> {
Statement statement = (Statement) invocation.callRealMethod();
Statement statementSpy = Mockito.spy( statement );
Expand All @@ -96,6 +115,7 @@ public void clear() {
releasedConnections.clear();
preparedStatementMap.keySet().forEach( Mockito::reset );
preparedStatementMap.clear();
clearWarningsCallCounter.set( 0 );
}

/**
Expand All @@ -113,4 +133,8 @@ public List<Connection> getAcquiredConnections() {
public List<Connection> getReleasedConnections() {
return releasedConnections;
}

public int getCountInvocationsToClearWarnings() {
return clearWarningsCallCounter.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {

private boolean nativeExceptionHandling51Compliance;
private int queryStatisticsMaxSize;
private final boolean connectionWarningsResetCanBeSkippedOnClose;


@SuppressWarnings({"WeakerAccess", "deprecation"})
Expand Down Expand Up @@ -534,6 +535,8 @@ else if ( jdbcTimeZoneValue != null ) {
log.nativeExceptionHandling51ComplianceJpaBootstrapping();
this.nativeExceptionHandling51Compliance = false;
}

this.connectionWarningsResetCanBeSkippedOnClose = jdbcServices.getBootstrapJdbcConnectionAccess().connectionWarningsResetCanBeSkippedOnClose();
}

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -1072,6 +1075,11 @@ public boolean isOmitJoinOfSuperclassTablesEnabled() {
return omitJoinOfSuperclassTablesEnabled;
}

@Override
public boolean isConnectionWarningsResetCanBeSkippedOnClose() {
return connectionWarningsResetCanBeSkippedOnClose;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// In-flight mutation access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,9 @@ public boolean isEnhancementAsProxyEnabled() {
public boolean isOmitJoinOfSuperclassTablesEnabled() {
return delegate.isOmitJoinOfSuperclassTablesEnabled();
}

@Override
public boolean isConnectionWarningsResetCanBeSkippedOnClose() {
return delegate.isConnectionWarningsResetCanBeSkippedOnClose();
}
}
Loading