[Mulgara-svn] r481 - branches/nw-interface/src/jar/itql/java/org/mulgara/itql
pag at mulgara.org
pag at mulgara.org
Tue Oct 16 21:16:11 UTC 2007
Author: pag
Date: 2007-10-16 16:16:10 -0500 (Tue, 16 Oct 2007)
New Revision: 481
Modified:
branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java
branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java
Log:
Handling seeded sessions. Also checking that sessions are non-null before using them.
Modified: branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java
===================================================================
--- branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java 2007-10-16 05:13:10 UTC (rev 480)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java 2007-10-16 21:16:10 UTC (rev 481)
@@ -60,6 +60,7 @@
import org.mulgara.query.operation.DataTx;
import org.mulgara.query.operation.Load;
import org.mulgara.query.operation.Restore;
+import org.mulgara.query.operation.SetAutoCommit;
import org.mulgara.query.rdf.LiteralImpl;
import org.mulgara.query.rdf.Mulgara;
import org.mulgara.server.Session;
@@ -131,6 +132,7 @@
log.info("Created an ItqlInterpreterBean with a supplied session and security domain");
}
legacySession = session;
+ interpreter.preSeedSession(session);
interpreter.setSecurityDomain(securityDomain);
}
@@ -243,9 +245,10 @@
/**
* Returns the session to use to communicate with the Mulgara server.
+ * If you need a session, use a {@link ConnectionFactory} instead.
* @deprecated The user should not be accessing a session through a
* command interpreter.
- * @return the session to use to communicate with the Mulgara server
+ * @return the session to use to communicate with the Mulgara server. This is probably <code>null</code>.
*/
public Session getSession() {
return legacySession;
@@ -295,43 +298,55 @@
*/
public void beginTransaction(String name) throws QueryException {
- if (log.isDebugEnabled()) {
+ if (log.isDebugEnabled()) log.debug("Begin transaction for :" + name);
- log.debug("Begin transaction for :" + name);
+ if (legacySession != null) legacySession.setAutoCommit(false);
+ SetAutoCommit autocommitCmd = new SetAutoCommit(false);
+ try {
+ // do what the autointerpreter does, but don't worry about the result
+ Connection localConnection = interpreter.getLocalConnection();
+ autocommitCmd.execute(localConnection);
+ interpreter.updateOnConnectionState(localConnection);
+ } catch (QueryException qe) {
+ throw qe;
+ } catch (Exception e) {
+ throw new QueryException("Unable to start a transaction", e);
}
-
- this.getSession().setAutoCommit(false);
}
/**
* Commit a new transaction by setting the autocommit on
*
* @param name the name of the transaction ( debug purposes only ) *
- * @throws QueryException EXCEPTION TO DO
+ * @throws QueryException Unable to commit one of the connections.
*/
+ @SuppressWarnings("deprecation")
public void commit(String name) throws QueryException {
- if (log.isDebugEnabled()) {
+ if (log.isDebugEnabled()) log.debug("Commit transaction for :" + name);
- log.debug("Commit transaction for :" + name);
- }
-
// this is the same as a commit call
- this.getSession().setAutoCommit(true);
+ if (legacySession != null) legacySession.setAutoCommit(true);
+ interpreter.commitAll();
}
+
/**
* Rollback an existing transaction
*
* @param name the name of the transaction ( debug purposes only ) *
- * @throws QueryException EXCEPTION TO DO
+ * @throws QueryException Unable to rollback one of the connections
*/
+ @SuppressWarnings("deprecation")
public void rollback(String name) throws QueryException {
log.warn("Rollback transaction for :" + name);
- this.getSession().rollback();
- this.getSession().setAutoCommit(true);
+ if (legacySession != null) {
+ legacySession.rollback();
+ legacySession.setAutoCommit(true);
+ }
+ interpreter.rollbackAll();
}
/**
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-10-16 05:13:10 UTC (rev 480)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlAutoInterpreter.java 2007-10-16 21:16:10 UTC (rev 481)
@@ -13,6 +13,7 @@
import java.net.URI;
import java.util.Collection;
+import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
@@ -26,6 +27,7 @@
import org.mulgara.query.QueryException;
import org.mulgara.query.operation.Command;
import org.mulgara.query.operation.LocalCommand;
+import org.mulgara.server.Session;
/**
* This class interprets TQL statements, and automatically executes them,
@@ -141,7 +143,7 @@
* @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 {
+ void updateOnConnectionState(Connection conn) throws QueryException {
// check if the transaction state changed.
if (inTransaction == conn.getAutoCommit()) {
// update the client state
@@ -210,8 +212,8 @@
*/
Connection establishConnection(URI serverUri) throws ConnectionException, QueryException {
Connection connection = (serverUri == null) ? localStateConnection : connectionFactory.newConnection(serverUri);
- // If in a transaction, turn off autocommit
- if (inTransaction && connection.getAutoCommit()) {
+ // If in a transaction, turn off autocommit - ignore the dummy connection
+ if (inTransaction && connection.getAutoCommit() && connection != localStateConnection) {
connection.setAutoCommit(false);
assert !transConnections.contains(connection);
transConnections.add(connection);
@@ -249,4 +251,82 @@
lastException = null;
}
+
+ /**
+ * Returns the internal local connection. Supports local operations for the current package.
+ * @return The local "state" connection.
+ */
+ Connection getLocalConnection() {
+ return localStateConnection;
+ }
+
+
+ /**
+ * Commits 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 committed.
+ */
+ void commitAll() throws QueryException {
+ handleEndOfTx(TxEndOp.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);
+ }
+
+ /**
+ * Seeds the cache with a connection wrapping the given session.
+ * @deprecated Only for use by {@link ItqlInterpreterBean}.
+ * @param session The session to seed into the connection cache.
+ */
+ void preSeedSession(Session session) {
+ // get back a new connection, and then drop it, since it will now be cached
+ try {
+ connectionFactory.newConnection(session);
+ } catch (ConnectionException e) {
+ logger.warn("Unable to use the given session for establishing a connection", e);
+ }
+ }
+
+ /**
+ * 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.
+ * @param op The operation to end the transaction.
+ * @throws QueryException The operation could not be successfully performed.
+ */
+ private void handleEndOfTx(TxEndOp op) throws QueryException {
+ if (inTransaction) {
+ // Commit 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 connection
+ c.remove();
+ }
+ // will only get here once all connections were successfully processed.
+ inTransaction = false;
+ }
+ }
+
}
More information about the Mulgara-svn
mailing list