--- /dev/null
+/*\r
+ \r
+ Derby - Class org.apache.derby.impl.jdbc.SQLExceptionFactory40\r
+ \r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+ \r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+ \r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+ \r
+ */\r
+\r
+package org.apache.derby.impl.jdbc;\r
+\r
+import java.sql.SQLDataException;\r
+import java.sql.SQLException;\r
+import java.sql.SQLIntegrityConstraintViolationException;\r
+import java.sql.SQLInvalidAuthorizationSpecException;\r
+import java.sql.SQLNonTransientConnectionException;\r
+import java.sql.SQLSyntaxErrorException;\r
+import java.sql.SQLTransactionRollbackException;\r
+import java.sql.SQLFeatureNotSupportedException;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.shared.common.reference.SQLState;\r
+\r
+/**\r
+ * SQLExceptionFactory40 overwrites getSQLException method\r
+ * to return SQLException or one of its sub class\r
+ */\r
+\r
+public class SQLExceptionFactory40 extends SQLExceptionFactory {\r
+ \r
+ /**\r
+ * overwrites super class method to create JDBC4 exceptions \r
+ * SQLSTATE CLASS (prefix) Exception\r
+ * 0A java.sql.SQLFeatureNotSupportedException\r
+ * 08 java.sql.SQLNonTransientConnectionException\r
+ * 22 java.sql.SQLDataException\r
+ * 28 java.sql.SQLInvalidAuthorizationSpecException\r
+ * 40 java.sql.SQLTransactionRollbackException\r
+ * 42 java.sql.SQLSyntaxErrorException\r
+ * \r
+ * Note the following divergence from JDBC3 behavior: When running\r
+ * a JDBC3 client, we return EmbedSQLException. That exception class\r
+ * overrides Throwable.toString() and strips off the Throwable's class name.\r
+ * In contrast, the following JDBC4 implementation returns\r
+ * subclasses of java.sql.Exception. These subclasses inherit the behavior \r
+ * of Throwable.toString(). That is, their toString() output includes\r
+ * their class name. This will break code which relies on the\r
+ * stripping behavior of EmbedSQLSxception.toString(). \r
+ */\r
+ \r
+ public SQLException getSQLException(String message, String messageId,\r
+ SQLException next, int severity, Throwable t, Object[] args) {\r
+ String sqlState = StandardException.getSQLStateFromIdentifier(messageId);\r
+\r
+ //\r
+ // Create dummy exception which ferries arguments needed to serialize\r
+ // SQLExceptions across the DRDA network layer.\r
+ //\r
+ t = wrapArgsForTransportAcrossDRDA( message, messageId, next, severity, t, args );\r
+\r
+ final SQLException ex;\r
+ if (sqlState.startsWith(SQLState.CONNECTIVITY_PREFIX)) {\r
+ //none of the sqlstate supported by derby belongs to\r
+ //TransientConnectionException DERBY-3074\r
+ ex = new SQLNonTransientConnectionException(message, sqlState,\r
+ severity, t);\r
+ } else if (sqlState.startsWith(SQLState.SQL_DATA_PREFIX)) {\r
+ ex = new SQLDataException(message, sqlState, severity, t);\r
+ } else if (sqlState.startsWith(SQLState.INTEGRITY_VIOLATION_PREFIX)) {\r
+ ex = new SQLIntegrityConstraintViolationException(message, sqlState,\r
+ severity, t);\r
+ } else if (sqlState.startsWith(SQLState.AUTHORIZATION_SPEC_PREFIX)) {\r
+ ex = new SQLInvalidAuthorizationSpecException(message, sqlState,\r
+ severity, t);\r
+ } \r
+ else if (sqlState.startsWith(SQLState.TRANSACTION_PREFIX)) {\r
+ ex = new SQLTransactionRollbackException(message, sqlState,\r
+ severity, t);\r
+ } else if (sqlState.startsWith(SQLState.LSE_COMPILATION_PREFIX)) {\r
+ ex = new SQLSyntaxErrorException(message, sqlState, severity, t);\r
+ } else if (sqlState.startsWith(SQLState.UNSUPPORTED_PREFIX)) {\r
+ ex = new SQLFeatureNotSupportedException(message, sqlState, severity, t);\r
+ } else {\r
+ ex = new SQLException(message, sqlState, severity, t);\r
+ }\r
+ \r
+ if (next != null) {\r
+ ex.setNextException(next);\r
+ }\r
+ return ex;\r
+ } \r
+\r
+ /**\r
+ * Unpack the exception, looking for an EmbedSQLException which carries\r
+ * the Derby messageID and args which we will serialize across DRDA so\r
+ * that the client can reconstitute a SQLException with appropriate text.\r
+ * If we are running JDBC4, then the\r
+ * passed-in exception will hopefully wrap an informative EmbedSQLException.\r
+ * See wrapArgsForTransportAcrossDRDA() below.\r
+ */\r
+ public SQLException getArgumentFerry(SQLException se)\r
+ {\r
+ Throwable cause = se.getCause();\r
+\r
+ if ( (cause == null) || !(cause instanceof EmbedSQLException )) { return se; }\r
+ else { return (SQLException) cause; }\r
+ }\r
+\r
+ /**\r
+ * <p>\r
+ * The following method helps handle DERBY-1178. The problem is that we may\r
+ * need to serialize our final SQLException across the DRDA network layer.\r
+ * That serialization involves some clever encoding of the Derby messageID and\r
+ * arguments. Unfortunately, once we create one of the\r
+ * JDBC4-specific subclasses of SQLException, we lose the messageID and\r
+ * args. This method creates a dummy EmbedSQLException which preserves that\r
+ * information. We return the dummy exception.\r
+ * </p>\r
+ */\r
+ private SQLException wrapArgsForTransportAcrossDRDA\r
+ ( String message, String messageId, SQLException next, int severity, Throwable t, Object[] args )\r
+ {\r
+ // Generate an EmbedSQLException\r
+ SQLException e =\r
+ super.getSQLException(message, messageId,\r
+ (next == null ? null : getArgumentFerry(next)),\r
+ severity, t, args);\r
+ return e;\r
+ }\r
+ \r
+}\r