HHH-18313 NullPointerException because JdbcIsolationDelegate.sqlExceptionHelper() returns null

This commit is contained in:
Andrea Boriero 2024-07-03 11:13:32 +02:00 committed by Andrea Boriero
parent 3fcb58646b
commit 8cd501fa6e
9 changed files with 52 additions and 21 deletions

View File

@ -28,6 +28,7 @@ import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl;
import org.hibernate.engine.jdbc.internal.JdbcServicesImpl;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.internal.EmptyEventManager;
import org.hibernate.event.spi.EventManager;
@ -278,6 +279,7 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
final TemporaryJdbcSessionOwner temporaryJdbcSessionOwner = new TemporaryJdbcSessionOwner(
jdbcConnectionAccess,
jdbcServices,
new SqlExceptionHelper( false ),
registry
);
temporaryJdbcSessionOwner.transactionCoordinator = registry.requireService( TransactionCoordinatorBuilder.class )
@ -586,6 +588,7 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
private final boolean connectionProviderDisablesAutoCommit;
private final PhysicalConnectionHandlingMode connectionHandlingMode;
private final JpaCompliance jpaCompliance;
private final SqlExceptionHelper sqlExceptionHelper;
private static final EmptyJdbcObserver EMPTY_JDBC_OBSERVER = EmptyJdbcObserver.INSTANCE;
TransactionCoordinator transactionCoordinator;
private final EmptyEventManager eventManager;
@ -593,9 +596,11 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
public TemporaryJdbcSessionOwner(
JdbcConnectionAccess jdbcConnectionAccess,
JdbcServices jdbcServices,
SqlExceptionHelper sqlExceptionHelper,
ServiceRegistryImplementor serviceRegistry) {
this.jdbcConnectionAccess = jdbcConnectionAccess;
this.jdbcServices = jdbcServices;
this.sqlExceptionHelper = sqlExceptionHelper;
this.serviceRegistry = serviceRegistry;
final ConfigurationService configuration = serviceRegistry.requireService( ConfigurationService.class );
this.jtaTrackByThread = configuration.getSetting( JTA_TRACK_BY_THREAD, BOOLEAN, true );
@ -749,6 +754,11 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
return true;
}
@Override
public SqlExceptionHelper getSqlExceptionHelper() {
return sqlExceptionHelper;
}
private static class EmptyJdbcObserver implements JdbcObserver{
public static final EmptyJdbcObserver INSTANCE = new EmptyJdbcObserver();

View File

@ -92,8 +92,8 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
this.logicalConnection = new LogicalConnectionManagedImpl(
owner.getJdbcConnectionAccess(),
owner.getJdbcSessionContext(),
resourceRegistry,
jdbcServices
owner.getSqlExceptionHelper(),
resourceRegistry
);
}
this.owner = owner;

View File

@ -84,26 +84,20 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
@Override
public SqlExceptionHelper getSqlExceptionHelper() {
if ( jdbcEnvironment != null ) {
assert jdbcEnvironment != null : "JdbcEnvironment was not found";
return jdbcEnvironment.getSqlExceptionHelper();
}
return null;
}
@Override
public ExtractedDatabaseMetaData getExtractedMetaDataSupport() {
if ( jdbcEnvironment != null ) {
assert jdbcEnvironment != null : "JdbcEnvironment was not found";
return jdbcEnvironment.getExtractedDatabaseMetaData();
}
return null;
}
@Override
public LobCreator getLobCreator(LobCreationContext lobCreationContext) {
if ( jdbcEnvironment != null ) {
assert jdbcEnvironment != null : "JdbcEnvironment was not found";
return jdbcEnvironment.getLobCreatorBuilder().buildLobCreator( lobCreationContext );
}
return null;
}
}

View File

