Changed Depot to throw a RuntimeException on unexpected database error instead

of a checked exception. Invariably, we don't do anything with our
PersistenceExceptions except let them percolate all the way to the top and then
log a warning. We can probably automate the process of logging a warning with
useful information and save ourselves the trouble of doing it manually
everywhere.
This commit is contained in:
Michael Bayne
2008-09-03 16:10:06 +00:00
parent 2fd131c0d2
commit 67bdfda3fe
9 changed files with 152 additions and 67 deletions
@@ -0,0 +1,53 @@
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2008 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.jdbc.depot;
/**
* Represents a failure reported by the underlying database.
*/
public class DatabaseException extends RuntimeException
{
/**
* Constructs a database exception with the specified error message.
*/
public DatabaseException (String message)
{
super(message);
}
/**
* Constructs a database exception with the specified error message and the chained causing
* event.
*/
public DatabaseException (String message, Throwable cause)
{
super(message);
initCause(cause);
}
/**
* Constructs a database exception with the specified chained causing event.
*/
public DatabaseException (Throwable cause)
{
initCause(cause);
}
}
@@ -36,7 +36,6 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.depot.annotation.Computed; import com.samskivert.jdbc.depot.annotation.Computed;
import com.samskivert.jdbc.depot.annotation.Entity; import com.samskivert.jdbc.depot.annotation.Entity;
import com.samskivert.jdbc.depot.annotation.FullTextIndex; import com.samskivert.jdbc.depot.annotation.FullTextIndex;
@@ -525,7 +524,7 @@ public class DepotMarshaller<T extends PersistentRecord>
* database schema, it will be migrated. * database schema, it will be migrated.
*/ */
protected void init (PersistenceContext ctx) protected void init (PersistenceContext ctx)
throws PersistenceException throws DatabaseException
{ {
if (_initialized) { // sanity check if (_initialized) { // sanity check
throw new IllegalStateException( throw new IllegalStateException(
@@ -632,7 +631,7 @@ public class DepotMarshaller<T extends PersistentRecord>
log.info("Waiting for migration lock for " + _pClass.getName() + "."); log.info("Waiting for migration lock for " + _pClass.getName() + ".");
Thread.sleep(5000); Thread.sleep(5000);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
throw new PersistenceException("Interrupted while waiting for migration lock."); throw new DatabaseException("Interrupted while waiting for migration lock.");
} }
} }
@@ -672,7 +671,7 @@ public class DepotMarshaller<T extends PersistentRecord>
protected void createTable (PersistenceContext ctx, final SQLBuilder builder, protected void createTable (PersistenceContext ctx, final SQLBuilder builder,
final ColumnDefinition[] declarations) final ColumnDefinition[] declarations)
throws PersistenceException throws DatabaseException
{ {
log.info("Creating initial table '" + getTableName() + "'."); log.info("Creating initial table '" + getTableName() + "'.");
@@ -721,7 +720,7 @@ public class DepotMarshaller<T extends PersistentRecord>
protected void runMigrations (PersistenceContext ctx, TableMetaData metaData, protected void runMigrations (PersistenceContext ctx, TableMetaData metaData,
final SQLBuilder builder, int currentVersion) final SQLBuilder builder, int currentVersion)
throws PersistenceException throws DatabaseException
{ {
log.info("Migrating " + getTableName() + " from " + currentVersion + " to " + log.info("Migrating " + getTableName() + " from " + currentVersion + " to " +
_schemaVersion + "..."); _schemaVersion + "...");
@@ -928,7 +927,7 @@ public class DepotMarshaller<T extends PersistentRecord>
* Checks that there are no database columns for which we no longer have Java fields. * Checks that there are no database columns for which we no longer have Java fields.
*/ */
protected void checkForStaleness (TableMetaData meta, PersistenceContext ctx, SQLBuilder builder) protected void checkForStaleness (TableMetaData meta, PersistenceContext ctx, SQLBuilder builder)
throws PersistenceException throws DatabaseException
{ {
for (String fname : _columnFields) { for (String fname : _columnFields) {
FieldMarshaller<?> fmarsh = _fields.get(fname); FieldMarshaller<?> fmarsh = _fields.get(fname);
@@ -992,7 +991,7 @@ public class DepotMarshaller<T extends PersistentRecord>
public Set<String> pkColumns = new HashSet<String>(); public Set<String> pkColumns = new HashSet<String>();
public static TableMetaData load (PersistenceContext ctx, final String tableName) public static TableMetaData load (PersistenceContext ctx, final String tableName)
throws PersistenceException throws DatabaseException
{ {
return ctx.invoke(new Query.TrivialQuery<TableMetaData>() { return ctx.invoke(new Query.TrivialQuery<TableMetaData>() {
@Override public TableMetaData invoke (Connection conn, DatabaseLiaison dl) @Override public TableMetaData invoke (Connection conn, DatabaseLiaison dl)
@@ -30,7 +30,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.samskivert.io.PersistenceException;
import com.samskivert.util.ArrayUtil; import com.samskivert.util.ArrayUtil;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
@@ -81,7 +80,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> T load (Class<T> type, Comparable<?> primaryKey, protected <T extends PersistentRecord> T load (Class<T> type, Comparable<?> primaryKey,
QueryClause... clauses) QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
clauses = ArrayUtil.append(clauses, _ctx.getMarshaller(type).makePrimaryKey(primaryKey)); clauses = ArrayUtil.append(clauses, _ctx.getMarshaller(type).makePrimaryKey(primaryKey));
return load(type, clauses); return load(type, clauses);
@@ -92,7 +91,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> T load (Class<T> type, String ix, Comparable<?> val, protected <T extends PersistentRecord> T load (Class<T> type, String ix, Comparable<?> val,
QueryClause... clauses) QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
clauses = ArrayUtil.append(clauses, new Key<T>(type, ix, val)); clauses = ArrayUtil.append(clauses, new Key<T>(type, ix, val));
return load(type, clauses); return load(type, clauses);
@@ -104,7 +103,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> T load (Class<T> type, String ix1, Comparable<?> val1, protected <T extends PersistentRecord> T load (Class<T> type, String ix1, Comparable<?> val1,
String ix2, Comparable<?> val2, String ix2, Comparable<?> val2,
QueryClause... clauses) QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
clauses = ArrayUtil.append(clauses, new Key<T>(type, ix1, val1, ix2, val2)); clauses = ArrayUtil.append(clauses, new Key<T>(type, ix1, val1, ix2, val2));
return load(type, clauses); return load(type, clauses);
@@ -116,7 +115,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> T load (Class<T> type, String ix1, Comparable<?> val1, protected <T extends PersistentRecord> T load (Class<T> type, String ix1, Comparable<?> val1,
String ix2, Comparable<?> val2, String ix3, String ix2, Comparable<?> val2, String ix3,
Comparable<?> val3, QueryClause... clauses) Comparable<?> val3, QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
clauses = ArrayUtil.append(clauses, new Key<T>(type, ix1, val1, ix2, val2, ix3, val3)); clauses = ArrayUtil.append(clauses, new Key<T>(type, ix1, val1, ix2, val2, ix3, val3));
return load(type, clauses); return load(type, clauses);
@@ -127,7 +126,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> T load ( protected <T extends PersistentRecord> T load (
Class<T> type, Collection<? extends QueryClause> clauses) Class<T> type, Collection<? extends QueryClause> clauses)
throws PersistenceException throws DatabaseException
{ {
return load(type, clauses.toArray(new QueryClause[clauses.size()])); return load(type, clauses.toArray(new QueryClause[clauses.size()]));
} }
@@ -136,7 +135,7 @@ public abstract class DepotRepository
* Loads the first persistent object that matches the supplied query clauses. * Loads the first persistent object that matches the supplied query clauses.
*/ */
protected <T extends PersistentRecord> T load (Class<T> type, QueryClause... clauses) protected <T extends PersistentRecord> T load (Class<T> type, QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
return _ctx.invoke(new FindOneQuery<T>(_ctx, type, clauses)); return _ctx.invoke(new FindOneQuery<T>(_ctx, type, clauses));
} }
@@ -155,7 +154,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> List<T> findAll ( protected <T extends PersistentRecord> List<T> findAll (
Class<T> type, Collection<? extends QueryClause> clauses) Class<T> type, Collection<? extends QueryClause> clauses)
throws PersistenceException throws DatabaseException
{ {
DepotMarshaller<T> marsh = _ctx.getMarshaller(type); DepotMarshaller<T> marsh = _ctx.getMarshaller(type);
boolean useExplicit = boolean useExplicit =
@@ -175,7 +174,7 @@ public abstract class DepotRepository
* A varargs version of {@link #findAll(Class<T>,Collection<QueryClause>)}. * A varargs version of {@link #findAll(Class<T>,Collection<QueryClause>)}.
*/ */
protected <T extends PersistentRecord> List<T> findAll (Class<T> type, QueryClause... clauses) protected <T extends PersistentRecord> List<T> findAll (Class<T> type, QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
return findAll(type, Arrays.asList(clauses)); return findAll(type, Arrays.asList(clauses));
} }
@@ -187,7 +186,7 @@ public abstract class DepotRepository
* @return the number of rows modified by this action, this should always be one. * @return the number of rows modified by this action, this should always be one.
*/ */
protected <T extends PersistentRecord> int insert (T record) protected <T extends PersistentRecord> int insert (T record)
throws PersistenceException throws DatabaseException
{ {
@SuppressWarnings("unchecked") final Class<T> pClass = (Class<T>) record.getClass(); @SuppressWarnings("unchecked") final Class<T> pClass = (Class<T>) record.getClass();
final DepotMarshaller<T> marsh = _ctx.getMarshaller(pClass); final DepotMarshaller<T> marsh = _ctx.getMarshaller(pClass);
@@ -238,7 +237,7 @@ public abstract class DepotRepository
* @return the number of rows modified by this action. * @return the number of rows modified by this action.
*/ */
protected <T extends PersistentRecord> int update (T record) protected <T extends PersistentRecord> int update (T record)
throws PersistenceException throws DatabaseException
{ {
@SuppressWarnings("unchecked") Class<T> pClass = (Class<T>) record.getClass(); @SuppressWarnings("unchecked") Class<T> pClass = (Class<T>) record.getClass();
requireNotComputed(pClass, "update"); requireNotComputed(pClass, "update");
@@ -275,7 +274,7 @@ public abstract class DepotRepository
* @return the number of rows modified by this action. * @return the number of rows modified by this action.
*/ */
protected <T extends PersistentRecord> int update (T record, final String... modifiedFields) protected <T extends PersistentRecord> int update (T record, final String... modifiedFields)
throws PersistenceException throws DatabaseException
{ {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Class<T> pClass = (Class<T>) record.getClass(); Class<T> pClass = (Class<T>) record.getClass();
@@ -322,7 +321,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> int updatePartial ( protected <T extends PersistentRecord> int updatePartial (
Class<T> type, Comparable<?> primaryKey, Map<String,Object> updates) Class<T> type, Comparable<?> primaryKey, Map<String,Object> updates)
throws PersistenceException throws DatabaseException
{ {
Object[] fieldsValues = new Object[updates.size()*2]; Object[] fieldsValues = new Object[updates.size()*2];
int idx = 0; int idx = 0;
@@ -345,7 +344,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> int updatePartial ( protected <T extends PersistentRecord> int updatePartial (
Class<T> type, Comparable<?> primaryKey, Object... fieldsValues) Class<T> type, Comparable<?> primaryKey, Object... fieldsValues)
throws PersistenceException throws DatabaseException
{ {
return updatePartial(_ctx.getMarshaller(type).makePrimaryKey(primaryKey), fieldsValues); return updatePartial(_ctx.getMarshaller(type).makePrimaryKey(primaryKey), fieldsValues);
} }
@@ -364,7 +363,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> int updatePartial ( protected <T extends PersistentRecord> int updatePartial (
Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2, Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2,
Object... fieldsValues) Object... fieldsValues)
throws PersistenceException throws DatabaseException
{ {
return updatePartial(new Key<T>(type, ix1, val1, ix2, val2), fieldsValues); return updatePartial(new Key<T>(type, ix1, val1, ix2, val2), fieldsValues);
} }
@@ -383,7 +382,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> int updatePartial ( protected <T extends PersistentRecord> int updatePartial (
Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2, Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2,
String ix3, Comparable<?> val3, Object... fieldsValues) String ix3, Comparable<?> val3, Object... fieldsValues)
throws PersistenceException throws DatabaseException
{ {
return updatePartial(new Key<T>(type, ix1, val1, ix2, val2, ix3, val3), fieldsValues); return updatePartial(new Key<T>(type, ix1, val1, ix2, val2, ix3, val3), fieldsValues);
} }
@@ -398,7 +397,7 @@ public abstract class DepotRepository
* @return the number of rows modified by this action. * @return the number of rows modified by this action.
*/ */
protected <T extends PersistentRecord> int updatePartial (Key<T> key, Object... fieldsValues) protected <T extends PersistentRecord> int updatePartial (Key<T> key, Object... fieldsValues)
throws PersistenceException throws DatabaseException
{ {
return updatePartial(key.condition.getPersistentClass(), key, key, fieldsValues); return updatePartial(key.condition.getPersistentClass(), key, key, fieldsValues);
} }
@@ -420,7 +419,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> int updatePartial ( protected <T extends PersistentRecord> int updatePartial (
Class<T> type, final WhereClause key, CacheInvalidator invalidator, Object... fieldsValues) Class<T> type, final WhereClause key, CacheInvalidator invalidator, Object... fieldsValues)
throws PersistenceException throws DatabaseException
{ {
if (invalidator instanceof ValidatingCacheInvalidator) { if (invalidator instanceof ValidatingCacheInvalidator) {
((ValidatingCacheInvalidator)invalidator).validateFlushType(type); // sanity check ((ValidatingCacheInvalidator)invalidator).validateFlushType(type); // sanity check
@@ -470,7 +469,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> int updateLiteral ( protected <T extends PersistentRecord> int updateLiteral (
Class<T> type, Comparable<?> primaryKey, Map<String, SQLExpression> fieldsToValues) Class<T> type, Comparable<?> primaryKey, Map<String, SQLExpression> fieldsToValues)
throws PersistenceException throws DatabaseException
{ {
Key<T> key = _ctx.getMarshaller(type).makePrimaryKey(primaryKey); Key<T> key = _ctx.getMarshaller(type).makePrimaryKey(primaryKey);
return updateLiteral(type, key, key, fieldsToValues); return updateLiteral(type, key, key, fieldsToValues);
@@ -496,7 +495,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> int updateLiteral ( protected <T extends PersistentRecord> int updateLiteral (
Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2, Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2,
Map<String, SQLExpression> fieldsToValues) Map<String, SQLExpression> fieldsToValues)
throws PersistenceException throws DatabaseException
{ {
Key<T> key = new Key<T>(type, ix1, val1, ix2, val2); Key<T> key = new Key<T>(type, ix1, val1, ix2, val2);
return updateLiteral(type, key, key, fieldsToValues); return updateLiteral(type, key, key, fieldsToValues);
@@ -522,7 +521,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> int updateLiteral ( protected <T extends PersistentRecord> int updateLiteral (
Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2, Class<T> type, String ix1, Comparable<?> val1, String ix2, Comparable<?> val2,
String ix3, Comparable<?> val3, Map<String, SQLExpression> fieldsToValues) String ix3, Comparable<?> val3, Map<String, SQLExpression> fieldsToValues)
throws PersistenceException throws DatabaseException
{ {
Key<T> key = new Key<T>(type, ix1, val1, ix2, val2, ix3, val3); Key<T> key = new Key<T>(type, ix1, val1, ix2, val2, ix3, val3);
return updateLiteral(type, key, key, fieldsToValues); return updateLiteral(type, key, key, fieldsToValues);
@@ -548,7 +547,7 @@ public abstract class DepotRepository
protected <T extends PersistentRecord> int updateLiteral ( protected <T extends PersistentRecord> int updateLiteral (
Class<T> type, final WhereClause key, CacheInvalidator invalidator, Class<T> type, final WhereClause key, CacheInvalidator invalidator,
Map<String, SQLExpression> fieldsToValues) Map<String, SQLExpression> fieldsToValues)
throws PersistenceException throws DatabaseException
{ {
requireNotComputed(type, "updateLiteral"); requireNotComputed(type, "updateLiteral");
@@ -592,7 +591,7 @@ public abstract class DepotRepository
* @return true if the record was created, false if it was updated. * @return true if the record was created, false if it was updated.
*/ */
protected <T extends PersistentRecord> boolean store (T record) protected <T extends PersistentRecord> boolean store (T record)
throws PersistenceException throws DatabaseException
{ {
@SuppressWarnings("unchecked") final Class<T> pClass = (Class<T>) record.getClass(); @SuppressWarnings("unchecked") final Class<T> pClass = (Class<T>) record.getClass();
requireNotComputed(pClass, "store"); requireNotComputed(pClass, "store");
@@ -665,7 +664,7 @@ public abstract class DepotRepository
* @return the number of rows deleted by this action. * @return the number of rows deleted by this action.
*/ */
protected <T extends PersistentRecord> int delete (T record) protected <T extends PersistentRecord> int delete (T record)
throws PersistenceException throws DatabaseException
{ {
@SuppressWarnings("unchecked") Class<T> type = (Class<T>)record.getClass(); @SuppressWarnings("unchecked") Class<T> type = (Class<T>)record.getClass();
Key<T> primaryKey = _ctx.getMarshaller(type).getPrimaryKey(record); Key<T> primaryKey = _ctx.getMarshaller(type).getPrimaryKey(record);
@@ -683,7 +682,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> int delete ( protected <T extends PersistentRecord> int delete (
Class<T> type, Comparable<?> primaryKeyValue) Class<T> type, Comparable<?> primaryKeyValue)
throws PersistenceException throws DatabaseException
{ {
return delete(type, _ctx.getMarshaller(type).makePrimaryKey(primaryKeyValue)); return delete(type, _ctx.getMarshaller(type).makePrimaryKey(primaryKeyValue));
} }
@@ -695,7 +694,7 @@ public abstract class DepotRepository
* @return the number of rows deleted by this action. * @return the number of rows deleted by this action.
*/ */
protected <T extends PersistentRecord> int delete (Class<T> type, Key<T> primaryKey) protected <T extends PersistentRecord> int delete (Class<T> type, Key<T> primaryKey)
throws PersistenceException throws DatabaseException
{ {
return deleteAll(type, primaryKey, primaryKey); return deleteAll(type, primaryKey, primaryKey);
} }
@@ -707,7 +706,7 @@ public abstract class DepotRepository
*/ */
protected <T extends PersistentRecord> int deleteAll ( protected <T extends PersistentRecord> int deleteAll (
Class<T> type, final WhereClause key, CacheInvalidator invalidator) Class<T> type, final WhereClause key, CacheInvalidator invalidator)
throws PersistenceException throws DatabaseException
{ {
if (invalidator instanceof ValidatingCacheInvalidator) { if (invalidator instanceof ValidatingCacheInvalidator) {
((ValidatingCacheInvalidator)invalidator).validateFlushType(type); // sanity check ((ValidatingCacheInvalidator)invalidator).validateFlushType(type); // sanity check
@@ -733,14 +732,14 @@ public abstract class DepotRepository
// make sure the given type corresponds to a concrete class // make sure the given type corresponds to a concrete class
protected void requireNotComputed (Class<? extends PersistentRecord> type, String action) protected void requireNotComputed (Class<? extends PersistentRecord> type, String action)
throws PersistenceException throws DatabaseException
{ {
DepotMarshaller<?> marsh = _ctx.getMarshaller(type); DepotMarshaller<?> marsh = _ctx.getMarshaller(type);
if (marsh == null) { if (marsh == null) {
throw new PersistenceException("Unknown persistent type [class=" + type + "]"); throw new DatabaseException("Unknown persistent type [class=" + type + "]");
} }
if (marsh.getTableName() == null) { if (marsh.getTableName() == null) {
throw new PersistenceException( throw new DatabaseException(
"Can't " + action + " computed entities [class=" + type + "]"); "Can't " + action + " computed entities [class=" + type + "]");
} }
} }
@@ -29,7 +29,6 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.depot.clause.QueryClause; import com.samskivert.jdbc.depot.clause.QueryClause;
import com.samskivert.jdbc.depot.expression.SQLExpression; import com.samskivert.jdbc.depot.expression.SQLExpression;
@@ -40,9 +39,9 @@ import com.samskivert.jdbc.depot.expression.SQLExpression;
* constructed. * constructed.
* *
* The main motivation for breaking this functionality out into its own class is to encapsulate the * The main motivation for breaking this functionality out into its own class is to encapsulate the
* operation that throws {@link PersistenceException} as separate from the operations that throw * operation that throws {@link DatabaseException} as separate from the operations that throw
* {@link SQLException}. Once this class has been constructed, it may be used to create {@link * {@link SQLException}. Once this class has been constructed, it may be used to create {@link
* SQLBuilder} instances without any {@link PersistenceException} worries. * SQLBuilder} instances without any {@link DatabaseException} worries.
*/ */
public class DepotTypes public class DepotTypes
{ {
@@ -56,7 +55,7 @@ public class DepotTypes
*/ */
public static <T extends PersistentRecord> DepotTypes getDepotTypes ( public static <T extends PersistentRecord> DepotTypes getDepotTypes (
PersistenceContext ctx, Collection<? extends QueryClause> clauses) PersistenceContext ctx, Collection<? extends QueryClause> clauses)
throws PersistenceException throws DatabaseException
{ {
Set<Class<? extends PersistentRecord>> classSet = Set<Class<? extends PersistentRecord>> classSet =
new HashSet<Class<? extends PersistentRecord>>(); new HashSet<Class<? extends PersistentRecord>>();
@@ -73,7 +72,7 @@ public class DepotTypes
*/ */
public static <T extends PersistentRecord> DepotTypes getDepotTypes ( public static <T extends PersistentRecord> DepotTypes getDepotTypes (
PersistenceContext ctx, QueryClause... clauses) PersistenceContext ctx, QueryClause... clauses)
throws PersistenceException throws DatabaseException
{ {
return getDepotTypes(ctx, Arrays.asList(clauses)); return getDepotTypes(ctx, Arrays.asList(clauses));
} }
@@ -83,7 +82,7 @@ public class DepotTypes
* persistent record classes. * persistent record classes.
*/ */
public DepotTypes (PersistenceContext ctx, Collection<Class<? extends PersistentRecord>> others) public DepotTypes (PersistenceContext ctx, Collection<Class<? extends PersistentRecord>> others)
throws PersistenceException throws DatabaseException
{ {
for (Class<? extends PersistentRecord> c : others) { for (Class<? extends PersistentRecord> c : others) {
addClass(ctx, c); addClass(ctx, c);
@@ -95,7 +94,7 @@ public class DepotTypes
* persistent record. * persistent record.
*/ */
public DepotTypes (PersistenceContext ctx, Class<? extends PersistentRecord> pClass) public DepotTypes (PersistenceContext ctx, Class<? extends PersistentRecord> pClass)
throws PersistenceException throws DatabaseException
{ {
addClass(ctx, pClass); addClass(ctx, pClass);
} }
@@ -165,7 +164,7 @@ public class DepotTypes
* Register a new persistent class with this object. * Register a new persistent class with this object.
*/ */
public void addClass (PersistenceContext ctx, Class <? extends PersistentRecord> type) public void addClass (PersistenceContext ctx, Class <? extends PersistentRecord> type)
throws PersistenceException throws DatabaseException
{ {
if (_classMap.containsKey(type)) { if (_classMap.containsKey(type)) {
return; return;
@@ -0,0 +1,33 @@
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2008 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.jdbc.depot;
/**
* Thrown when an insert or update results in a duplicate key on a column that has a uniqueness
* constraint.
*/
public class DuplicateKeyException extends DatabaseException
{
public DuplicateKeyException (String message)
{
super(message);
}
}
@@ -33,7 +33,6 @@ import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ColumnDefinition; import com.samskivert.jdbc.ColumnDefinition;
import com.samskivert.jdbc.depot.annotation.Column; import com.samskivert.jdbc.depot.annotation.Column;
import com.samskivert.jdbc.depot.annotation.Computed; import com.samskivert.jdbc.depot.annotation.Computed;
@@ -116,7 +115,7 @@ public abstract class FieldMarshaller<T>
* definition according to the appropriate database dialect. * definition according to the appropriate database dialect.
*/ */
public void init (SQLBuilder builder) public void init (SQLBuilder builder)
throws PersistenceException throws DatabaseException
{ {
_columnDefinition = builder.buildColumnDefinition(this); _columnDefinition = builder.buildColumnDefinition(this);
} }
@@ -34,8 +34,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.jdbc.JDBCUtil; import com.samskivert.jdbc.JDBCUtil;
@@ -63,7 +61,7 @@ public abstract class FindAllQuery<T extends PersistentRecord>
{ {
public WithCache (PersistenceContext ctx, Class<T> type, public WithCache (PersistenceContext ctx, Class<T> type,
Collection<? extends QueryClause> clauses) Collection<? extends QueryClause> clauses)
throws PersistenceException throws DatabaseException
{ {
super(ctx, type); super(ctx, type);
@@ -84,7 +82,8 @@ public abstract class FindAllQuery<T extends PersistentRecord>
_clauses = clauses; _clauses = clauses;
} }
public List<T> invoke (Connection conn, DatabaseLiaison liaison) throws SQLException public List<T> invoke (Connection conn, DatabaseLiaison liaison)
throws SQLException
{ {
Map<Key<T>, T> entities = new HashMap<Key<T>, T>(); Map<Key<T>, T> entities = new HashMap<Key<T>, T>();
List<Key<T>> allKeys = new ArrayList<Key<T>>(); List<Key<T>> allKeys = new ArrayList<Key<T>>();
@@ -205,7 +204,7 @@ public abstract class FindAllQuery<T extends PersistentRecord>
{ {
public Explicitly (PersistenceContext ctx, Class<T> type, public Explicitly (PersistenceContext ctx, Class<T> type,
Collection<? extends QueryClause> clauses) Collection<? extends QueryClause> clauses)
throws PersistenceException throws DatabaseException
{ {
super(ctx, type); super(ctx, type);
SelectClause<T> select = new SelectClause<T>(type, _marsh.getFieldNames(), clauses); SelectClause<T> select = new SelectClause<T>(type, _marsh.getFieldNames(), clauses);
@@ -213,7 +212,8 @@ public abstract class FindAllQuery<T extends PersistentRecord>
_builder.newQuery(select); _builder.newQuery(select);
} }
public List<T> invoke (Connection conn, DatabaseLiaison liaison) throws SQLException public List<T> invoke (Connection conn, DatabaseLiaison liaison)
throws SQLException
{ {
List<T> result = new ArrayList<T>(); List<T> result = new ArrayList<T>();
PreparedStatement stmt = _builder.prepare(conn); PreparedStatement stmt = _builder.prepare(conn);
@@ -230,7 +230,7 @@ public abstract class FindAllQuery<T extends PersistentRecord>
} }
public FindAllQuery (PersistenceContext ctx, Class<T> type) public FindAllQuery (PersistenceContext ctx, Class<T> type)
throws PersistenceException throws DatabaseException
{ {
_ctx = ctx; _ctx = ctx;
_type = type; _type = type;
@@ -25,8 +25,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.jdbc.JDBCUtil; import com.samskivert.jdbc.JDBCUtil;
import com.samskivert.jdbc.depot.clause.QueryClause; import com.samskivert.jdbc.depot.clause.QueryClause;
@@ -39,7 +37,7 @@ public class FindOneQuery<T extends PersistentRecord>
implements Query<T> implements Query<T>
{ {
public FindOneQuery (PersistenceContext ctx, Class<T> type, QueryClause[] clauses) public FindOneQuery (PersistenceContext ctx, Class<T> type, QueryClause[] clauses)
throws PersistenceException throws DatabaseException
{ {
_marsh = ctx.getMarshaller(type); _marsh = ctx.getMarshaller(type);
_select = new SelectClause<T>(type, _marsh.getFieldNames(), clauses); _select = new SelectClause<T>(type, _marsh.getFieldNames(), clauses);
@@ -34,7 +34,6 @@ import com.samskivert.util.StringUtil;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.jdbc.DuplicateKeyException;
import com.samskivert.jdbc.LiaisonRegistry; import com.samskivert.jdbc.LiaisonRegistry;
import com.samskivert.jdbc.MySQLLiaison; import com.samskivert.jdbc.MySQLLiaison;
import com.samskivert.jdbc.PostgreSQLLiaison; import com.samskivert.jdbc.PostgreSQLLiaison;
@@ -219,7 +218,7 @@ public class PersistenceContext
* it if necessary. * it if necessary.
*/ */
public <T extends PersistentRecord> DepotMarshaller<T> getMarshaller (Class<T> type) public <T extends PersistentRecord> DepotMarshaller<T> getMarshaller (Class<T> type)
throws PersistenceException throws DatabaseException
{ {
DepotMarshaller<T> marshaller = getRawMarshaller(type); DepotMarshaller<T> marshaller = getRawMarshaller(type);
try { try {
@@ -231,8 +230,8 @@ public class PersistenceContext
log.warning("Record initialized lazily [type=" + type.getName() + "]."); log.warning("Record initialized lazily [type=" + type.getName() + "].");
} }
} }
} catch (PersistenceException pe) { } catch (DatabaseException pe) {
throw (PersistenceException)new PersistenceException( throw (DatabaseException)new DatabaseException(
"Failed to initialize marshaller [type=" + type + "].").initCause(pe); "Failed to initialize marshaller [type=" + type + "].").initCause(pe);
} }
return marshaller; return marshaller;
@@ -242,7 +241,7 @@ public class PersistenceContext
* Invokes a non-modifying query and returns its result. * Invokes a non-modifying query and returns its result.
*/ */
public <T> T invoke (Query<T> query) public <T> T invoke (Query<T> query)
throws PersistenceException throws DatabaseException
{ {
CacheKey key = query.getCacheKey(); CacheKey key = query.getCacheKey();
// if there is a cache key, check the cache // if there is a cache key, check the cache
@@ -270,7 +269,7 @@ public class PersistenceContext
* Invokes a modifying query and returns the number of rows modified. * Invokes a modifying query and returns the number of rows modified.
*/ */
public int invoke (Modifier modifier) public int invoke (Modifier modifier)
throws PersistenceException throws DatabaseException
{ {
modifier.cacheInvalidation(this); modifier.cacheInvalidation(this);
int rows = (Integer) invoke(null, modifier, true); int rows = (Integer) invoke(null, modifier, true);
@@ -456,7 +455,7 @@ public class PersistenceContext
* to ensure that those records are properly registered prior to this call. * to ensure that those records are properly registered prior to this call.
*/ */
public void initializeManagedRecords (boolean warnOnLazyInit) public void initializeManagedRecords (boolean warnOnLazyInit)
throws PersistenceException throws DatabaseException
{ {
for (Class<? extends PersistentRecord> rclass : _managedRecords) { for (Class<? extends PersistentRecord> rclass : _managedRecords) {
getMarshaller(rclass); getMarshaller(rclass);
@@ -495,10 +494,16 @@ public class PersistenceContext
*/ */
protected <T> Object invoke ( protected <T> Object invoke (
Query<T> query, Modifier modifier, boolean retryOnTransientFailure) Query<T> query, Modifier modifier, boolean retryOnTransientFailure)
throws PersistenceException throws DatabaseException
{ {
boolean isReadOnlyQuery = (query != null); boolean isReadOnlyQuery = (query != null);
Connection conn = _conprov.getConnection(_ident, isReadOnlyQuery); Connection conn;
try {
conn = _conprov.getConnection(_ident, isReadOnlyQuery);
} catch (PersistenceException pe) {
throw new DatabaseException(pe.getMessage(), pe.getCause());
}
// TEMP: we synchronize on the connection to cooperate with SimpleRepository when used in // TEMP: we synchronize on the connection to cooperate with SimpleRepository when used in
// conjunction with a StaticConnectionProvider; at some point we'll switch to standard JDBC // conjunction with a StaticConnectionProvider; at some point we'll switch to standard JDBC
// connection pooling which will block in getConnection() instead of returning a connection // connection pooling which will block in getConnection() instead of returning a connection
@@ -538,7 +543,7 @@ public class PersistenceContext
} else { } else {
String msg = isReadOnlyQuery ? String msg = isReadOnlyQuery ?
"Query failure " + query : "Modifier failure " + modifier; "Query failure " + query : "Modifier failure " + modifier;
throw new PersistenceException(msg, sqe); throw new DatabaseException(msg, sqe);
} }
} finally { } finally {