sql: properly propagate SQL exceptions stack traces add sql ânextâ exceptions as âsuppressedâ exceptions
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/75b2ce84 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/75b2ce84 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/75b2ce84 Branch: refs/heads/develop Commit: 75b2ce8470f419187bb2e7703a84e89d628ed65f Parents: 260eeb2 Author: Paul Merlin <[email protected]> Authored: Sun Dec 11 22:47:10 2016 +0100 Committer: Paul Merlin <[email protected]> Committed: Sun Dec 11 22:47:10 2016 +0100 ---------------------------------------------------------------------- .../entitystore/sql/SQLEntityStoreMixin.java | 23 +++++--------------- .../index/sql/internal/SQLEntityFinder.java | 2 +- .../sql/internal/SQLStateChangeListener.java | 16 ++------------ .../postgresql/PostgreSQLAppStartup.java | 2 +- .../postgresql/PostgreSQLIndexExporter.java | 6 ++--- .../support/skeletons/AbstractSQLIndexing.java | 18 ++++++++++----- .../support/skeletons/AbstractSQLStartup.java | 3 ++- .../sql/liquibase/LiquibaseServiceTest.java | 4 +++- .../apache/zest/library/sql/common/SQLUtil.java | 11 ++++++++++ .../AbstractDataSourceServiceImporterMixin.java | 4 +++- 10 files changed, 43 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java index fa66da2..d424ec4 100644 --- a/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java +++ b/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java @@ -202,20 +202,8 @@ public class SQLEntityStoreMixin catch( SQLException sqle ) { SQLUtil.rollbackQuietly( connection ); - if( LOGGER.isDebugEnabled() ) - { - StringWriter sb = new StringWriter(); - sb.append( - "SQLException during commit, logging nested exceptions before throwing EntityStoreException:\n" ); - SQLException e = sqle; - while( e != null ) - { - e.printStackTrace( new PrintWriter( sb, true ) ); - e = e.getNextException(); - } - LOGGER.debug( sb.toString() ); - } - throw new EntityStoreException( sqle ); + throw new EntityStoreException( "Unable to apply state changes", + SQLUtil.withAllSQLExceptions( sqle ) ); } catch( RuntimeException re ) { @@ -310,7 +298,8 @@ public class SQLEntityStoreMixin SQLUtil.closeQuietly( rs, ex ); SQLUtil.closeQuietly( ps, ex ); SQLUtil.closeQuietly( connection, ex ); - throw new EntityStoreException( ex ); + throw new EntityStoreException( "Unable to get next entity state", + SQLUtil.withAllSQLExceptions( ex ) ); } } }, @@ -326,7 +315,7 @@ public class SQLEntityStoreMixin } catch( SQLException ex ) { - throw new EntityStoreException( ex ); + throw new EntityStoreException( "Unable to get entity states", SQLUtil.withAllSQLExceptions( ex ) ); } } @@ -532,7 +521,7 @@ public class SQLEntityStoreMixin } catch( SQLException sqle ) { - throw new EntityStoreException( "Unable to get Entity " + ref, sqle ); + throw new EntityStoreException( "Unable to get Entity " + ref, SQLUtil.withAllSQLExceptions( sqle ) ); } finally { http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java ---------------------------------------------------------------------- diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java index db2dd3a..b965109 100644 --- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java +++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLEntityFinder.java @@ -243,7 +243,7 @@ public class SQLEntityFinder } catch( SQLException sqle ) { - throw new EntityFinderException( sqle ); + throw new EntityFinderException( SQLUtil.withAllSQLExceptions( sqle ) ); } finally { http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLStateChangeListener.java ---------------------------------------------------------------------- diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLStateChangeListener.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLStateChangeListener.java index 2c51f84..1af26f4 100644 --- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLStateChangeListener.java +++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/internal/SQLStateChangeListener.java @@ -23,17 +23,13 @@ import java.sql.SQLException; import org.apache.zest.api.injection.scope.Service; import org.apache.zest.api.unitofwork.UnitOfWorkException; import org.apache.zest.index.sql.support.api.SQLIndexing; +import org.apache.zest.library.sql.common.SQLUtil; import org.apache.zest.spi.entity.EntityState; import org.apache.zest.spi.entitystore.StateChangeListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class SQLStateChangeListener implements StateChangeListener { - - private static final Logger LOGGER = LoggerFactory.getLogger( SQLStateChangeListener.class ); - @Service private SQLIndexing _indexing; @@ -46,16 +42,8 @@ public class SQLStateChangeListener } catch( SQLException sqle ) { - SQLException lastException = sqle; - while( sqle.getNextException() != null ) - { - sqle = sqle.getNextException(); - } - LOGGER.error( "Error when indexing entities", sqle ); - // TODO is UoWException right one for this? - throw new UnitOfWorkException( lastException ); + throw new UnitOfWorkException( SQLUtil.withAllSQLExceptions( sqle ) ); } } - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLAppStartup.java ---------------------------------------------------------------------- diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLAppStartup.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLAppStartup.java index 34dcef7..5b83cb9 100644 --- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLAppStartup.java +++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLAppStartup.java @@ -92,7 +92,7 @@ public class PostgreSQLAppStartup throw new InternalError( "It seems that your database doesn't have ltree as type. It is needed to store " + "collections. Please refer to hopefully supplied instructions on how to add " + "ltree type (hint: run <pg_install_dir>/share/contrib/ltree.sql script or " - + "command 'CREATE EXTENSION ltree;')." ); + + "command 'CREATE EXTENSION ltree;').", SQLUtil.withAllSQLExceptions( sqle ) ); } finally { http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLIndexExporter.java ---------------------------------------------------------------------- diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLIndexExporter.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLIndexExporter.java index e05f05d..fbbd0ff 100644 --- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLIndexExporter.java +++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/postgresql/PostgreSQLIndexExporter.java @@ -194,8 +194,7 @@ public class PostgreSQLIndexExporter } catch( SQLException sqle ) { - // TODO Just wrap around for now... - throw new IOException( sqle ); + throw new IOException( SQLUtil.withAllSQLExceptions( sqle ) ); } finally { @@ -332,8 +331,7 @@ public class PostgreSQLIndexExporter } catch( SQLException sqle ) { - // TODO Just wrap around for now... - throw new IOException( sqle ); + throw new IOException( SQLUtil.withAllSQLExceptions( sqle ) ); } finally { http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLIndexing.java ---------------------------------------------------------------------- diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLIndexing.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLIndexing.java index e42d2c5..019bd7a 100644 --- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLIndexing.java +++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLIndexing.java @@ -262,7 +262,8 @@ public abstract class AbstractSQLIndexing } catch( SQLException sqle ) { - SQLUtil.rollbackQuietly( connection ); + sqle = SQLUtil.withAllSQLExceptions( sqle ); + SQLUtil.rollbackQuietly( connection, sqle ); throw sqle; } finally @@ -616,7 +617,8 @@ public abstract class AbstractSQLIndexing } catch( SQLException e ) { - throw new EntityStoreException( "Underlying exception when inserting property " + pDesc ); + throw new EntityStoreException( "Underlying exception when inserting property " + pDesc, + SQLUtil.withAllSQLExceptions( e ) ); } } ); return propertyPK[0]; @@ -655,7 +657,8 @@ public abstract class AbstractSQLIndexing } catch( SQLException e ) { - throw new EntityStoreException( "Underlying exception when inserting association " + aDesc ); + throw new EntityStoreException( "Underlying exception when inserting association " + aDesc, + SQLUtil.withAllSQLExceptions( e ) ); } } ); @@ -687,7 +690,8 @@ public abstract class AbstractSQLIndexing } catch( SQLException e ) { - throw new EntityStoreException( "Underlying exception when inserting manyassociation " + mDesc ); + throw new EntityStoreException( "Underlying exception when inserting manyassociation " + mDesc, + SQLUtil.withAllSQLExceptions( e ) ); } } ); } @@ -938,7 +942,8 @@ public abstract class AbstractSQLIndexing } catch( SQLException e ) { - throw new EntityStoreException( "Underlying exception when inserting property " + pDesc + " in value " + vDesc, e ); + throw new EntityStoreException( "Underlying exception when inserting property " + pDesc + " in value " + vDesc, + SQLUtil.withAllSQLExceptions( e ) ); } } ); @@ -1054,7 +1059,8 @@ public abstract class AbstractSQLIndexing } catch( SQLException e ) { - throw new EntityStoreException( "Underlying Exception when inserting " + entityPK, e ); + throw new EntityStoreException( "Underlying Exception when inserting " + entityPK, + SQLUtil.withAllSQLExceptions( e ) ); } } ); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLStartup.java ---------------------------------------------------------------------- diff --git a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLStartup.java b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLStartup.java index 1677027..6faaf07 100644 --- a/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLStartup.java +++ b/extensions/indexing-sql/src/main/java/org/apache/zest/index/sql/support/skeletons/AbstractSQLStartup.java @@ -928,7 +928,8 @@ public abstract class AbstractSQLStartup } catch( SQLException e ) { - throw new EntityStoreException( "Underlying exception when setting " + pk, e ); + throw new EntityStoreException( "Underlying exception when setting " + pk, + SQLUtil.withAllSQLExceptions( e ) ); } this._state.entityTypePKs().get().put( entityTypeName, (int) pk ); // this._state.entityTypeInfos().get().put( entityTypeName, new EntityTypeInfo( descriptor, (int) pk ) ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/libraries/sql-liquibase/src/test/java/org/apache/zest/library/sql/liquibase/LiquibaseServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/sql-liquibase/src/test/java/org/apache/zest/library/sql/liquibase/LiquibaseServiceTest.java b/libraries/sql-liquibase/src/test/java/org/apache/zest/library/sql/liquibase/LiquibaseServiceTest.java index 83ef261..5e4fd3d 100644 --- a/libraries/sql-liquibase/src/test/java/org/apache/zest/library/sql/liquibase/LiquibaseServiceTest.java +++ b/libraries/sql-liquibase/src/test/java/org/apache/zest/library/sql/liquibase/LiquibaseServiceTest.java @@ -40,6 +40,7 @@ import org.apache.zest.bootstrap.ModuleAssembly; import org.apache.zest.bootstrap.SingletonAssembler; import org.apache.zest.library.sql.assembly.DataSourceAssembler; import org.apache.zest.library.sql.common.Databases; +import org.apache.zest.library.sql.common.SQLUtil; import org.apache.zest.library.sql.dbcp.DBCPDataSourceServiceAssembler; import org.apache.zest.test.EntityTestAssembler; import org.junit.Test; @@ -145,7 +146,8 @@ public class LiquibaseServiceTest } catch( SQLException e ) { - throw new IllegalArgumentException( "Could not convert to SomeValue", e ); + throw new IllegalArgumentException( "Could not convert to SomeValue", + SQLUtil.withAllSQLExceptions( e ) ); } return builder.newInstance(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java ---------------------------------------------------------------------- diff --git a/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java b/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java index e87c9c6..577e62b 100644 --- a/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java +++ b/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java @@ -26,6 +26,17 @@ import java.sql.Statement; public class SQLUtil { + public static SQLException withAllSQLExceptions( SQLException sqlEx ) + { + SQLException next = sqlEx.getNextException(); + while( next != null ) + { + sqlEx.addSuppressed( next ); + next = next.getNextException(); + } + return sqlEx; + } + public static void closeQuietly( ResultSet resultSet ) { closeQuietly( resultSet, null ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/75b2ce84/libraries/sql/src/main/java/org/apache/zest/library/sql/datasource/AbstractDataSourceServiceImporterMixin.java ---------------------------------------------------------------------- diff --git a/libraries/sql/src/main/java/org/apache/zest/library/sql/datasource/AbstractDataSourceServiceImporterMixin.java b/libraries/sql/src/main/java/org/apache/zest/library/sql/datasource/AbstractDataSourceServiceImporterMixin.java index 41838e1..70c02c0 100644 --- a/libraries/sql/src/main/java/org/apache/zest/library/sql/datasource/AbstractDataSourceServiceImporterMixin.java +++ b/libraries/sql/src/main/java/org/apache/zest/library/sql/datasource/AbstractDataSourceServiceImporterMixin.java @@ -39,6 +39,7 @@ import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; import org.apache.zest.api.unitofwork.UnitOfWorkFactory; import org.apache.zest.api.usecase.UsecaseBuilder; import org.apache.zest.library.circuitbreaker.CircuitBreaker; +import org.apache.zest.library.sql.common.SQLUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -102,7 +103,8 @@ public abstract class AbstractDataSourceServiceImporterMixin<PooledDataSourceTyp LOGGER.info( "Database for DataSource is up!" ); } catch ( SQLException e ) { LOGGER.warn( "Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available" ); - throw new ServiceImporterException( "Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available", e ); + throw new ServiceImporterException( "Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available", + SQLUtil.withAllSQLExceptions( e ) ); } finally { Thread.currentThread().setContextClassLoader( cl ); }
