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

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

Closed
goranschwarz opened this issue Apr 1, 2020 · 1 comment · Fixed by #2251
Labels
Enhancement An enhancement to the driver. Lower priority than bugs.

Comments

@goranschwarz
Copy link
Contributor

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();
}
@goranschwarz goranschwarz added the Enhancement An enhancement to the driver. Lower priority than bugs. label Apr 1, 2020
@peterbae
Copy link
Contributor

peterbae commented Apr 1, 2020

Hi @goranschwarz, thanks for suggesting this. The team will add this item to the backlog for future improvements on the driver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement An enhancement to the driver. Lower priority than bugs.
Projects
Status: Closed Issues
Development

Successfully merging a pull request may close this issue.

2 participants