mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-27 14:30:16 +00:00
HHH-8621 - NPE in BasicConnectionCreator
This commit is contained in:
parent
1cb6be9d10
commit
202e46eef2
@ -27,8 +27,14 @@
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.JDBCException;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.exception.internal.SQLStateConversionDelegate;
|
||||
import org.hibernate.exception.spi.ConversionContext;
|
||||
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
|
||||
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
|
||||
import org.hibernate.internal.util.ValueHolder;
|
||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||
|
||||
/**
|
||||
@ -66,6 +72,9 @@ public String getUrl() {
|
||||
@Override
|
||||
public Connection createConnection() {
|
||||
final Connection conn = makeConnection( url, connectionProps );
|
||||
if ( conn == null ) {
|
||||
throw new HibernateException( "Unable to make JDBC Connection [" + url + "]" );
|
||||
}
|
||||
|
||||
try {
|
||||
if ( isolation != null ) {
|
||||
@ -88,8 +97,33 @@ public Connection createConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
private ValueHolder<SQLExceptionConversionDelegate> simpleConverterAccess = new ValueHolder<SQLExceptionConversionDelegate>(
|
||||
new ValueHolder.DeferredInitializer<SQLExceptionConversionDelegate>() {
|
||||
@Override
|
||||
public SQLExceptionConversionDelegate initialize() {
|
||||
return new SQLStateConversionDelegate(
|
||||
new ConversionContext() {
|
||||
@Override
|
||||
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
|
||||
// this should never happen...
|
||||
throw new HibernateException( "Unexpected call to org.hibernate.exception.spi.ConversionContext.getViolatedConstraintNameExtracter" );
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
protected JDBCException convertSqlException(String message, SQLException e) {
|
||||
return serviceRegistry.getService( JdbcServices.class ).getSqlExceptionHelper().convert( e, message, null );
|
||||
// if JdbcServices#getSqlExceptionHelper is available, use it...
|
||||
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
|
||||
if ( jdbcServices != null && jdbcServices.getSqlExceptionHelper() != null ) {
|
||||
return jdbcServices.getSqlExceptionHelper().convert( e, message, null );
|
||||
}
|
||||
|
||||
// likely we are still in the process of initializing the ServiceRegistry, so use the simplified
|
||||
// SQLException conversion
|
||||
return simpleConverterAccess.getValue().convert( e, message, null );
|
||||
}
|
||||
|
||||
protected abstract Connection makeConnection(String url, Properties connectionProps);
|
||||
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.connection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.boot.registry.StandardServiceInitiator;
|
||||
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
|
||||
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
|
||||
import org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator;
|
||||
import org.hibernate.engine.jdbc.internal.JdbcServicesImpl;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
import org.hibernate.service.Service;
|
||||
import org.hibernate.service.internal.ProvidedService;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ConnectionCreatorTest extends BaseUnitTestCase {
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-8621" )
|
||||
public void testBadUrl() throws Exception {
|
||||
DriverConnectionCreator connectionCreator = new DriverConnectionCreator(
|
||||
(Driver) Class.forName( "org.h2.Driver" ).newInstance(),
|
||||
new StandardServiceRegistryImpl(
|
||||
new BootstrapServiceRegistryImpl(),
|
||||
Collections.<StandardServiceInitiator>emptyList(),
|
||||
Collections.<ProvidedService>emptyList(),
|
||||
Collections.emptyMap()
|
||||
) {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
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();
|
||||
}
|
||||
return super.getService( serviceRole );
|
||||
}
|
||||
},
|
||||
"jdbc:h2:???:bad-url",
|
||||
new Properties(),
|
||||
false,
|
||||
null
|
||||
);
|
||||
|
||||
try {
|
||||
Connection conn = connectionCreator.createConnection();
|
||||
conn.close();
|
||||
fail( "Expecting the bad Connection URL to cause an exception" );
|
||||
}
|
||||
catch (JDBCConnectionException expected) {
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user