@ -54,8 +54,8 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
public LogicalConnectionManagedImpl(
JdbcConnectionAccess jdbcConnectionAccess,
JdbcSessionContext jdbcSessionContext,
ResourceRegistry resourceRegistry,
JdbcServices jdbcServices) {
SqlExceptionHelper sqlExceptionHelper,
ResourceRegistry resourceRegistry) {
this.jdbcConnectionAccess = jdbcConnectionAccess;
this.observer = jdbcSessionContext.getObserver();
this.resourceRegistry = resourceRegistry;
@ -64,7 +64,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
jdbcSessionContext.getPhysicalConnectionHandlingMode(),
jdbcConnectionAccess );
this.sqlExceptionHelper = jdbcServices.getSqlExceptionHelper();
this.sqlExceptionHelper = sqlExceptionHelper;
if ( connectionHandlingMode.getAcquisitionMode() == IMMEDIATELY ) {
acquireConnectionIfNeeded();
@ -82,6 +82,19 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
}
}
public LogicalConnectionManagedImpl(
JdbcConnectionAccess jdbcConnectionAccess,
JdbcSessionContext jdbcSessionContext,
ResourceRegistry resourceRegistry,
JdbcServices jdbcServices) {
this(
jdbcConnectionAccess,
jdbcSessionContext,
jdbcServices.getSqlExceptionHelper(),
resourceRegistry
);
}
private PhysicalConnectionHandlingMode determineConnectionHandlingMode(
PhysicalConnectionHandlingMode connectionHandlingMode,
JdbcConnectionAccess jdbcConnectionAccess) {

View File

@ -7,6 +7,7 @@
package org.hibernate.resource.jdbc.spi;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.event.spi.EventManager;
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
@ -72,4 +73,8 @@ public interface JdbcSessionOwner {
Integer getJdbcBatchSize();
EventManager getEventManager();
default SqlExceptionHelper getSqlExceptionHelper() {
return getJdbcSessionContext().getJdbcServices().getSqlExceptionHelper();
}
}

View File

@ -37,7 +37,7 @@ public class JdbcIsolationDelegate implements IsolationDelegate {
public JdbcIsolationDelegate(JdbcSessionOwner jdbcSessionOwner) {
this(
jdbcSessionOwner.getJdbcConnectionAccess(),
jdbcSessionOwner.getJdbcSessionContext().getJdbcServices().getSqlExceptionHelper()
jdbcSessionOwner.getSqlExceptionHelper()
);
}

View File

@ -50,7 +50,7 @@ public class JtaIsolationDelegate implements IsolationDelegate {
public JtaIsolationDelegate(JdbcSessionOwner jdbcSessionOwner, TransactionManager transactionManager) {
this(
jdbcSessionOwner.getJdbcConnectionAccess(),
jdbcSessionOwner.getJdbcSessionContext().getJdbcServices().getSqlExceptionHelper(),
jdbcSessionOwner.getSqlExceptionHelper(),
transactionManager
);
}

View File

@ -9,6 +9,7 @@ package org.hibernate.orm.test.connection;
import java.sql.Connection;
import java.sql.Driver;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -17,7 +18,10 @@ import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.internal.JdbcServicesImpl;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.exception.JDBCConnectionException;
@ -75,7 +79,12 @@ public class ConnectionCreatorTest extends BaseUnitTestCase {
public <R extends Service> R getService(Class<R> serviceRole) {
if ( JdbcServices.class.equals( serviceRole ) ) {
// return a new, not fully initialized JdbcServicesImpl
return (R) new JdbcServicesImpl();
JdbcServicesImpl jdbcServices = new JdbcServicesImpl(this);
jdbcServices.configure( new HashMap<>() );
return (R) jdbcServices;
}
if( JdbcEnvironment.class.equals( serviceRole ) ){
return (R) new JdbcEnvironmentImpl( this, new H2Dialect() );
}
return super.getService( serviceRole );
}

View File

@ -79,7 +79,7 @@ public class JdbcCoordinatorTest {
SqlExceptionHelper sqlExceptionHelper = Mockito.mock( SqlExceptionHelper.class );
when( jdbcServices.getSqlExceptionHelper() ).thenReturn(
sqlExceptionHelper );
when(sessionOwner.getSqlExceptionHelper()).thenReturn( sqlExceptionHelper );
JdbcCoordinatorImpl jdbcCoordinator = new JdbcCoordinatorImpl(
null,
sessionOwner,