[Mulgara-svn] r450 - branches/nw-interface/src/jar/itql/java/org/mulgara/itql
pag at mulgara.org
pag at mulgara.org
Mon Sep 24 21:01:42 UTC 2007
Author: pag
Date: 2007-09-24 16:01:42 -0500 (Mon, 24 Sep 2007)
New Revision: 450
Modified:
branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java
Log:
Checks for entering and leaving transactions, and sets the state on connections appropriately.
Modified: branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java
===================================================================
--- branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java 2007-09-24 21:00:59 UTC (rev 449)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java 2007-09-24 21:01:42 UTC (rev 450)
@@ -12,12 +12,13 @@
package org.mulgara.itql;
import java.net.URI;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Collection;
+import java.util.LinkedList;
import org.apache.log4j.Logger;
import org.mulgara.connection.Connection;
import org.mulgara.connection.ConnectionException;
+import org.mulgara.connection.ConnectionFactory;
import org.mulgara.parser.Interpreter;
import org.mulgara.query.Answer;
import org.mulgara.query.QueryException;
@@ -49,14 +50,20 @@
/** The most recent exception, if there was one. */
private Exception lastException;
- /** Cached connections to servers. Duplicates may exist if more than one URI is used to refer to the same server. */
- private Map<URI,Connection> establishedConnections = new HashMap<URI,Connection>();
-
+ /** Factory for building and caching connections. */
+ private ConnectionFactory connectionFactory = new ConnectionFactory();
+ /** Indicates that the client is in a transaction. */
+ private boolean inTransaction;
+
+ /** All the connections involved in the current transaction. */
+ private Collection<Connection> transConnections = new LinkedList<Connection>();
+
/**
* Creates a new autointerpreter with no prior connections.
*/
public TqlAutoInterpreter() {
+ inTransaction = false;
resetState();
}
@@ -83,6 +90,7 @@
// set up a connection, if required
Connection conn = cmd.isLocalOperation() ? null : establishConnection(cmd.getServerURI());
cmd.execute(conn);
+ updateOnConnectionState(conn);
lastMessage = cmd.getResultMessage();
} catch (Exception e) {
lastException = e;
@@ -94,6 +102,28 @@
}
+ /**
+ * Perform any actions required on the update of the state of a connection.
+ * @param conn The connection whose state needs checking.
+ * @throws QueryException Can be caused by a failed change into a transaction.
+ */
+ private void updateOnConnectionState(Connection conn) throws QueryException {
+ // check if the transaction state changed.
+ if (inTransaction == conn.getAutoCommit()) {
+ // update the client state
+ inTransaction = !conn.getAutoCommit();
+ if (inTransaction) {
+ // Started a transaction. All connection changes set this state on the new connection.
+ assert transConnections.isEmpty();
+ } else {
+ // Closed a transaction. Commit all outstanding transactions.
+ for (Connection c: transConnections) c.setAutoCommit(true);
+ transConnections.clear();
+ }
+ }
+ }
+
+
/** @return the message set from the last operation */
public String getLastMessage() { return lastMessage; }
@@ -108,13 +138,7 @@
* Close any resources that are still in use.
*/
public void close() {
- for (Connection c: establishedConnections.values()) {
- try {
- c.close();
- } catch (QueryException e) {
- logger.warn("Unable to close connection", e);
- }
- }
+ connectionFactory.closeAll();
}
@@ -133,13 +157,16 @@
* @param serverUri The URI for the server to get a connection to.
* @return A connection to the server, cached if available.
* @throws ConnectionException It was not possible to create a connection to the described server.
+ * @throws QueryException There is a transaction underway, but the new connection cannot turn off autocommit.
*/
- private Connection establishConnection(URI serverUri) throws ConnectionException {
+ private Connection establishConnection(URI serverUri) throws ConnectionException, QueryException {
if (serverUri == null) throw new IllegalArgumentException("Server URI may not be null.");
- Connection connection = establishedConnections.get(serverUri);
- if (connection == null) {
- connection = new Connection(serverUri);
- establishedConnections.put(serverUri, connection);
+ Connection connection = connectionFactory.newConnection(serverUri);
+ // If in a transaction, turn off autocommit
+ if (inTransaction && connection.getAutoCommit()) {
+ connection.setAutoCommit(false);
+ assert !transConnections.contains(connection);
+ transConnections.add(connection);
}
return connection;
}
More information about the Mulgara-svn
mailing list