Index: com/ziclix/python/sql/PyConnection.java =================================================================== --- com/ziclix/python/sql/PyConnection.java (Revision 6988) +++ com/ziclix/python/sql/PyConnection.java (Arbeitskopie) @@ -13,6 +13,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.WeakHashMap; import org.python.core.ClassDictInit; import org.python.core.Py; @@ -50,11 +51,15 @@ /** The underlying java.sql.Connection. */ protected Connection connection; - /** Underlying cursors. */ - private Set cursors; + /** Underlying cursors. + * + * Note: the appropriate container would be a WeakHashSet, + * but the Java runtime does not provide that type. + * */ + private WeakHashMap cursors; /** Underlying statements. */ - private Set statements; + private WeakHashMap statements; /** Field __members__ */ protected static PyList __members__; @@ -93,9 +98,9 @@ */ public PyConnection(Connection connection) throws SQLException { this.closed = false; - this.cursors = new HashSet(); + this.cursors = new WeakHashMap(); this.connection = connection; - this.statements = new HashSet(); + this.statements = new WeakHashMap(); this.supportsTransactions = this.connection.getMetaData().supportsTransactions(); this.supportsMultipleResultSets = this.connection.getMetaData().supportsMultipleResultSets(); @@ -223,9 +228,9 @@ } else if ("__connection__".equals(name)) { return Py.java2py(this.connection); } else if ("__cursors__".equals(name)) { - return Py.java2py(Collections.unmodifiableSet(this.cursors)); + return Py.java2py(Collections.unmodifiableSet(this.cursors.keySet())); } else if ("__statements__".equals(name)) { - return Py.java2py(Collections.unmodifiableSet(this.statements)); + return Py.java2py(Collections.unmodifiableSet(this.statements.keySet())); } else if ("__methods__".equals(name)) { return __methods__; } else if ("__members__".equals(name)) { @@ -253,14 +258,14 @@ this.closed = true; synchronized (this.cursors) { - for (PyCursor cursor: cursors) { + for (PyCursor cursor: cursors.keySet()) { cursor.close(); } this.cursors.clear(); } synchronized (this.statements) { - for (PyStatement statement : statements) { + for (PyStatement statement : statements.keySet()) { statement.close(); } this.statements.clear(); @@ -387,7 +392,7 @@ } PyCursor cursor = new PyExtendedCursor(this, dynamicFetch, rsType, rsConcur); - this.cursors.add(cursor); + this.cursors.put(cursor,Boolean.TRUE); return cursor; } @@ -400,7 +405,9 @@ if (closed) { return; } - this.cursors.remove(cursor); + synchronized (this.cursors) { + this.cursors.remove(cursor); + } } /** @@ -412,7 +419,9 @@ if (closed) { return; } - this.statements.add(statement); + synchronized (this.statements) { + this.statements.put(statement, Boolean.TRUE); + } } /** @@ -425,7 +434,9 @@ if (closed) { return false; } - return this.statements.contains(statement); + synchronized (this.statements) { + return this.statements.containsKey(statement); + } } public PyObject __enter__(ThreadState ts) {