changeset 60:80bae18085f0 embedded

Better handling with concurrent connections.
author Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
date Thu, 24 Nov 2016 13:59:33 +0100 (2016-11-24)
parents 37ebdc34a400
children f1de7262d8d9
files src/main/java/nl/cwi/monetdb/embedded/env/AbstractConnectionResult.java src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedException.java
diffstat 3 files changed, 29 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/embedded/env/AbstractConnectionResult.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/env/AbstractConnectionResult.java
@@ -9,6 +9,7 @@
 package nl.cwi.monetdb.embedded.env;
 
 import java.io.Closeable;
+import java.util.Random;
 
 /**
  * The base class for a pending statement to a connection.
@@ -18,20 +19,38 @@ import java.io.Closeable;
 public abstract class AbstractConnectionResult implements Closeable {
 
     /**
+     * A random instance to generate the result set identifier.
+     */
+    private static final Random Randomizer = new Random();
+
+    /**
      * The corresponding connection of this result.
      */
     private final MonetDBEmbeddedConnection connection;
 
-    protected AbstractConnectionResult(MonetDBEmbeddedConnection connection) { this.connection = connection; }
+    /**
+     * A long value used to identify this result set.
+     */
+    private final long randomIdentifier;
+
+    protected AbstractConnectionResult(MonetDBEmbeddedConnection connection) {
+        this.connection = connection;
+        this.randomIdentifier = Randomizer.nextLong();
+    }
 
     /**
-     * Get the corresponding connection to this statement result.
+     * Gets the corresponding connection to this statement result.
      *
      * @return A MonetDBEmbeddedConnection instance
      */
     public MonetDBEmbeddedConnection getConnection() { return connection; }
 
-    protected long getConnectionPointer() { return connection.connectionPointer; }
+    /**
+     * Gets a long number randomly generated, used to identify the result set.
+     *
+     * @return A random long identifier
+     */
+    protected long getRandomIdentifier() { return randomIdentifier; }
 
     /**
      * To be called by the connection when is closing.
--- a/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java
@@ -96,7 +96,7 @@ public class MonetDBEmbeddedConnection {
             query += ";";
         }
         UpdateResultSet res = this.sendUpdateInternal(this.connectionPointer, query, true);
-        results.put(res.getConnectionPointer(), res);
+        results.put(res.getRandomIdentifier(), res);
         return res;
     }
 
@@ -123,7 +123,7 @@ public class MonetDBEmbeddedConnection {
             query += ";";
 		}
         QueryResultSet res = this.sendQueryInternal(this.connectionPointer, query, true);
-        results.put(res.getConnectionPointer(), res);
+        results.put(res.getRandomIdentifier(), res);
         return res;
 	}
 
@@ -148,7 +148,7 @@ public class MonetDBEmbeddedConnection {
      */
     public MonetDBTable getMonetDBTable(String schemaName, String tableName) throws MonetDBEmbeddedException {
         MonetDBTable res = this.getMonetDBTableInternal(this.connectionPointer, schemaName, tableName);
-        results.put(res.getConnectionPointer(), res);
+        results.put(res.getRandomIdentifier(), res);
         return res;
     }
 
@@ -240,7 +240,7 @@ public class MonetDBEmbeddedConnection {
     /**
      * Removes a query result from this connection.
      */
-    protected void removeQueryResult(AbstractConnectionResult res) { this.results.remove(res.getConnectionPointer()); }
+    protected void removeQueryResult(AbstractConnectionResult res) { this.results.remove(res.getRandomIdentifier()); }
 
     /**
      * Internal implementation of sendUpdate.
--- a/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedException.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedException.java
@@ -8,12 +8,14 @@
 
 package nl.cwi.monetdb.embedded.env;
 
+import java.sql.SQLException;
+
 /**
  * The exception fired from embedded methods.
  *
  * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a>
  */
-public class MonetDBEmbeddedException extends Exception {
+public class MonetDBEmbeddedException extends SQLException {
 
     public MonetDBEmbeddedException(String message) { super(message); }
 }