Author: kwilliams
Date: Fri Mar 24 09:32:46 2006
New Revision: 388585

URL: http://svn.apache.org/viewcvs?rev=388585&view=rev
Log:
Added "dispose" API to CommandGroup
Fix for Tuscany-125 

Modified:
    
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/CommandGroup.java
    
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/CommandGroupImpl.java

Modified: 
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/CommandGroup.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/CommandGroup.java?rev=388585&r1=388584&r2=388585&view=diff
==============================================================================
--- 
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/CommandGroup.java
 (original)
+++ 
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/CommandGroup.java
 Fri Mar 24 09:32:46 2006
@@ -52,5 +52,12 @@
      * @param connection
      */
     public void setConnection(Connection connection);
+    
+    /**
+     * If the CommandGroup is managing connections then this method
+     * must be called when the client is done with the instance.
+     * 
+     */
+    public void releaseResources();
 
 }

Modified: 
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/CommandGroupImpl.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/CommandGroupImpl.java?rev=388585&r1=388584&r2=388585&view=diff
==============================================================================
--- 
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/CommandGroupImpl.java
 (original)
+++ 
incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/CommandGroupImpl.java
 Fri Mar 24 09:32:46 2006
@@ -113,11 +113,11 @@
 
     private void setConfig(InputStream stream) {
         XMLHelper helper = XMLHelper.INSTANCE;
-  
+
         ConfigPackageImpl impl = ConfigPackageImpl.eINSTANCE;
 
         try {
-            config = (Config) helper.load(stream).getRootObject();           
+            config = (Config) helper.load(stream).getRootObject();
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
@@ -166,7 +166,8 @@
 
     }
 
-    //TODO - Refactor to eliminate common initialization code after connection 
is got
+    // TODO - Refactor to eliminate common initialization code after connection
+    // is got
     private void initViaDataSource(ConnectionProperties cp) {
 
         Connection connection = null;
@@ -178,18 +179,63 @@
             throw new Error(e);
         }
         try {
-            //TODO - I think we should rename this getDataSourceURL?
+            // TODO - I think we should rename this getDataSourceURL?
             DataSource ds = (DataSource) ctx.lookup(cp.getDataSource());
             try {
                 connection = ds.getConnection();
                 connection.setAutoCommit(false);
                 setConnection(connection);
             } catch (SQLException e) {
-                throw new Error (e);
+                throw new Error(e);
             }
         } catch (NamingException e) {
             throw new Error(e);
         }
 
     }
+
+    public void releaseResources() {
+
+        if (managingConnections())
+            closeConnection();
+    }
+
+    private void closeConnection() {
+        if (connection != null)
+            try {
+                connection.close();
+                connection = null;
+            } catch (SQLException e) {
+                throw new RuntimeException(e);
+            }
+    }
+
+    /**
+     * If the config has connection properties then we are "manageing" the
+     * connection via DriverManager or DataSource
+     */
+    private boolean managingConnections() {
+
+        if (config.getConnectionProperties() == null)
+            return false;
+        else
+            return true;
+
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#finalize()
+     * We should clean up on finalize in case the client neglected to do so
+     */
+    @Override
+    protected void finalize() throws Throwable {
+
+        try {
+            releaseResources();
+        } finally {
+            super.finalize();
+        }
+
+    }
+
 }


Reply via email to