KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > SQLExceptionFactory40


1 /*
2  
3    Derby - Class org.apache.derby.impl.jdbc.SQLExceptionFactory40
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import java.sql.SQLDataException JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.SQLIntegrityConstraintViolationException JavaDoc;
27 import java.sql.SQLInvalidAuthorizationSpecException JavaDoc;
28 import java.sql.SQLSyntaxErrorException JavaDoc;
29 import java.sql.SQLTransactionRollbackException JavaDoc;
30 import java.sql.SQLTransientConnectionException JavaDoc;
31 import java.sql.SQLFeatureNotSupportedException JavaDoc;
32 import org.apache.derby.iapi.error.StandardException;
33 import org.apache.derby.shared.common.reference.SQLState;
34
35 /**
36  * SQLExceptionFactory40 overwrites getSQLException method
37  * to return SQLException or one of its sub class
38  */

39
40 public class SQLExceptionFactory40 extends SQLExceptionFactory {
41     
42     /**
43      * overwrites super class method to create JDBC4 exceptions
44      * SQLSTATE CLASS (prefix) Exception
45      * 0A java.sql.SQLFeatureNotSupportedException
46      * 08 java.sql.SQLTransientConnectionException
47      * 22 java.sql.SQLDataException
48      * 28 java.sql.SQLInvalidAuthorizationSpecException
49      * 40 java.sql.SQLTransactionRollbackException
50      * 42 java.sql.SQLSyntaxErrorException
51      *
52      * Note the following divergence from JDBC3 behavior: When running
53      * a JDBC3 client, we return EmbedSQLException. That exception class
54      * overrides Throwable.toString() and strips off the Throwable's class name.
55      * In contrast, the following JDBC4 implementation returns
56      * subclasses of java.sql.Exception. These subclasses inherit the behavior
57      * of Throwable.toString(). That is, their toString() output includes
58      * their class name. This will break code which relies on the
59      * stripping behavior of EmbedSQLSxception.toString().
60      */

61     
62     public SQLException JavaDoc getSQLException(String JavaDoc message, String JavaDoc messageId,
63             SQLException JavaDoc next, int severity, Throwable JavaDoc t, Object JavaDoc[] args) {
64         String JavaDoc sqlState = StandardException.getSQLStateFromIdentifier(messageId);
65
66         //
67
// Create dummy exception which ferries arguments needed to serialize
68
// SQLExceptions across the DRDA network layer.
69
//
70
t = wrapArgsForTransportAcrossDRDA( message, messageId, next, severity, t, args );
71
72         final SQLException JavaDoc ex;
73         if (sqlState.startsWith(SQLState.CONNECTIVITY_PREFIX)) {
74             //none of the sqlstate supported by derby belongs to
75
//NonTransientConnectionException
76
ex = new SQLTransientConnectionException JavaDoc(message, sqlState,
77                     severity, t);
78         } else if (sqlState.startsWith(SQLState.SQL_DATA_PREFIX)) {
79             ex = new SQLDataException JavaDoc(message, sqlState, severity, t);
80         } else if (sqlState.startsWith(SQLState.INTEGRITY_VIOLATION_PREFIX)) {
81             ex = new SQLIntegrityConstraintViolationException JavaDoc(message, sqlState,
82                     severity, t);
83         } else if (sqlState.startsWith(SQLState.AUTHORIZATION_PREFIX)) {
84             ex = new SQLInvalidAuthorizationSpecException JavaDoc(message, sqlState,
85                     severity, t);
86         }
87         else if (sqlState.startsWith(SQLState.TRANSACTION_PREFIX)) {
88             ex = new SQLTransactionRollbackException JavaDoc(message, sqlState,
89                     severity, t);
90         } else if (sqlState.startsWith(SQLState.LSE_COMPILATION_PREFIX)) {
91             ex = new SQLSyntaxErrorException JavaDoc(message, sqlState, severity, t);
92         } else if (sqlState.startsWith(SQLState.UNSUPPORTED_PREFIX)) {
93             ex = new SQLFeatureNotSupportedException JavaDoc(message, sqlState, severity, t);
94         } else {
95             ex = new SQLException JavaDoc(message, sqlState, severity, t);
96         }
97         
98         if (next != null) {
99             ex.setNextException(next);
100         }
101         return ex;
102     }
103
104     /**
105      * Unpack the exception, looking for an EmbedSQLException which carries
106      * the Derby messageID and args which we will serialize across DRDA so
107      * that the client can reconstitute a SQLException with appropriate text.
108      * If we are running JDBC4, then the
109      * passed-in exception will hopefully wrap an informative EmbedSQLException.
110      * See wrapArgsForTransportAcrossDRDA() below.
111      */

112     public SQLException JavaDoc getArgumentFerry(SQLException JavaDoc se)
113     {
114         Throwable JavaDoc cause = se.getCause();
115
116         if ( (cause == null) || !(cause instanceof EmbedSQLException )) { return se; }
117         else { return (SQLException JavaDoc) cause; }
118     }
119
120     /**
121      * <p>
122      * The following method helps handle DERBY-1178. The problem is that we may
123      * need to serialize our final SQLException across the DRDA network layer.
124      * That serialization involves some clever encoding of the Derby messageID and
125      * arguments. Unfortunately, once we create one of the
126      * JDBC4-specific subclasses of SQLException, we lose the messageID and
127      * args. This method creates a dummy EmbedSQLException which preserves that
128      * information. We return the dummy exception.
129      * </p>
130      */

131     private SQLException JavaDoc wrapArgsForTransportAcrossDRDA
132     ( String JavaDoc message, String JavaDoc messageId, SQLException JavaDoc next, int severity, Throwable JavaDoc t, Object JavaDoc[] args )
133     {
134         return super.getSQLException( message, messageId, next, severity, t, args );
135     }
136     
137 }
138
Popular Tags