improve reporting of connection errors

This commit is contained in:
Gavin King 2024-01-19 19:46:57 +01:00
parent b771afda5b
commit d9af0eb7e1
4 changed files with 6 additions and 5 deletions

View File

@ -42,7 +42,7 @@ public class JDBCException extends HibernateException {
* @param sql The sql being executed when the exception occurred
*/
public JDBCException(String message, SQLException cause, String sql) {
super( message + " [" + sql + "]", cause );
super( sql == null ? message : message + " [" + sql + "]", cause );
this.message = message;
this.sqlException = cause;
this.sql = sql;

View File

@ -127,11 +127,12 @@ public abstract class BasicConnectionCreator implements ConnectionCreator {
);
protected JDBCException convertSqlException(String message, SQLException e) {
final String fullMessage = message + " [" + e.getMessage() + "]";
try {
// 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 );
return jdbcServices.getSqlExceptionHelper().convert( e, fullMessage );
}
}
catch (ServiceException se) {
@ -140,7 +141,7 @@ public abstract class BasicConnectionCreator implements ConnectionCreator {
// likely we are still in the process of initializing the ServiceRegistry, so use the simplified
// SQLException conversion
return simpleConverterAccess.getValue().convert( e, message, null );
return simpleConverterAccess.getValue().convert( e, fullMessage, null );
}
protected abstract Connection makeConnection(String url, Properties connectionProps);

View File

@ -40,7 +40,7 @@ public class DriverConnectionCreator extends BasicConnectionCreator {
return driver.connect( url, connectionProps );
}
catch (SQLException e) {
throw convertSqlException( "Error calling Driver#connect", e );
throw convertSqlException( "Error calling Driver.connect()", e );
}
}
}

View File

@ -36,7 +36,7 @@ public class DriverManagerConnectionCreator extends BasicConnectionCreator {
return DriverManager.getConnection( url, connectionProps );
}
catch (SQLException e) {
throw convertSqlException( "Error calling DriverManager#getConnection", e );
throw convertSqlException( "Error calling DriverManager.getConnection()", e );
}
}
}