| Method from com.mysql.jdbc.SQLError Detail: |
static SQLWarning convertShowWarningsToSQLWarnings(Connection connection) throws SQLException {
return convertShowWarningsToSQLWarnings(connection, 0, false);
}
Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances.
If 'forTruncationOnly' is true, only looks for truncation warnings, and
actually throws DataTruncation as an exception. |
static SQLWarning convertShowWarningsToSQLWarnings(Connection connection,
int warningCountIfKnown,
boolean forTruncationOnly) throws SQLException {
java.sql.Statement stmt = null;
java.sql.ResultSet warnRs = null;
SQLWarning currentWarning = null;
try {
if (warningCountIfKnown < 100) {
stmt = connection.createStatement();
if (stmt.getMaxRows() != 0) {
stmt.setMaxRows(0);
}
} else {
// stream large warning counts
stmt = connection.createStatement(
java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
}
/*
* +---------+------+---------------------------------------------+ |
* Level | Code | Message |
* +---------+------+---------------------------------------------+ |
* Warning | 1265 | Data truncated for column 'field1' at row 1 |
* +---------+------+---------------------------------------------+
*/
warnRs = stmt.executeQuery("SHOW WARNINGS"); //$NON-NLS-1$
while (warnRs.next()) {
int code = warnRs.getInt("Code"); //$NON-NLS-1$
if (forTruncationOnly) {
if (code == 1265 || code == 1264) {
DataTruncation newTruncation = new MysqlDataTruncation(
warnRs.getString("Message"), 0, false, false, 0, 0, code); //$NON-NLS-1$
if (currentWarning == null) {
currentWarning = newTruncation;
} else {
currentWarning.setNextWarning(newTruncation);
}
}
} else {
String level = warnRs.getString("Level"); //$NON-NLS-1$
String message = warnRs.getString("Message"); //$NON-NLS-1$
SQLWarning newWarning = new SQLWarning(message, SQLError
.mysqlToSqlState(code, connection
.getUseSqlStateCodes()), code);
if (currentWarning == null) {
currentWarning = newWarning;
} else {
currentWarning.setNextWarning(newWarning);
}
}
}
if (forTruncationOnly && (currentWarning != null)) {
throw currentWarning;
}
return currentWarning;
} finally {
SQLException reThrow = null;
if (warnRs != null) {
try {
warnRs.close();
} catch (SQLException sqlEx) {
reThrow = sqlEx;
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) {
// ideally, we'd use chained exceptions here,
// but we still support JDK-1.2.x with this driver
// which doesn't have them....
reThrow = sqlEx;
}
}
if (reThrow != null) {
throw reThrow;
}
}
}
Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances.
If 'forTruncationOnly' is true, only looks for truncation warnings, and
actually throws DataTruncation as an exception. |
public static SQLException createCommunicationsException(ConnectionImpl conn,
long lastPacketSentTimeMs,
long lastPacketReceivedTimeMs,
Exception underlyingException,
ExceptionInterceptor interceptor) {
SQLException exToReturn = null;
if (!Util.isJdbc4()) {
exToReturn = new CommunicationsException(conn, lastPacketSentTimeMs, lastPacketReceivedTimeMs, underlyingException);
} else {
try {
exToReturn = (SQLException) Util.handleNewInstance(JDBC_4_COMMUNICATIONS_EXCEPTION_CTOR, new Object[] {
conn, Constants.longValueOf(lastPacketSentTimeMs), Constants.longValueOf(lastPacketReceivedTimeMs), underlyingException}, interceptor);
} catch (SQLException sqlEx) {
// We should _never_ get this, but let's not swallow it either
return sqlEx;
}
}
if (THROWABLE_INIT_CAUSE_METHOD != null && underlyingException != null) {
try {
THROWABLE_INIT_CAUSE_METHOD.invoke(exToReturn, new Object[] {underlyingException});
} catch (Throwable t) {
// we're not going to muck with that here, since it's
// an error condition anyway!
}
}
if (interceptor != null) {
SQLException interceptedEx = interceptor.interceptException(exToReturn, conn);
if (interceptedEx != null) {
return interceptedEx;
}
}
return exToReturn;
}
|
public static String createLinkFailureMessageBasedOnHeuristics(ConnectionImpl conn,
long lastPacketSentTimeMs,
long lastPacketReceivedTimeMs,
Exception underlyingException,
boolean streamingResultSetInPlay) {
long serverTimeoutSeconds = 0;
boolean isInteractiveClient = false;
if (conn != null) {
isInteractiveClient = conn.getInteractiveClient();
String serverTimeoutSecondsStr = null;
if (isInteractiveClient) {
serverTimeoutSecondsStr = conn
.getServerVariable("interactive_timeout"); //$NON-NLS-1$
} else {
serverTimeoutSecondsStr = conn
.getServerVariable("wait_timeout"); //$NON-NLS-1$
}
if (serverTimeoutSecondsStr != null) {
try {
serverTimeoutSeconds = Long
.parseLong(serverTimeoutSecondsStr);
} catch (NumberFormatException nfe) {
serverTimeoutSeconds = 0;
}
}
}
StringBuffer exceptionMessageBuf = new StringBuffer();
if (lastPacketSentTimeMs == 0) {
lastPacketSentTimeMs = System.currentTimeMillis();
}
long timeSinceLastPacket = (System.currentTimeMillis() - lastPacketSentTimeMs) / 1000;
long timeSinceLastPacketMs = (System.currentTimeMillis() - lastPacketSentTimeMs);
long timeSinceLastPacketReceivedMs = (System.currentTimeMillis() - lastPacketReceivedTimeMs);
int dueToTimeout = DUE_TO_TIMEOUT_FALSE;
StringBuffer timeoutMessageBuf = null;
if (streamingResultSetInPlay) {
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.ClientWasStreaming")); //$NON-NLS-1$
} else {
if (serverTimeoutSeconds != 0) {
if (timeSinceLastPacket > serverTimeoutSeconds) {
dueToTimeout = DUE_TO_TIMEOUT_TRUE;
timeoutMessageBuf = new StringBuffer();
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.2")); //$NON-NLS-1$
if (!isInteractiveClient) {
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.3")); //$NON-NLS-1$
} else {
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.4")); //$NON-NLS-1$
}
}
} else if (timeSinceLastPacket > DEFAULT_WAIT_TIMEOUT_SECONDS) {
dueToTimeout = DUE_TO_TIMEOUT_MAYBE;
timeoutMessageBuf = new StringBuffer();
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.5")); //$NON-NLS-1$
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.6")); //$NON-NLS-1$
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.7")); //$NON-NLS-1$
timeoutMessageBuf.append(Messages
.getString("CommunicationsException.8")); //$NON-NLS-1$
}
if (dueToTimeout == DUE_TO_TIMEOUT_TRUE
|| dueToTimeout == DUE_TO_TIMEOUT_MAYBE) {
if (lastPacketReceivedTimeMs != 0) {
Object[] timingInfo = {
new Long(timeSinceLastPacketReceivedMs),
new Long(timeSinceLastPacketMs)
};
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.ServerPacketTimingInfo", //$NON-NLS-1$
timingInfo));
} else {
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", //$NON-NLS-1$
new Object[] { new Long(timeSinceLastPacketMs)}));
}
if (timeoutMessageBuf != null) {
exceptionMessageBuf.append(timeoutMessageBuf);
}
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.11")); //$NON-NLS-1$
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.12")); //$NON-NLS-1$
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.13")); //$NON-NLS-1$
} else {
//
// Attempt to determine the reason for the underlying exception
// (we can only make a best-guess here)
//
if (underlyingException instanceof BindException) {
if (conn.getLocalSocketAddress() != null
&& !Util.interfaceExists(conn
.getLocalSocketAddress())) {
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.LocalSocketAddressNotAvailable")); //$NON-NLS-1$
} else {
// too many client connections???
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.TooManyClientConnections")); //$NON-NLS-1$
}
}
}
}
if (exceptionMessageBuf.length() == 0) {
// We haven't figured out a good reason, so copy it.
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.20")); //$NON-NLS-1$
if (THROWABLE_INIT_CAUSE_METHOD == null &&
underlyingException != null) {
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.21")); //$NON-NLS-1$
exceptionMessageBuf.append(Util
.stackTraceToString(underlyingException));
}
if (conn != null && conn.getMaintainTimeStats()
&& !conn.getParanoid()) {
exceptionMessageBuf.append("\n\n"); //$NON-NLS-1$
if (lastPacketReceivedTimeMs != 0) {
Object[] timingInfo = {
new Long(timeSinceLastPacketReceivedMs),
new Long(timeSinceLastPacketMs)
};
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.ServerPacketTimingInfo", //$NON-NLS-1$
timingInfo));
} else {
exceptionMessageBuf.append(Messages
.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", //$NON-NLS-1$
new Object[] { new Long(timeSinceLastPacketMs)}));
}
}
}
return exceptionMessageBuf.toString();
}
Creates a communications link failure message to be used
in CommunicationsException that (hopefully) has some better
information and suggestions based on heuristics. |
public static SQLException createSQLException(String message,
ExceptionInterceptor interceptor) {
return createSQLException(message, interceptor, null);
}
|
public static SQLException createSQLException(String message,
String sqlState,
ExceptionInterceptor interceptor) {
return createSQLException(message, sqlState, 0, interceptor);
}
|
public static SQLException createSQLException(String message,
ExceptionInterceptor interceptor,
Connection conn) {
SQLException sqlEx = new SQLException(message);
if (interceptor != null) {
SQLException interceptedEx = interceptor.interceptException(sqlEx, conn);
if (interceptedEx != null) {
return interceptedEx;
}
}
return sqlEx;
}
|
public static SQLException createSQLException(String message,
String sqlState,
Throwable cause,
ExceptionInterceptor interceptor) {
return createSQLException(message, sqlState, cause, interceptor, null);
}
|
public static SQLException createSQLException(String message,
String sqlState,
int vendorErrorCode,
ExceptionInterceptor interceptor) {
return createSQLException(message, sqlState, vendorErrorCode, false, interceptor);
}
|
public static SQLException createSQLException(String message,
String sqlState,
Throwable cause,
ExceptionInterceptor interceptor,
Connection conn) {
if (THROWABLE_INIT_CAUSE_METHOD == null) {
if (cause != null) {
message = message + " due to " + cause.toString();
}
}
SQLException sqlEx = createSQLException(message, sqlState, interceptor);
if (cause != null && THROWABLE_INIT_CAUSE_METHOD != null) {
try {
THROWABLE_INIT_CAUSE_METHOD.invoke(sqlEx, new Object[] {cause});
} catch (Throwable t) {
// we're not going to muck with that here, since it's
// an error condition anyway!
}
}
if (interceptor != null) {
SQLException interceptedEx = interceptor.interceptException(sqlEx, conn);
if (interceptedEx != null) {
return interceptedEx;
}
}
return sqlEx;
}
|
public static SQLException createSQLException(String message,
String sqlState,
int vendorErrorCode,
boolean isTransient,
ExceptionInterceptor interceptor) {
return createSQLException(message, sqlState, vendorErrorCode, false, interceptor, null);
}
|
public static SQLException createSQLException(String message,
String sqlState,
int vendorErrorCode,
boolean isTransient,
ExceptionInterceptor interceptor,
Connection conn) {
try {
SQLException sqlEx = null;
if (sqlState != null) {
if (sqlState.startsWith("08")) {
if (isTransient) {
if (!Util.isJdbc4()) {
sqlEx = new MySQLTransientConnectionException(
message, sqlState, vendorErrorCode);
} else {
sqlEx = (SQLException) Util
.getInstance(
"com.mysql.jdbc.exceptions.jdbc4.MySQLTransientConnectionException",
new Class[] { String.class,
String.class, Integer.TYPE },
new Object[] { message, sqlState,
Constants.integerValueOf(vendorErrorCode) }, interceptor);
}
} else if (!Util.isJdbc4()) {
sqlEx = new MySQLNonTransientConnectionException(
message, sqlState, vendorErrorCode);
} else {
sqlEx = (SQLException) Util.getInstance(
"com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException",
new Class[] { String.class, String.class,
Integer.TYPE }, new Object[] {
message, sqlState,
Constants.integerValueOf(vendorErrorCode) }, interceptor);
}
} else if (sqlState.startsWith("22")) {
if (!Util.isJdbc4()) {
sqlEx = new MySQLDataException(message, sqlState,
vendorErrorCode);
} else {
sqlEx = (SQLException) Util
.getInstance(
"com.mysql.jdbc.exceptions.jdbc4.MySQLDataException",
new Class[] { String.class, String.class,
Integer.TYPE }, new Object[] {
message, sqlState,
Constants.integerValueOf(vendorErrorCode) }, interceptor);
}
} else if (sqlState.startsWith("23")) {
if (!Util.isJdbc4()) {
sqlEx = new MySQLIntegrityConstraintViolationException(
message, sqlState, vendorErrorCode);
} else {
sqlEx = (SQLException) Util
.getInstance(
"com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException",
new Class[] { String.class, String.class,
Integer.TYPE }, new Object[] {
message, sqlState,
Constants.integerValueOf(vendorErrorCode) }, interceptor);
}
} else if (sqlState.startsWith("42")) {
if (!Util.isJdbc4()) {
sqlEx = new MySQLSyntaxErrorException(message, sqlState,
vendorErrorCode);
} else {
sqlEx = (SQLException) Util.getInstance(
"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException",
new Class[] { String.class, String.class,
Integer.TYPE }, new Object[] {
message, sqlState,
Constants.integerValueOf(vendorErrorCode) }, interceptor);
}
} else if (sqlState.startsWith("40")) {
if (!Util.isJdbc4()) {
sqlEx = new MySQLTransactionRollbackException(message,
sqlState, vendorErrorCode);
} else {
sqlEx = (SQLException) Util
.getInstance(
"com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException",
new Class[] { String.class, String.class,
Integer.TYPE }, new Object[] {
message, sqlState,
Constants.integerValueOf(vendorErrorCode) }, interceptor);
}
} else {
sqlEx = new SQLException(message, sqlState, vendorErrorCode);
}
} else {
sqlEx = new SQLException(message, sqlState, vendorErrorCode);
}
if (interceptor != null) {
SQLException interceptedEx = interceptor.interceptException(sqlEx, conn);
if (interceptedEx != null) {
return interceptedEx;
}
}
if (sqlEx == null) {
System.out.println("!");
}
return sqlEx;
} catch (SQLException sqlEx) {
SQLException unexpectedEx = new SQLException(
"Unable to create correct SQLException class instance, error class/codes may be incorrect. Reason: "
+ Util.stackTraceToString(sqlEx),
SQL_STATE_GENERAL_ERROR);
if (interceptor != null) {
SQLException interceptedEx = interceptor.interceptException(unexpectedEx, conn);
if (interceptedEx != null) {
return interceptedEx;
}
}
return unexpectedEx;
}
}
|
public static void dumpSqlStatesMappingsAsXml() throws Exception {
TreeMap allErrorNumbers = new TreeMap();
Map mysqlErrorNumbersToNames = new HashMap();
Integer errorNumber = null;
//
// First create a list of all 'known' error numbers that
// are mapped.
//
for (Iterator mysqlErrorNumbers = mysqlToSql99State.keySet().iterator(); mysqlErrorNumbers
.hasNext();) {
errorNumber = (Integer) mysqlErrorNumbers.next();
allErrorNumbers.put(errorNumber, errorNumber);
}
for (Iterator mysqlErrorNumbers = mysqlToSqlState.keySet().iterator(); mysqlErrorNumbers
.hasNext();) {
errorNumber = (Integer) mysqlErrorNumbers.next();
allErrorNumbers.put(errorNumber, errorNumber);
}
//
// Now create a list of the actual MySQL error numbers we know about
//
java.lang.reflect.Field[] possibleFields = MysqlErrorNumbers.class
.getDeclaredFields();
for (int i = 0; i < possibleFields.length; i++) {
String fieldName = possibleFields[i].getName();
if (fieldName.startsWith("ER_")) {
mysqlErrorNumbersToNames.put(possibleFields[i].get(null),
fieldName);
}
}
System.out.println("< ErrorMappings >");
for (Iterator allErrorNumbersIter = allErrorNumbers.keySet().iterator(); allErrorNumbersIter
.hasNext();) {
errorNumber = (Integer) allErrorNumbersIter.next();
String sql92State = mysqlToSql99(errorNumber.intValue());
String oldSqlState = mysqlToXOpen(errorNumber.intValue());
System.out.println(" < ErrorMapping mysqlErrorNumber=\""
+ errorNumber + "\" mysqlErrorName=\""
+ mysqlErrorNumbersToNames.get(errorNumber)
+ "\" legacySqlState=\""
+ ((oldSqlState == null) ? "" : oldSqlState)
+ "\" sql92SqlState=\""
+ ((sql92State == null) ? "" : sql92State) + "\"/ >");
}
System.out.println("< /ErrorMappings >");
}
|
static String get(String stateCode) {
return (String) sqlStateMessages.get(stateCode);
}
|
static String mysqlToSqlState(int errno,
boolean useSql92States) {
if (useSql92States) {
return mysqlToSql99(errno);
}
return mysqlToXOpen(errno);
}
Map MySQL error codes to X/Open or SQL-92 error codes |
public static SQLException notImplemented() {
if (Util.isJdbc4()) {
try {
return (SQLException) Class.forName(
"java.sql.SQLFeatureNotSupportedException")
.newInstance();
} catch (Throwable t) {
// proceed
}
}
return new NotImplemented();
}
|