[Mulgara-svn] r527 - in branches/nw-interface/src/jar: itql/java/org/mulgara/itql query/java/org/mulgara/query/operation
pag at mulgara.org
pag at mulgara.org
Fri Nov 9 21:26:06 UTC 2007
Author: pag
Date: 2007-11-09 15:26:06 -0600 (Fri, 09 Nov 2007)
New Revision: 527
Modified:
branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Command.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Commit.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/LocalCommand.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Rollback.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/ServerCommand.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/SetAutoCommit.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TransactionCommand.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TxOp.java
Log:
Refactored the interpreter to handle transaction commands better, including closing connections while a transaction is still open. This introduced the TxOp interface for those commands that affect the transaction state.
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-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -26,7 +26,11 @@
import org.mulgara.query.Answer;
import org.mulgara.query.QueryException;
import org.mulgara.query.operation.Command;
+import org.mulgara.query.operation.Commit;
import org.mulgara.query.operation.LocalCommand;
+import org.mulgara.query.operation.Rollback;
+import org.mulgara.query.operation.SetAutoCommit;
+import org.mulgara.query.operation.TxOp;
import org.mulgara.server.Session;
/**
@@ -141,33 +145,6 @@
}
- /**
- * Perform any actions required on the update of the state of a connection.
- * Most commands will skip through this method. Only transaction commands do anything.
- * @param conn The connection whose state needs checking.
- * @throws QueryException Can be caused by a failed change into a transaction.
- */
- void updateConnectionsForTx(Connection conn, Command cmd) throws QueryException {
- // check if the transaction state changed. This will happen for setAutocommit operations.
- if (inTransaction == conn.getAutoCommit()) {
- assert cmd instanceof org.mulgara.query.operation.SetAutoCommit;
- // 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();
- }
- } else if (cmd.isTxOperation()) {
- // this is a commit or rollback. Inform every connection we've used in this transaction.
- operateOnTxConnections(cmd);
- }
- }
-
-
/** @return the message set from the last operation */
public String getLastMessage() { return lastMessage; }
@@ -179,10 +156,18 @@
/**
- * Close any resources that are still in use.
+ * Close any resources that are still in use, and rolls back any outstanding transactions.
*/
public void close() {
- transConnections.clear();
+ if (inTransaction) {
+ logger.info("Closing a current transaction. Rolling back.");
+ try {
+ handleTxOp(new SetAutoCommit(true));
+ } catch (QueryException e) {
+ logger.error("Error while cleaning up a transaction", e);
+ }
+ }
+ assert transConnections.isEmpty();
connectionFactory.closeAll();
}
@@ -311,23 +296,22 @@
/**
* Commits all connections that started on a transaction. This operates directly
* on all known transacted connections.
- * @deprecated This is only for manual transactions managed from ItqlInterpreterBean.
* @throws QueryException One of the connections could not be successfully committed.
*/
void commitAll() throws QueryException {
- handleEndOfTx(TxEndOp.COMMIT);
+ handleTxOp(new Commit());
}
/**
* Rolls back all connections that started on a transaction. This oeprates directly
* on all known transacted connections.
- * @deprecated This is only for manual transactions managed from ItqlInterpreterBean.
* @throws QueryException One of the connections could not be successfully rolled back.
*/
void rollbackAll() throws QueryException {
- handleEndOfTx(TxEndOp.ROLLBACK);
+ handleTxOp(new Rollback());
}
-
+
+
/**
* Seeds the cache with a connection wrapping the given session.
* @deprecated Only for use by {@link ItqlInterpreterBean}.
@@ -344,67 +328,70 @@
/**
- * This method wraps the simple loop of applying a command to all transaction connections.
- * The wrapping is done to attempt the operation on all connections, despite exceptions
- * being thrown.
- * @param cmd The operation to execute on the transaction connections.
- * @throws QueryException If there was an error applying the operation. If multiple
- * errors occur, then this will be the first one.
+ * Perform any actions required on the update of the state of a connection.
+ * Most commands will skip through this method. Only transaction commands do anything.
+ * @param conn The connection whose state needs checking.
+ * @throws QueryException Can be caused by a failed change into a transaction.
*/
- private void operateOnTxConnections(Command cmd) throws QueryException {
- // used to record the first exception, if there is one.
- QueryException qe = null;
-
- // perform operation on all transaction connections. Catch and log exceptions.
- for (Connection c: transConnections) {
- try {
- cmd.execute(c);
- } catch (QueryException e) {
- if (qe == null) qe = e;
- else logger.error("Discarding subsequent exception during operation: " + cmd.getClass().getSimpleName(), e);
- } catch (Exception e) {
- String opName = cmd.getClass().getSimpleName();
- if (qe == null) qe = new QueryException("Unexpected exception during " + opName, e);
- else logger.error("Discarding subsequent unexpected exception during operation: " + cmd.getClass().getSimpleName(), e);
+ void updateConnectionsForTx(Connection conn, Command cmd) throws QueryException {
+ // check if the transaction state changed. This will happen for setAutocommit operations.
+ if (inTransaction == conn.getAutoCommit()) {
+ assert cmd instanceof org.mulgara.query.operation.SetAutoCommit: "Got a state change on " + cmd.getClass() + " instead of SetAutoCommit";
+ // 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 {
+ handleTxOp((TxOp)cmd);
+ assert transConnections.isEmpty();
}
+ } else if (cmd.isTxOperation()) {
+ // this is a commit or rollback. Inform every connection we've used in this transaction.
+ handleTxOp((TxOp)cmd);
}
- // if an exception was recorded, then throw it
- if (qe != null) throw qe;
}
/**
- * Enumerates a set of operations that can be performed to end a transaction.
- */
- private enum TxEndOp {
- COMMIT {
- void execute(Session s) throws QueryException { s.commit(); }
- },
- ROLLBACK {
- void execute(Session s) throws QueryException { s.rollback(); }
- };
- abstract void execute(Session s) throws QueryException;
- }
-
- /**
- * Performs an operation to end a transaction.
+ * This method wraps the simple loop of applying a command to all transaction connections.
+ * The wrapping is done to attempt the operation on all connections, despite exceptions
+ * being thrown.
* @param op The operation to end the transaction.
* @throws QueryException The operation could not be successfully performed.
*/
- private void handleEndOfTx(TxEndOp op) throws QueryException {
+ private void handleTxOp(TxOp op) throws QueryException {
+ // used to record the first exception, if there is one.
+ QueryException qe = null;
+ String errorMessage = null;
+
if (inTransaction) {
// Operate on all outstanding transactions.
Iterator<Connection> c = transConnections.iterator();
while (c.hasNext()) {
- Session s = c.next().getSession();
- if (s != null) op.execute(s);
- else logger.warn("Found transaction on a connection with no session.");
- // now get rid of the commited/rolled-back connection
- c.remove();
+ try {
+ // do the work
+ op.execute(c.next());
+ } catch (QueryException e) {
+ // store the details of the first exception only
+ if (qe != null) logger.error("Discarding subsequent exception during operation: " + op.getClass().getSimpleName(), e);
+ else {
+ qe = e;
+ errorMessage = op.getResultMessage();
+ }
+ }
}
- // will only get here once all connections were successfully processed.
- inTransaction = false;
+ // will only get here once all connections were processed.
+ inTransaction = op.stayInTx();
+ if (!inTransaction) transConnections.clear();
}
+
+ // if an exception was recorded, then throw it
+ if (qe != null) {
+ // remember the error message associated with the exception
+ op.setResultMessage(errorMessage);
+ throw qe;
+ }
}
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Command.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Command.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Command.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -67,7 +67,7 @@
* operation.
*/
String getResultMessage();
-
+
/**
* Indicates that this command returns an Answer. Saves the overhead of checking
* the return type of execute.
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Commit.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Commit.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Commit.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -13,8 +13,6 @@
package org.mulgara.query.operation;
-import java.util.Iterator;
-
import org.mulgara.connection.Connection;
import org.mulgara.query.QueryException;
import org.mulgara.server.Session;
@@ -56,16 +54,16 @@
/**
* {@inheritDoc}
*/
- public void cleanup(Iterator<?> it) {
- /* no-op */
+ public boolean stayInTx() {
+ return true;
}
/**
- * {@inheritDoc}
+ * Sets message text relevant to the operation. Exposes this publicly, but only for internal use.
+ * @return The set text.
*/
- public boolean stayInTx() {
- return true;
+ public String setResultMessage(String resultMessage) {
+ return super.setResultMessage(resultMessage);
}
-
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/LocalCommand.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/LocalCommand.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/LocalCommand.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -91,7 +91,7 @@
* Sets message text relevant to the operation. Useful for the UI.
* @return The set text.
*/
- protected String setResultMessage(String resultMessage) {
+ String setResultMessage(String resultMessage) {
return this.resultMessage = resultMessage;
}
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Rollback.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Rollback.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/Rollback.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -49,16 +49,16 @@
/**
* {@inheritDoc}
*/
- public void cleanup(Iterator<?> it) {
- it.remove();
+ public boolean stayInTx() {
+ return false;
}
/**
- * {@inheritDoc}
+ * Sets message text relevant to the operation. Exposes this publicly, but only for internal use.
+ * @return The set text.
*/
- public boolean stayInTx() {
- return false;
+ public String setResultMessage(String resultMessage) {
+ return super.setResultMessage(resultMessage);
}
-
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/ServerCommand.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/ServerCommand.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/ServerCommand.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -105,7 +105,7 @@
* Sets message text relevant to the operation. Useful for the UI.
* @return The set text.
*/
- protected String setResultMessage(String resultMessage) {
+ String setResultMessage(String resultMessage) {
return this.resultMessage = resultMessage;
}
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/SetAutoCommit.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/SetAutoCommit.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/SetAutoCommit.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -11,8 +11,6 @@
*/
package org.mulgara.query.operation;
-import java.util.Iterator;
-
import org.mulgara.connection.Connection;
import org.mulgara.query.QueryException;
@@ -54,18 +52,17 @@
/**
* {@inheritDoc}
*/
- public void cleanup(Iterator<?> it) {
- // if leaving a transaction, then remove the item.
- if (isOn()) it.remove();
+ public boolean stayInTx() {
+ // only stay in a transaction if autocommit is not on
+ return !isOn();
}
/**
- * {@inheritDoc}
+ * Sets message text relevant to the operation. Exposes this publicly, but only for internal use.
+ * @return The set text.
*/
- public boolean stayInTx() {
- // only stay in a transaction if autocommit is not on
- return !isOn();
+ public String setResultMessage(String resultMessage) {
+ return super.setResultMessage(resultMessage);
}
-
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TransactionCommand.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TransactionCommand.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TransactionCommand.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -27,9 +27,6 @@
*/
public abstract class TransactionCommand extends LocalCommand {
- /** An optional message that can be returned to a UI after execution. */
- private String resultMessage = "";
-
/**
* Indicates that the command modifies the state in a transaction.
* @return <code>true</code> If the transaction state is to be modified.
@@ -63,20 +60,4 @@
return null;
}
- /**
- * Gets a message text relevant to the operation. Useful for the UI.
- * @return A text message associated with the result of this
- * operation.
- */
- public String getResultMessage() {
- return resultMessage;
- }
-
- /**
- * Sets message text relevant to the operation. Useful for the UI.
- * @return The set text.
- */
- protected String setResultMessage(String resultMessage) {
- return this.resultMessage = resultMessage;
- }
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TxOp.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TxOp.java 2007-11-09 21:21:32 UTC (rev 526)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/operation/TxOp.java 2007-11-09 21:26:06 UTC (rev 527)
@@ -11,8 +11,6 @@
*/
package org.mulgara.query.operation;
-import java.util.Iterator;
-
import org.mulgara.connection.Connection;
import org.mulgara.query.QueryException;
@@ -36,16 +34,21 @@
Object execute(Connection conn) throws QueryException;
/**
- * This operation is to be performed on a collection of items. The cleanup method
- * describes what to do with that item once the operation has completed.
- * @param it the iterator item. Expect either a no-op or a {@link java.util.Iterator#remove()}.
- */
- void cleanup(Iterator<?> it);
-
- /**
* Indicates if this operation should result in the transaction finishing or continuing.
* @return <code>true</code> if the transaction stays open, <code>false</code> if it closes.
*/
boolean stayInTx();
+ /**
+ * Sets the result message. This is for internal use only.
+ * @param msg A text message associated with the result of this operation.
+ */
+ String setResultMessage(String msg);
+
+ /**
+ * Gets the result message. This will be mixed in from the Command interface.
+ * @return A text message associated with the result of this operation.
+ */
+ String getResultMessage();
+
}
More information about the Mulgara-svn
mailing list