Skip to content

[FEATURE REQUEST] Message handler - To intercept messages and potentially downgrade any SQLException to SQLWarning #1299

@goranschwarz

Description

@goranschwarz

Message handler - To intercept messages and potentially downgrade any SQLException to SQLWarning

This can for example be used to:

  • logg all messages into a DB-access-errorlog
  • simulate clients like 'sqlcmd' or 'ssms'... where a SQL Batch isn't aborted on first error, instead it continues to "read out" the TDS Stream

I'm not sure where the best entry point for adding this functionality, but possibly the TDS Parser, onError() then call the installed message handler (for the Connection)...

  • if message handler returns SQLException then add it to the SQLExceptions list
  • if message handler returns SQLWarning then add it to the SQLWarnings list

Below is an example of Sybase jConnect implementation of this:

// Create a new message handler which will be used for jConnect
SybMessageHandler newMsgHandler = new SybMessageHandler()
{
	@Override
	public SQLException messageHandler(SQLException sqle)
	{
		// Increment Statistics
		if (sqle instanceof SQLWarning)
			incSqlWarningCount();
		else
			incSqlExceptionCount();

		// Add it to the progress dialog
		progress.addMessage(sqle);

		// If we want to STOP if we get any errors...
		// Then we should return the origin Exception
		// SQLException will abort current SQL Batch, while SQLWarnings will continue to execute
		if (_abortOnDbMessages)
			return sqle;

		// Downgrade ALL messages to SQLWarnings, so executions wont be interuppted.
		return AseConnectionUtils.sqlExceptionToWarning(sqle);
	}
};

if (conn instanceof SybConnection)
{
	// get current message handler, so we can restore it later on
	curMsgHandler = ((SybConnection)conn).getSybMessageHandler();

	// Install the new message handler
	((SybConnection)conn).setSybMessageHandler(newMsgHandler);
}

FYI: Below is the interface used in jConnect... if something similar is needed...

public interface SybMessageHandler 
{
	SQLException messageHandler(SQLException ex);
}

and normally a SybSQLException also implemets a interface called EedInfo, which basically holds the same methods as https://github.com/microsoft/mssql-jdbc/blob/d4362039923a77caa080152d75d18fc8d1c7ff88/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerError.java

public interface EedInfo {
	int getState();
	int getSeverity();
	String getServerName();
	String getProcedureName();
	int getLineNumber();
	ResultSet getEedParams();
	int getTranState();
	int getStatus();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementAn enhancement to the driver. Lower priority than bugs.

    Type

    No type

    Projects

    Status

    Closed Issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions