[Mulgara-svn] r448 - branches/nw-interface/src/jar/query/java/org/mulgara/connection

pag at mulgara.org pag at mulgara.org
Mon Sep 24 21:00:25 UTC 2007


Author: pag
Date: 2007-09-24 16:00:24 -0500 (Mon, 24 Sep 2007)
New Revision: 448

Modified:
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java
Log:
Added support for managing transactions on connections

Modified: branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java	2007-09-19 21:10:37 UTC (rev 447)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java	2007-09-24 21:00:24 UTC (rev 448)
@@ -43,6 +43,9 @@
   /** The session to use for this connection. */
   private Session session;
 
+  /** Indicates the current autocommit state */
+  private boolean autoCommit = false;
+
   /**
    * Creates a new connection, given a URI to a server.
    * @param serverUri The URI to connect to.
@@ -110,6 +113,26 @@
 
 
   /**
+   * Starts and commits transactions on this connection, by turning the autocommit
+   * flag on and off. 
+   * @param autocommit <code>true</code> if the flag is to be on.
+   * @throws QueryException The session could not change state.
+   */
+  public void setAutoCommit(boolean autoCommit) throws QueryException {
+    this.autoCommit = autoCommit;
+    session.setAutoCommit(autoCommit);
+  }
+
+
+  /**
+   * @return the autoCommit value
+   */
+  public boolean getAutoCommit() {
+    return autoCommit;
+  }
+
+
+  /**
    * Closes the current connection.
    */
   public void close() throws QueryException {

Modified: branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java	2007-09-19 21:10:37 UTC (rev 447)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java	2007-09-24 21:00:24 UTC (rev 448)
@@ -15,6 +15,8 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
+import org.mulgara.query.QueryException;
 import org.mulgara.server.Session;
 
 
@@ -29,18 +31,29 @@
  */
 public class ConnectionFactory {
 
+  /** The logger. */
+  private final static Logger logger = Logger.getLogger(ConnectionFactory.class.getName());
+
   /** Cache of Connections, based on their server URI. */
-  static private Map<URI,Connection> cacheOnUri = new HashMap<URI,Connection>();
+  private Map<URI,Connection> cacheOnUri;
   /** Cache of Connections, based on their session data. */
-  static private Map<Session,Connection> cacheOnSession = new HashMap<Session,Connection>();
+  private Map<Session,Connection> cacheOnSession;
 
   /**
+   * Default constructor.
+   */
+  public ConnectionFactory() {
+    cacheOnUri = new HashMap<URI,Connection>();
+    cacheOnSession = new HashMap<Session,Connection>();
+  }
+
+  /**
    * Retrieve a connection based on a server URI.
    * @param serverUri The URI to get the connection to.
    * @return The new Connection.
    * @throws ConnectionException There was an error getting a connection.
    */
-  static Connection newConnection(URI serverUri) throws ConnectionException {
+  public Connection newConnection(URI serverUri) throws ConnectionException {
     Connection c = cacheOnUri.get(serverUri);
     if (c == null) {
       c = new Connection(serverUri);
@@ -57,7 +70,7 @@
    * @return The new Connection.
    * @throws ConnectionException There was an error getting a connection.
    */
-  static Connection newConnection(Session session) throws ConnectionException {
+  public Connection newConnection(Session session) throws ConnectionException {
     Connection c = cacheOnSession.get(session);
     if (c == null) {
       c = new Connection(session);
@@ -67,4 +80,27 @@
     return c;
   }
 
+
+  /**
+   * Close all connections served by this factory. Exceptions are logged, but not acted on.
+   */
+  public void closeAll() {
+    safeCloseAll(cacheOnUri.values());
+    safeCloseAll(cacheOnSession.values());
+  }
+
+
+  /**
+   * Closes all connections in a collection. Exceptions are logged, but not acted on.
+   * @param connections The connections to close.
+   */
+  private void safeCloseAll(Iterable<Connection> connections) {
+    for (Connection c: connections) {
+      try {
+        c.close();
+      } catch (QueryException qe) {
+        logger.warn("Unable to close connection", qe);
+      }
+    }
+  }
 }




More information about the Mulgara-svn mailing list