diff -r ab576c51e77c -r dd74a80a34d6 src/net/lemnik/eodsql/spi/Context.java --- a/src/net/lemnik/eodsql/spi/Context.java Sun Sep 19 22:06:50 2010 +0200 +++ b/src/net/lemnik/eodsql/spi/Context.java Fri Oct 29 21:13:08 2010 +0200 @@ -53,6 +53,8 @@ private boolean dontCloseConnection = false; private boolean closed = false; + + private final boolean childContext; /** *
@@ -75,6 +77,7 @@ this.parameters = parameters != null ? parameters : EMPTY_PARAMETERS; + this.childContext = false; } /** @@ -82,8 +85,13 @@ * {@code Context} with new parameters. This constructor will copy * the annotation and all of the current resources from the * {@code parentContext} into itself. Resources added to the parent - * context after this construction will not affect the new {@code Context}, - * and vice-versa. + * context after this construction will not affect the new {@code Context}. + *
+ * This context is read-only. Calling {@link #setResource(Resource)} will throw an + * {@link IllegalStateException}. Calling {@link #close()} on this context is a no-op, i.e. + * no resources will be freed when calling this. It is the responsibility of the parent context + * to free the resources. + *
* * @param parentContext the context to copy the state from * @param newParameters the new parameters @@ -108,6 +116,7 @@ this.resources.putAll(parentContext.resources); this.parameters = newParameters; this.autoclose = parentContext.autoclose; + this.childContext = true; } @Override @@ -176,6 +185,10 @@ * @param resource the {@code Resource} to place into this {@code Context} */ public void setResource(final Resource> resource) { + if (childContext) + { + throw new IllegalStateException("Cannot add resource to nested context"); + } resources.put(resource.getResourceType(), resource); } @@ -259,7 +272,7 @@ * @see #getResource(java.lang.Class) */ public void close() throws SQLException { - if(!closed) { + if(!closed && !childContext) { closed = true; // we want to iterate through the values in reverse