db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Armin Waibel <arm...@apache.org>
Subject Re: [OJB] Issue #OJB302 - Patch for plattform dependent mapping to specific excepti...
Date Sun, 12 Dec 2004 01:52:55 GMT
Hi Oliver,

thanks for the patch! I will check in ASAP.

regards,
Armin

Oliver Zeigermann wrote:
> Hmmm, somehow my patch has vanished as it seems, hope it is OK to
> attach it to this post.
> 
> I have not reasonably tested this patch, but rather see it as a sketch
> to discuss if this all makes any sense.
> 
> Would be great if anyone could have a look at this!
> 
> Thanks in advance,
> 
> Oliver
> 
> On Sat, 11 Dec 2004 11:17:20 -0800 (PST), Oliver Zeigermann
> <ozeigermann@apache.org> wrote:
> 
>>You can view the issue detail at the following URL:
>>
>>    http://nagoya.apache.org/scarab/issues/id/OJB302
>>
>>Type
>> Patch
>>
>>Issue ID
>> OJB302 (Patch for plattform dependent mapping to specific exceptions)
>>
>>Reported by
>> Oliver Zeigermann
>> ozeigermann (ozeigermann@apache.org)
>>
>>New issue details:
>>---------------------------------------------------------
>>
>>Summary set to "Patch for plattform dependent ..."
>>Description set to "SQL exceptions thrown while ex..."
>>Operating system set to "All"
>>Issue created
>>Platform set to "All"
>>
>>---------------------------------------------------------
>>This message is automatically generated by the Scarab
>>issue tracking system.  For more information:
>>http://scarab.tigris.org/
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-dev-help@db.apache.org
>>
>>
>>
>>
>>------------------------------------------------------------------------
>>
>>Index: KeyConstraintViolatedException.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/KeyConstraintViolatedException.java,v
>>retrieving revision 1.6
>>diff -u -r1.6 KeyConstraintViolatedException.java
>>--- KeyConstraintViolatedException.java	28 Nov 2004 03:16:38 -0000	1.6
>>+++ KeyConstraintViolatedException.java	11 Dec 2004 18:59:42 -0000
>>@@ -18,7 +18,7 @@
>>  */
>> 
>> /**
>>- * Exception indicate an SQL key contraint violation.
>>+ * This exception indicates a general SQL constraint violation.
>>  *
>>  * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird<a>
>>  * @version $Id: KeyConstraintViolatedException.java,v 1.6 2004/11/28 03:16:38 arminw
Exp $
>>Index: accesslayer/JdbcAccessImpl.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java,v
>>retrieving revision 1.28
>>diff -u -r1.28 JdbcAccessImpl.java
>>--- accesslayer/JdbcAccessImpl.java	28 Nov 2004 03:12:51 -0000	1.28
>>+++ accesslayer/JdbcAccessImpl.java	11 Dec 2004 18:59:44 -0000
>>@@ -25,7 +25,6 @@
>> import java.util.WeakHashMap;
>> 
>> import org.apache.ojb.broker.Identity;
>>-import org.apache.ojb.broker.KeyConstraintViolatedException;
>> import org.apache.ojb.broker.OptimisticLockException;
>> import org.apache.ojb.broker.PersistenceBrokerException;
>> import org.apache.ojb.broker.PersistenceBrokerInternal;
>>@@ -144,7 +143,7 @@
>>                 + cld.getClassOfObject().getName()
>>                 + "): " + e.getMessage();
>>             logger.error(msg, e);
>>-            throw new PersistenceBrokerSQLException("JdbcAccessImpl: " + msg, e);
>>+            throw getPlatform().createException("JdbcAccessImpl: " + msg, e);
>>         }
>>         finally
>>         {
>>@@ -180,7 +179,7 @@
>>                 + cld.getClassOfObject().getName()
>>                 + "): " + e.getMessage();
>>             logger.error(msg,e);
>>-            throw new PersistenceBrokerSQLException(msg, e);
>>+            throw getPlatform().createException(msg, e);
>>         }
>>         finally
>>         {
>>@@ -251,18 +250,7 @@
>>             {
>>                 msg.append("- Sorry, can't generate a more detailed message");
>>             }
>>-
>>-            /**
>>-             * throw a specific type of runtime exception for a key constraint.
>>-             */
>>-            if (SQL_STATE_KEY_VIOLATED.equals(e.getSQLState()))
>>-            {
>>-                throw new KeyConstraintViolatedException(msg.toString(), e);
>>-            }
>>-            else
>>-            {
>>-                throw new PersistenceBrokerSQLException(msg.toString(), e);
>>-            }
>>+            throw getPlatform().createException(msg.toString(), e);
>>         }
>>         finally
>>         {
>>@@ -337,7 +325,7 @@
>>             {
>>                 retval.close();
>>             }
>>-            throw new PersistenceBrokerSQLException(msg, e);
>>+            throw getPlatform().createException(msg, e);
>>         }
>>     }
>> 
>>@@ -399,7 +387,7 @@
>>             {
>>                 retval.close();
>>             }
>>-            throw new PersistenceBrokerSQLException(msg, e);
>>+            throw getPlatform().createException(msg, e);
>>         }
>>     }
>> 
>>@@ -440,14 +428,7 @@
>>         {
>>             String msg = "SQLException during the execution of the update SQL query:
" + sqlStatement;
>>             logger.error(msg, e);
>>-            if (SQL_STATE_KEY_VIOLATED.equals(e.getSQLState()))
>>-            {
>>-                throw new KeyConstraintViolatedException(msg, e);
>>-            }
>>-            else
>>-            {
>>-                throw new PersistenceBrokerSQLException(msg, e);
>>-            }
>>+            throw getPlatform().createException(msg.toString(), e);
>>         }
>>         finally
>>         {
>>@@ -541,11 +522,7 @@
>>                 + "): "
>>                 + e.getMessage();
>>             logger.error(msg,e);
>>-            if (SQL_STATE_KEY_VIOLATED.equals(e.getSQLState()))
>>-            {
>>-                throw new KeyConstraintViolatedException(msg, e);
>>-            }
>>-            throw new PersistenceBrokerSQLException(msg, e);
>>+            throw getPlatform().createException(msg.toString(), e);
>>         }
>>         finally
>>         {           
>>@@ -580,8 +557,8 @@
>>         catch(SQLException e)
>>         {
>>             logger.error("Can't check database for object existance", e);
>>-            throw new PersistenceBrokerSQLException("Exist check failed for identity
" + oid
>>-                    + ", sql was " + sql, e);
>>+            throw getPlatform().createException(
>>+                    "Exist check failed for identity " + oid + ", sql was " + sql,
e);
>>         }
>>         finally
>>         {
>>@@ -645,7 +622,7 @@
>>                 + "): "
>>                 + e.getMessage();
>>             logger.error(msg,e);
>>-            throw new PersistenceBrokerSQLException(msg, e);
>>+            throw getPlatform().createException(msg, e);
>>         }
>>         finally
>>         {
>>@@ -773,7 +750,7 @@
>>                 + " : "
>>                 + e.getMessage();
>>             logger.error(msg,e);
>>-            throw new PersistenceBrokerSQLException(msg, e);
>>+            throw getPlatform().createException(msg, e);
>>         }
>>     }
>> }
>>Index: accesslayer/batch/BatchEntryImpl.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/accesslayer/batch/BatchEntryImpl.java,v
>>retrieving revision 1.1
>>diff -u -r1.1 BatchEntryImpl.java
>>--- accesslayer/batch/BatchEntryImpl.java	14 Sep 2004 16:03:31 -0000	1.1
>>+++ accesslayer/batch/BatchEntryImpl.java	11 Dec 2004 18:59:45 -0000
>>@@ -8,12 +8,11 @@
>> 
>> import org.apache.commons.lang.SystemUtils;
>> import org.apache.commons.lang.builder.ToStringBuilder;
>>-import org.apache.ojb.broker.KeyConstraintViolatedException;
>> import org.apache.ojb.broker.OJBRuntimeException;
>> import org.apache.ojb.broker.OptimisticLockException;
>> import org.apache.ojb.broker.PersistenceBrokerException;
>>-import org.apache.ojb.broker.PersistenceBrokerSQLException;
>> import org.apache.ojb.broker.metadata.FieldDescriptor;
>>+import org.apache.ojb.broker.platforms.Platform;
>> import org.apache.ojb.broker.query.Query;
>> import org.apache.ojb.broker.util.ClassHelper;
>> 
>>@@ -94,17 +93,7 @@
>>             catch(SQLException e)
>>             {
>>                 String msg = buildDetailedMessage(value, e);
>>-                /**
>>-                 * throw a specific type of runtime exception for a key constraint.
>>-                 */
>>-                if(SQL_STATE_KEY_VIOLATED.equals(e.getSQLState()))
>>-                {
>>-                    throw new KeyConstraintViolatedException(msg, e);
>>-                }
>>-                else
>>-                {
>>-                    throw new PersistenceBrokerSQLException(msg, e);
>>-                }
>>+                throw getPlatform().createException(msg, e);
>>             }
>>             finally
>>             {
>>@@ -285,4 +274,13 @@
>>         }
>>         return msg.toString();
>>     }
>>+
>>+    /**
>>+     * Answer the Platform.
>>+     */
>>+    private Platform getPlatform() {
>>+        return batchManager.getPersistenceBroker().serviceConnectionManager()
>>+                .getSupportedPlatform();
>>+    }
>>+
>> }
>>Index: accesslayer/batch/BatchManagerImpl.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/accesslayer/batch/BatchManagerImpl.java,v
>>retrieving revision 1.3
>>diff -u -r1.3 BatchManagerImpl.java
>>--- accesslayer/batch/BatchManagerImpl.java	20 Sep 2004 14:33:14 -0000	1.3
>>+++ accesslayer/batch/BatchManagerImpl.java	11 Dec 2004 18:59:46 -0000
>>@@ -518,4 +518,11 @@
>>             .append("JdbcAccess", jdbcAccess);
>>         return buf.toString();
>>     }
>>+
>>+    /**
>>+     * Gets the persistence broker associated to this manager.
>>+     */
>>+    protected PersistenceBroker getPersistenceBroker() {
>>+        return broker;
>>+    }
>> }
>>Index: platforms/Platform.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java,v
>>retrieving revision 1.25
>>diff -u -r1.25 Platform.java
>>--- platforms/Platform.java	22 Nov 2004 20:55:24 -0000	1.25
>>+++ platforms/Platform.java	11 Dec 2004 18:59:47 -0000
>>@@ -15,6 +15,7 @@
>>  * limitations under the License.
>>  */
>> 
>>+import org.apache.ojb.broker.PersistenceBrokerSQLException;
>> import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
>> import org.apache.ojb.broker.query.LikeCriteria;
>> 
>>@@ -233,4 +234,17 @@
>>      */
>>     public String quoteName(String aString);
>> 
>>+    /**
>>+     * Analysis the given SQL exception and makes it a {@link PersistenceBrokerSQLException}.
>>+     * Implementations should generate the appropriate subclass of 
>>+     * {@link PersistenceBrokerSQLException} that indicates the type of the exception.
>>+     * Constraint violations or detected deadlocks are exceptions the user might
want to react
>>+     * in a special way.
>>+     * 
>>+     * @param message a detailed message string
>>+     * @param e the SQL exception to map to a PersistenceBrokerSQLException
>>+     * @return the mapped PersistenceBrokerSQLException
>>+     */
>>+    public PersistenceBrokerSQLException createException(String message, SQLException
e);
>>+    
>> }
>>Index: platforms/PlatformDefaultImpl.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java,v
>>retrieving revision 1.30
>>diff -u -r1.30 PlatformDefaultImpl.java
>>--- platforms/PlatformDefaultImpl.java	25 Nov 2004 17:31:34 -0000	1.30
>>+++ platforms/PlatformDefaultImpl.java	11 Dec 2004 18:59:48 -0000
>>@@ -25,7 +25,10 @@
>> import java.sql.Statement;
>> import java.sql.Types;
>> 
>>+import org.apache.ojb.broker.KeyConstraintViolatedException;
>> import org.apache.ojb.broker.PersistenceBrokerException;
>>+import org.apache.ojb.broker.PersistenceBrokerSQLException;
>>+import org.apache.ojb.broker.TransactionRollbackException;
>> import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes;
>> import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
>> import org.apache.ojb.broker.query.LikeCriteria;
>>@@ -43,6 +46,10 @@
>>  */
>> public class PlatformDefaultImpl implements Platform, JoinSyntaxTypes
>> {
>>+
>>+    public static final String SQL_STATE_INTEGRITY_CONSTRAINT_VIOLATION_CLASS = "23";
>>+    public static final String SQL_STATE_TRANSACTION_ROLLBACK_CLASS = "40";
>>+    
>>     private static final char OJB_QUOTE_BEGIN_CHAR = '\'';
>>     private static final char OJB_QUOTE_END_CHAR = '\'';
>>     
>>@@ -424,6 +431,32 @@
>>         return result; 
>>     }
>> 
>>+    /**
>>+     * Uses SQLSTATE to genericly find out if this is either a constraint violation
>>+     * or a transaction rollback and throw the appropriate exception. 
>>+     * If it is not, it simply wraps the SQL exception into a PB exception.
>>+     * 
>>+     * @see org.apache.ojb.broker.platforms.Platform#createException(SQLException)
>>+     */
>>+    public PersistenceBrokerSQLException createException(String message, SQLException
e) {
>>+        String sqlstate = e.getSQLState();
>>+
>>+        if (sqlstate != null)
>>+        {
>>+            if (sqlstate.startsWith(SQL_STATE_INTEGRITY_CONSTRAINT_VIOLATION_CLASS))
>>+            {
>>+                throw new KeyConstraintViolatedException(message, e);
>>+            } 
>>+            else if (sqlstate.startsWith(SQL_STATE_TRANSACTION_ROLLBACK_CLASS)) 
>>+            {
>>+                throw new TransactionRollbackException(message, e);
>>+            }
>>+        }
>>+        throw new PersistenceBrokerSQLException(message, e);
>>+    }
>>+    
>>+
>>+    
>>     /**
>>      * @return a quoted name. ie "name" or [name]
>>      */
>>Index: platforms/PlatformMsSQLServerImpl.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformMsSQLServerImpl.java,v
>>retrieving revision 1.10
>>diff -u -r1.10 PlatformMsSQLServerImpl.java
>>--- platforms/PlatformMsSQLServerImpl.java	25 Nov 2004 17:31:34 -0000	1.10
>>+++ platforms/PlatformMsSQLServerImpl.java	11 Dec 2004 18:59:48 -0000
>>@@ -5,6 +5,10 @@
>> import java.sql.Types;
>> import java.sql.SQLException;
>> 
>>+import org.apache.ojb.broker.KeyConstraintViolatedException;
>>+import org.apache.ojb.broker.PersistenceBrokerSQLException;
>>+import org.apache.ojb.broker.TransactionRollbackException;
>>+
>> /* Copyright 2002-2004 The Apache Software Foundation
>>  *
>>  * Licensed under the Apache License, Version 2.0 (the "License");
>>@@ -65,6 +69,29 @@
>>         // this call must be made before commit the insert cammand, so you
>>         // must turn off autocommit by setting the useAutoCommit="2"
>>         return "SELECT @@IDENTITY AS id FROM " + tableName;
>>+    }
>>+
>>+    /**
>>+     * Uses error codes to find out if this is either a constraint violation
>>+     * or a transaction rollback and throw the appropriate exception. 
>>+     * If it is not, it returns the exception of the default implementation.
>>+     * 
>>+     * @see org.apache.ojb.broker.platforms.Platform#createException(SQLException)
>>+     */
>>+    public PersistenceBrokerSQLException createException(String message, SQLException
e) {
>>+        switch (e.getErrorCode())
>>+        {
>>+        case 1205: case 1211: case 2625: case 3309: case 7112: 
>>+            throw new TransactionRollbackException(message, e);
>>+
>>+        case 233: case 272: case 273: case 515: 
>>+        case 530: case 547: case 1505: case 1508: 
>>+        case 2601: case 2615: case 2626: case 2627:
>>+        case 3604: case 3605: 
>>+            throw new KeyConstraintViolatedException(message, e);
>>+        default:
>>+            return super.createException(message, e);
>>+        }
>>     }
>> 
>>     /**
>>Index: platforms/PlatformMySQLImpl.java
>>===================================================================
>>RCS file: /home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java,v
>>retrieving revision 1.17
>>diff -u -r1.17 PlatformMySQLImpl.java
>>--- platforms/PlatformMySQLImpl.java	25 Nov 2004 17:31:34 -0000	1.17
>>+++ platforms/PlatformMySQLImpl.java	11 Dec 2004 18:59:49 -0000
>>@@ -23,6 +23,8 @@
>> import java.sql.SQLException;
>> import java.sql.Types;
>> 
>>+import org.apache.ojb.broker.PersistenceBrokerSQLException;
>>+import org.apache.ojb.broker.TransactionRollbackException;
>> import org.apache.ojb.broker.query.LikeCriteria;
>> 
>> /**
>>@@ -100,6 +102,23 @@
>>     public String getLastInsertIdentityQuery(String tableName)
>>     {
>>         return LAST_INSERT + tableName + LIMIT;
>>+    }
>>+
>>+    /**
>>+     * Uses error codes to find out if this is a transaction rollback 
>>+     * and throw the appropriate exception. 
>>+     * If it is not, it returns the exception of the default implementation.
>>+     * 
>>+     * @see org.apache.ojb.broker.platforms.Platform#createException(SQLException)
>>+     */
>>+    public PersistenceBrokerSQLException createException(String message, SQLException
e) {
>>+        switch (e.getErrorCode())
>>+        {
>>+        case 1213: 
>>+            throw new TransactionRollbackException(message, e);
>>+        default:
>>+            return super.createException(message, e);
>>+        }
>>     }
>> 
>>     /*
>>Index: TransactionRollbackException.java
>>===================================================================
>>RCS file: TransactionRollbackException.java
>>diff -N TransactionRollbackException.java
>>--- /dev/null	1 Jan 1970 00:00:00 -0000
>>+++ TransactionRollbackException.java	1 Jan 1970 00:00:00 -0000
>>@@ -0,0 +1,54 @@
>>+package org.apache.ojb.broker;
>>+
>>+import java.sql.SQLException;
>>+
>>+/* Copyright 2002-2004 The Apache Software Foundation
>>+ *
>>+ * Licensed under the Apache License, Version 2.0 (the "License");
>>+ * you may not use this file except in compliance with the License.
>>+ * You may obtain a copy of the License at
>>+ *
>>+ *     http://www.apache.org/licenses/LICENSE-2.0
>>+ *
>>+ * Unless required by applicable law or agreed to in writing, software
>>+ * distributed under the License is distributed on an "AS IS" BASIS,
>>+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>+ * See the License for the specific language governing permissions and
>>+ * limitations under the License.
>>+ */
>>+
>>+/**
>>+ * This exception indicates that the current transaction can not be committed and
must be rolled back. 
>>+ * It thus indicates the failure of the transaction.
>>+ * <br><br>
>>+ * The reason can be a general timeout or that the transaction 
>>+ * <ol>
>>+ * <li>can not be serialized if the DB, e.g. Oracle, uses an optimistic locking
scheme, or
>>+ * <li>is the vicitim of a deadlock resolvation if the DB, e.g. SQLServer,
uses a pessimistic locking scheme
>>+ * </ol>
>>+ * 
>>+ * @author <a href="mailto:ozeigermann@apache.org">Oliver Zeigermann<a>
>>+ * @version $Id: KeyConstraintViolatedException.java,v 1.6 2004/11/28 03:16:38 arminw
Exp $
>>+ */
>>+
>>+public class TransactionRollbackException extends PersistenceBrokerSQLException
>>+{
>>+    public TransactionRollbackException()
>>+    {
>>+    }
>>+
>>+    public TransactionRollbackException(SQLException t)
>>+    {
>>+        super(t);
>>+    }
>>+
>>+    public TransactionRollbackException(String message)
>>+    {
>>+        super(message);
>>+    }
>>+
>>+    public TransactionRollbackException(String message, SQLException t)
>>+    {
>>+        super(message, t);
>>+    }
>>+}
>>
>>
>>
>>------------------------------------------------------------------------
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-dev-help@db.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message