Author: tv
Date: Mon Aug 27 08:57:56 2018
New Revision: 1839284

URL: http://svn.apache.org/viewvc?rev=1839284&view=rev
Log:
Implement and document TORQUE-350: Make use of try-with-resources possible with 
Torque transactions. 
IMPORTANT: TorqueException now inherits from SQLException

Added:
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnection.java
   (with props)
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnectionImpl.java
   (with props)
    
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/TorqueConnectionImplTest.java
   (with props)
Modified:
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueException.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/AbstractPeerImpl.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Transaction.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManager.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManagerImpl.java
    
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
    
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/orm-reference/connections-transactions.xml
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/saveMethods.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoin.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoinAllExcept.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm
    
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/sql/SkipSqlTest.java

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueException.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueException.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueException.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueException.java
 Mon Aug 27 08:57:56 2018
@@ -1,5 +1,7 @@
 package org.apache.torque;
 
+import java.sql.SQLException;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -26,7 +28,7 @@ package org.apache.torque;
  * @author <a href="mailto:j...@apache.org";>Jason van Zyl</a>
  * @version $Id$
  */
-public class TorqueException extends Exception
+public class TorqueException extends SQLException
 {
     /**
      * Serial version.

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
 Mon Aug 27 08:57:56 2018
@@ -37,6 +37,7 @@ import org.apache.commons.logging.LogFac
 import org.apache.torque.Database;
 import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
+import org.apache.torque.util.TorqueConnection;
 import org.apache.torque.util.Transaction;
 
 /**
@@ -224,13 +225,10 @@ public class IDBroker implements Runnabl
         // Check for Transaction support.  Give warning message if
         // IDBroker is being used with a database that does not
         // support transactions.
-        Connection dbCon = null;
-        try
+        try (TorqueConnection dbCon = Transaction.begin(databaseName))
         {
-            dbCon = Transaction.begin(databaseName);
             transactionsSupported = dbCon.getMetaData().supportsTransactions();
             Transaction.commit(dbCon);
-            dbCon = null;
         }
         catch (Exception e)
         {
@@ -240,13 +238,6 @@ public class IDBroker implements Runnabl
                     e);
             transactionsSupported = false;
         }
-        finally
-        {
-            if (dbCon != null)
-            {
-                Transaction.safeRollback(dbCon);
-            }
-        }
         if (!transactionsSupported)
         {
             log.warn("IDBroker is being used with db '" + databaseName
@@ -497,23 +488,13 @@ public class IDBroker implements Runnabl
             .toString();
 
         boolean exists = false;
-        Connection dbCon = null;
-        try
+        try (TorqueConnection dbCon = Transaction.begin(databaseName))
         {
-            dbCon = Transaction.begin(databaseName);
             Statement statement = dbCon.createStatement();
             ResultSet rs = statement.executeQuery(query);
             exists = rs.next();
             statement.close();
             Transaction.commit(dbCon);
-            dbCon = null;
-        }
-        finally
-        {
-            if (dbCon != null)
-            {
-                Transaction.safeRollback(dbCon);
-            }
         }
         return exists;
     }

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/AbstractPeerImpl.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/AbstractPeerImpl.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/AbstractPeerImpl.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/AbstractPeerImpl.java
 Mon Aug 27 08:57:56 2018
@@ -260,22 +260,12 @@ public abstract class AbstractPeerImpl<T
      */
     public int doDelete(ObjectKey pk) throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             int deletedRows = doDelete(pk, connection);
             Transaction.commit(connection);
-            connection = null;
             return deletedRows;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
 Mon Aug 27 08:57:56 2018
@@ -210,23 +210,13 @@ public class BasePeerImpl<T> implements
      public int doDelete(final Criteria criteria)
              throws TorqueException
      {
-        Connection connection = null;
-        try
+        setDbName(criteria);
+        try (TorqueConnection connection = 
Transaction.begin(criteria.getDbName()))
         {
-            setDbName(criteria);
-            connection = Transaction.begin(criteria.getDbName());
             int deletedRows = doDelete(criteria, connection);
             Transaction.commit(connection);
-            connection = null;
             return deletedRows;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
      }
 
     /**
@@ -333,22 +323,12 @@ public class BasePeerImpl<T> implements
         {
             databaseNameFromInsertValues = getDatabaseName();
         }
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(databaseNameFromInsertValues))
         {
-            connection = Transaction.begin(databaseNameFromInsertValues);
             ObjectKey id = doInsert(insertValues, connection);
             Transaction.commit(connection);
-            connection = null;
             return id;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -556,23 +536,13 @@ public class BasePeerImpl<T> implements
         {
             dbNameToUse = getDatabaseName();
         }
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = Transaction.begin(dbNameToUse))
         {
-            connection = Transaction.begin(dbNameToUse);
             int numberOfInsertedRows
                     = doInsert(toInsertInto, criteria, dbNameToUse, 
connection);
             Transaction.commit(connection);
-            connection = null;
             return numberOfInsertedRows;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -930,27 +900,16 @@ public class BasePeerImpl<T> implements
                 final RecordMapper<TT> mapper)
             throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(criteria.getDbName()))
         {
-            connection = Transaction.begin(criteria.getDbName());
-
             List<TT> result = doSelect(
                     criteria,
                     mapper,
                     connection);
 
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -1011,30 +970,19 @@ public class BasePeerImpl<T> implements
                 final String dbName)
             throws TorqueException
     {
-        Connection connection = null;
-
-        try
+        try (TorqueConnection connection = Transaction.begin(
+                (dbName == null)
+                ? Torque.getDefaultDB()
+                : dbName))
         {
-            connection = Transaction.begin((dbName == null)
-                    ? Torque.getDefaultDB()
-                    : dbName);
-
             List<TT> result = doSelect(
                     query,
                     mapper,
                     connection);
 
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -1314,22 +1262,12 @@ public class BasePeerImpl<T> implements
         {
             databaseNameFromUpdateValues = getDatabaseName();
         }
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(databaseNameFromUpdateValues))
         {
-            connection = Transaction.begin(databaseNameFromUpdateValues);
             int result = doUpdate(updateValues, connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -1401,22 +1339,12 @@ public class BasePeerImpl<T> implements
         {
             databaseNameFromUpdateValues = getDatabaseName();
         }
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(databaseNameFromUpdateValues))
         {
-            connection = Transaction.begin(databaseNameFromUpdateValues);
             int result = doUpdate(selectCriteria, updateValues, connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -1568,25 +1496,15 @@ public class BasePeerImpl<T> implements
             final List<JdbcTypedValue> replacementValues)
         throws TorqueException
     {
-        Connection con = null;
-        try
+        try (TorqueConnection con = Transaction.begin(dbName))
         {
-            con = Transaction.begin(dbName);
             int rowCount = executeStatement(
                     statementString,
                     con,
                     replacementValues);
             Transaction.commit(con);
-            con = null;
             return rowCount;
         }
-        finally
-        {
-            if (con != null)
-            {
-                Transaction.safeRollback(con);
-            }
-        }
     }
 
     /**
@@ -1699,25 +1617,15 @@ public class BasePeerImpl<T> implements
                 final Map<String, JdbcTypedValue> replacementValues)
             throws TorqueException
         {
-            Connection con = null;
-            try
+            try (TorqueConnection con = Transaction.begin(dbName))
             {
-                con = Transaction.begin(dbName);
                 int rowCount = executeStatement(
                         statementString,
                         con,
                         replacementValues);
                 Transaction.commit(con);
-                con = null;
                 return rowCount;
             }
-            finally
-            {
-                if (con != null)
-                {
-                    Transaction.safeRollback(con);
-                }
-            }
         }
 
         /**

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
 Mon Aug 27 08:57:56 2018
@@ -22,7 +22,6 @@ package org.apache.torque.util;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.Serializable;
-import java.sql.Connection;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
@@ -524,9 +523,7 @@ public class LargeSelect<T> implements R
     public void run()
     {
         /* The connection to the database. */
-        Connection conn = null;
-
-        try
+        try (TorqueConnection conn = Transaction.begin(criteria.getDbName()))
         {
             results = new CopyOnWriteArrayList<T>();
 
@@ -555,9 +552,6 @@ public class LargeSelect<T> implements R
                 log.debug("run(): blockEnd = " + blockEnd);
             }
 
-            // Get a connection to the db.
-            conn = Transaction.begin(criteria.getDbName());
-
             // Continue getting rows one page at a time until the memory limit
             // is reached, all results have been retrieved, or the rest
             // of the results have been determined to be irrelevant.
@@ -627,7 +621,6 @@ public class LargeSelect<T> implements R
             }
 
             Transaction.commit(conn);
-            conn = null;
 
             if (log.isDebugEnabled())
             {
@@ -649,10 +642,6 @@ public class LargeSelect<T> implements R
         }
         finally
         {
-            if (conn != null)
-            {
-                Transaction.safeRollback(conn);
-            }
             threadRunning = false;
 
             // Make sure getResults() finally returns if we die.

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java
 Mon Aug 27 08:57:56 2018
@@ -139,22 +139,12 @@ public class SummaryHelper
                 final List<Class<?>> resultTypes)
             throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = Transaction.begin(crit.getDbName()))
         {
-            connection = Transaction.begin(crit.getDbName());
             List<ListOrderedMapCI<Object>> result = summarize(crit, 
resultTypes, connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**

Added: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnection.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnection.java?rev=1839284&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnection.java
 (added)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnection.java
 Mon Aug 27 08:57:56 2018
@@ -0,0 +1,35 @@
+package org.apache.torque.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.sql.Connection;
+
+import org.apache.torque.TorqueException;
+
+/**
+ * Torque's extension to the JDBC connection
+ *
+ * @author <a href="mailto:t...@apache.org";>Thomas Vandahl</a>
+ */
+public interface TorqueConnection extends Connection
+{
+    @Override
+    void close() throws TorqueException;
+}

Propchange: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnection.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnectionImpl.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnectionImpl.java?rev=1839284&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnectionImpl.java
 (added)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnectionImpl.java
 Mon Aug 27 08:57:56 2018
@@ -0,0 +1,548 @@
+package org.apache.torque.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.CallableStatement;
+import java.sql.Clob;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.NClob;
+import java.sql.PreparedStatement;
+import java.sql.SQLClientInfoException;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+import java.sql.SQLXML;
+import java.sql.Savepoint;
+import java.sql.Statement;
+import java.sql.Struct;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.Executor;
+
+import org.apache.torque.TorqueException;
+
+/**
+ * Encapsulates connection status tracking and allows AutoClosing within 
Torque.
+ *
+ * @author <a href="mailto:t...@apache.org";>Thomas Vandahl</a>
+ */
+public class TorqueConnectionImpl implements TorqueConnection
+{
+       /** Has this transaction already been committed? */
+       private boolean committed;
+
+    /** Has this transaction already been rolled back? */
+    private boolean rolledBack;
+
+       /** The wrapped connection instance */
+       private Connection connection;
+
+       /**
+     * Constructor
+     *
+        */
+       public TorqueConnectionImpl(Connection con)
+       {
+               this.committed = false;
+        this.rolledBack = false;
+               this.connection = con;
+       }
+
+       /**
+        * Has commit() been called successfully on this connection?
+        * (for testing)
+        *
+        * @return true, if commit() has been called.
+        */
+       public boolean isCommitted()
+       {
+               return committed;
+       }
+
+    /**
+     * Has rollback() been called successfully on this connection?
+     * (for testing)
+     *
+     * @return true, if rollback() has been called.
+     */
+    public boolean isRolledBack()
+    {
+        return rolledBack;
+    }
+
+       /**
+     * @see java.sql.Wrapper#unwrap(java.lang.Class)
+     */
+    public <T> T unwrap(Class<T> iface) throws SQLException
+    {
+        return connection.unwrap(iface);
+    }
+
+    /**
+     * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
+     */
+    public boolean isWrapperFor(Class<?> iface) throws SQLException
+    {
+        return connection.isWrapperFor(iface);
+    }
+
+    /**
+     * @see java.sql.Connection#createStatement()
+     */
+    public Statement createStatement() throws SQLException
+    {
+        return connection.createStatement();
+    }
+
+    /**
+     * @see java.sql.Connection#prepareStatement(java.lang.String)
+     */
+    public PreparedStatement prepareStatement(String sql) throws SQLException
+    {
+        return connection.prepareStatement(sql);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareCall(java.lang.String)
+     */
+    public CallableStatement prepareCall(String sql) throws SQLException
+    {
+        return connection.prepareCall(sql);
+    }
+
+    /**
+     * @see java.sql.Connection#nativeSQL(java.lang.String)
+     */
+    public String nativeSQL(String sql) throws SQLException
+    {
+        return connection.nativeSQL(sql);
+    }
+
+    /**
+     * @see java.sql.Connection#setAutoCommit(boolean)
+     */
+    public void setAutoCommit(boolean autoCommit) throws SQLException
+    {
+        connection.setAutoCommit(autoCommit);
+    }
+
+    /**
+     * @see java.sql.Connection#getAutoCommit()
+     */
+    public boolean getAutoCommit() throws SQLException
+    {
+        return connection.getAutoCommit();
+    }
+
+    /**
+     * @see java.sql.Connection#commit()
+     */
+    public void commit() throws SQLException
+    {
+        connection.commit();
+        this.committed = true;
+    }
+
+    /**
+     * @see java.sql.Connection#rollback()
+     */
+    public void rollback() throws SQLException
+    {
+        connection.rollback();
+        this.rolledBack = true;
+    }
+
+    /**
+     * @see java.sql.Connection#close()
+     */
+    public void close() throws TorqueException
+    {
+        try
+        {
+            if (this.committed == false &&
+                this.rolledBack == false &&
+                connection.getAutoCommit() == false)
+            {
+                // calls close on the connection
+                Transaction.safeRollback(connection);
+                this.rolledBack = true;
+            }
+
+            if (!connection.isClosed())
+            {
+                connection.close();
+            }
+        }
+        catch (SQLException e)
+        {
+            throw new TorqueException(e);
+        }
+    }
+
+    /**
+     * @see java.sql.Connection#isClosed()
+     */
+    public boolean isClosed() throws SQLException
+    {
+        return connection.isClosed();
+    }
+
+    /**
+     * @see java.sql.Connection#getMetaData()
+     */
+    public DatabaseMetaData getMetaData() throws SQLException
+    {
+        return connection.getMetaData();
+    }
+
+    /**
+     * @see java.sql.Connection#setReadOnly(boolean)
+     */
+    public void setReadOnly(boolean readOnly) throws SQLException
+    {
+        connection.setReadOnly(readOnly);
+    }
+
+    /**
+     * @see java.sql.Connection#isReadOnly()
+     */
+    public boolean isReadOnly() throws SQLException
+    {
+        return connection.isReadOnly();
+    }
+
+    /**
+     * @see java.sql.Connection#setCatalog(java.lang.String)
+     */
+    public void setCatalog(String catalog) throws SQLException
+    {
+        connection.setCatalog(catalog);
+    }
+
+    /**
+     * @see java.sql.Connection#getCatalog()
+     */
+    public String getCatalog() throws SQLException
+    {
+        return connection.getCatalog();
+    }
+
+    /**
+     * @see java.sql.Connection#setTransactionIsolation(int)
+     */
+    public void setTransactionIsolation(int level) throws SQLException
+    {
+        connection.setTransactionIsolation(level);
+    }
+
+    /**
+     * @see java.sql.Connection#getTransactionIsolation()
+     */
+    public int getTransactionIsolation() throws SQLException
+    {
+        return connection.getTransactionIsolation();
+    }
+
+    /**
+     * @see java.sql.Connection#getWarnings()
+     */
+    public SQLWarning getWarnings() throws SQLException
+    {
+        return connection.getWarnings();
+    }
+
+    /**
+     * @see java.sql.Connection#clearWarnings()
+     */
+    public void clearWarnings() throws SQLException
+    {
+        connection.clearWarnings();
+    }
+
+    /**
+     * @see java.sql.Connection#createStatement(int, int)
+     */
+    public Statement createStatement(int resultSetType, int 
resultSetConcurrency) throws SQLException
+    {
+        return connection.createStatement(resultSetType, resultSetConcurrency);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
+     */
+    public PreparedStatement prepareStatement(String sql, int resultSetType, 
int resultSetConcurrency)
+            throws SQLException
+    {
+        return connection.prepareStatement(sql, resultSetType, 
resultSetConcurrency);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
+     */
+    public CallableStatement prepareCall(String sql, int resultSetType, int 
resultSetConcurrency) throws SQLException
+    {
+        return connection.prepareCall(sql, resultSetType, 
resultSetConcurrency);
+    }
+
+    /**
+     * @see java.sql.Connection#getTypeMap()
+     */
+    public Map<String, Class<?>> getTypeMap() throws SQLException
+    {
+        return connection.getTypeMap();
+    }
+
+    /**
+     * @see java.sql.Connection#setTypeMap(java.util.Map)
+     */
+    public void setTypeMap(Map<String, Class<?>> map) throws SQLException
+    {
+        connection.setTypeMap(map);
+    }
+
+    /**
+     * @see java.sql.Connection#setHoldability(int)
+     */
+    public void setHoldability(int holdability) throws SQLException
+    {
+        connection.setHoldability(holdability);
+    }
+
+    /**
+     * @see java.sql.Connection#getHoldability()
+     */
+    public int getHoldability() throws SQLException
+    {
+        return connection.getHoldability();
+    }
+
+    /**
+     * @see java.sql.Connection#setSavepoint()
+     */
+    public Savepoint setSavepoint() throws SQLException
+    {
+        return connection.setSavepoint();
+    }
+
+    /**
+     * @see java.sql.Connection#setSavepoint(java.lang.String)
+     */
+    public Savepoint setSavepoint(String name) throws SQLException
+    {
+        return connection.setSavepoint(name);
+    }
+
+    /**
+     * @see java.sql.Connection#rollback(java.sql.Savepoint)
+     */
+    public void rollback(Savepoint savepoint) throws SQLException
+    {
+        connection.rollback(savepoint);
+    }
+
+    /**
+     * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
+     */
+    public void releaseSavepoint(Savepoint savepoint) throws SQLException
+    {
+        connection.releaseSavepoint(savepoint);
+    }
+
+    /**
+     * @see java.sql.Connection#createStatement(int, int, int)
+     */
+    public Statement createStatement(int resultSetType, int 
resultSetConcurrency, int resultSetHoldability)
+            throws SQLException
+    {
+        return connection.createStatement(resultSetType, resultSetConcurrency, 
resultSetHoldability);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, 
int)
+     */
+    public PreparedStatement prepareStatement(String sql, int resultSetType, 
int resultSetConcurrency,
+            int resultSetHoldability) throws SQLException
+    {
+        return connection.prepareStatement(sql, resultSetType, 
resultSetConcurrency, resultSetHoldability);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
+     */
+    public CallableStatement prepareCall(String sql, int resultSetType, int 
resultSetConcurrency,
+            int resultSetHoldability) throws SQLException
+    {
+        return connection.prepareCall(sql, resultSetType, 
resultSetConcurrency, resultSetHoldability);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareStatement(java.lang.String, int)
+     */
+    public PreparedStatement prepareStatement(String sql, int 
autoGeneratedKeys) throws SQLException
+    {
+        return connection.prepareStatement(sql, autoGeneratedKeys);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
+     */
+    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) 
throws SQLException
+    {
+        return connection.prepareStatement(sql, columnIndexes);
+    }
+
+    /**
+     * @see java.sql.Connection#prepareStatement(java.lang.String, 
java.lang.String[])
+     */
+    public PreparedStatement prepareStatement(String sql, String[] 
columnNames) throws SQLException
+    {
+        return connection.prepareStatement(sql, columnNames);
+    }
+
+    /**
+     * @see java.sql.Connection#createClob()
+     */
+    public Clob createClob() throws SQLException
+    {
+        return connection.createClob();
+    }
+
+    /**
+     * @see java.sql.Connection#createBlob()
+     */
+    public Blob createBlob() throws SQLException
+    {
+        return connection.createBlob();
+    }
+
+    /**
+     * @see java.sql.Connection#createNClob()
+     */
+    public NClob createNClob() throws SQLException
+    {
+        return connection.createNClob();
+    }
+
+    /**
+     * @see java.sql.Connection#createSQLXML()
+     */
+    public SQLXML createSQLXML() throws SQLException
+    {
+        return connection.createSQLXML();
+    }
+
+    /**
+     * @see java.sql.Connection#isValid(int)
+     */
+    public boolean isValid(int timeout) throws SQLException
+    {
+        return connection.isValid(timeout);
+    }
+
+    /**
+     * @see java.sql.Connection#setClientInfo(java.lang.String, 
java.lang.String)
+     */
+    public void setClientInfo(String name, String value) throws 
SQLClientInfoException
+    {
+        connection.setClientInfo(name, value);
+    }
+
+    /**
+     * @see java.sql.Connection#setClientInfo(java.util.Properties)
+     */
+    public void setClientInfo(Properties properties) throws 
SQLClientInfoException
+    {
+        connection.setClientInfo(properties);
+    }
+
+    /**
+     * @see java.sql.Connection#getClientInfo(java.lang.String)
+     */
+    public String getClientInfo(String name) throws SQLException
+    {
+        return connection.getClientInfo(name);
+    }
+
+    /**
+     * @see java.sql.Connection#getClientInfo()
+     */
+    public Properties getClientInfo() throws SQLException
+    {
+        return connection.getClientInfo();
+    }
+
+    /**
+     * @see java.sql.Connection#createArrayOf(java.lang.String, 
java.lang.Object[])
+     */
+    public Array createArrayOf(String typeName, Object[] elements) throws 
SQLException
+    {
+        return connection.createArrayOf(typeName, elements);
+    }
+
+    /**
+     * @see java.sql.Connection#createStruct(java.lang.String, 
java.lang.Object[])
+     */
+    public Struct createStruct(String typeName, Object[] attributes) throws 
SQLException
+    {
+        return connection.createStruct(typeName, attributes);
+    }
+
+    /**
+     * @see java.sql.Connection#setSchema(java.lang.String)
+     */
+    public void setSchema(String schema) throws SQLException
+    {
+        connection.setSchema(schema);
+    }
+
+    /**
+     * @see java.sql.Connection#getSchema()
+     */
+    public String getSchema() throws SQLException
+    {
+        return connection.getSchema();
+    }
+
+    /**
+     * @see java.sql.Connection#abort(java.util.concurrent.Executor)
+     */
+    public void abort(Executor executor) throws SQLException
+    {
+        connection.abort(executor);
+    }
+
+    /**
+     * @see 
java.sql.Connection#setNetworkTimeout(java.util.concurrent.Executor, int)
+     */
+    public void setNetworkTimeout(Executor executor, int milliseconds) throws 
SQLException
+    {
+        connection.setNetworkTimeout(executor, milliseconds);
+    }
+
+    /**
+     * @see java.sql.Connection#getNetworkTimeout()
+     */
+    public int getNetworkTimeout() throws SQLException
+    {
+        return connection.getNetworkTimeout();
+    }
+}

Propchange: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TorqueConnectionImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Transaction.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Transaction.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Transaction.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Transaction.java
 Mon Aug 27 08:57:56 2018
@@ -81,7 +81,7 @@ public final class Transaction
      * @throws TorqueException Any exceptions caught during processing will be
      *         rethrown wrapped into a TorqueException.
      */
-    public static Connection begin() throws TorqueException
+    public static TorqueConnection begin() throws TorqueException
     {
         return transactionManager.begin();
     }
@@ -99,7 +99,7 @@ public final class Transaction
      *
      * @throws TorqueException If the connection cannot be retrieved.
      */
-    public static Connection begin(final String dbName) throws TorqueException
+    public static TorqueConnection begin(final String dbName) throws 
TorqueException
     {
         return transactionManager.begin(dbName);
     }

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManager.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManager.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManager.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManager.java
 Mon Aug 27 08:57:56 2018
@@ -41,7 +41,7 @@ public interface TransactionManager
      * @throws TorqueException Any exceptions caught during processing will be
      *         rethrown wrapped into a TorqueException.
      */
-    Connection begin() throws TorqueException;
+    TorqueConnection begin() throws TorqueException;
 
     /**
      * Begin a transaction by retrieving a connection from the named database
@@ -56,7 +56,7 @@ public interface TransactionManager
      *
      * @throws TorqueException If the connection cannot be retrieved.
      */
-    Connection begin(String dbName) throws TorqueException;
+    TorqueConnection begin(String dbName) throws TorqueException;
 
 
     /**

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManagerImpl.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManagerImpl.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManagerImpl.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/TransactionManagerImpl.java
 Mon Aug 27 08:57:56 2018
@@ -52,7 +52,7 @@ public class TransactionManagerImpl impl
      * @throws TorqueException Any exceptions caught during processing will be
      *         rethrown wrapped into a TorqueException.
      */
-    public Connection begin() throws TorqueException
+    public TorqueConnection begin() throws TorqueException
     {
         return begin(Torque.getDefaultDB());
     }
@@ -70,9 +70,9 @@ public class TransactionManagerImpl impl
      *
      * @throws TorqueException If the connection cannot be retrieved.
      */
-    public Connection begin(String dbName) throws TorqueException
+    public TorqueConnection begin(String dbName) throws TorqueException
     {
-        Connection con = Torque.getConnection(dbName);
+        TorqueConnection con = new 
TorqueConnectionImpl(Torque.getConnection(dbName));
         return con;
     }
 

Modified: 
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
 Mon Aug 27 08:57:56 2018
@@ -26,7 +26,6 @@ import static org.mockito.Mockito.verify
 import static org.mockito.Mockito.when;
 
 import java.math.BigDecimal;
-import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -70,7 +69,7 @@ public class BasePeerImplTest extends Ba
 
     /** Mock db connection. */
     @Mock
-    private Connection connection;
+    private TorqueConnection connection;
 
     /** Mock prepared statement. */
     @Mock
@@ -104,6 +103,7 @@ public class BasePeerImplTest extends Ba
         when(preparedStatement.executeQuery()).thenReturn(resultSet);
         when(preparedStatement.executeQuery((String) 
any())).thenReturn(resultSet);
         when(preparedStatement.executeUpdate()).thenReturn(1);
+
         
when(transactionManager.begin(databaseMap.getName())).thenReturn(connection);
         
when(transactionManager.begin(basePeerImpl.getDatabaseName())).thenReturn(connection);
         when(transactionManager.begin(null)).thenReturn(connection);
@@ -154,6 +154,7 @@ public class BasePeerImplTest extends Ba
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement(
                 "SELECT TABLE.COLUMN1, TABLE.COLUMN4 FROM TABLE LIMIT 1 OFFSET 
2");
+        verify(connection).close();
         verify(preparedStatement).executeQuery();
         verify(preparedStatement).close();
         verify(resultSet, times(2)).next();
@@ -192,6 +193,7 @@ public class BasePeerImplTest extends Ba
 
         // verify mock (verification order not relevant)
         verify(connection).createStatement();
+        verify(connection).close();
         verify(preparedStatement).executeQuery("SELECT * from TABLE");
         verify(preparedStatement).close();
         verify(resultSet, times(2)).next();
@@ -312,6 +314,7 @@ public class BasePeerImplTest extends Ba
         verify(connection).prepareStatement(
                 "DELETE FROM schema.fully_qualified_table "
                 + "WHERE schema.fully_qualified_table.column=?");
+        verify(connection).close();
         verify(preparedStatement).setInt(1, 42);
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).close();
@@ -347,11 +350,13 @@ public class BasePeerImplTest extends Ba
         // verify mock
         verify(connection).prepareStatement(
                 "DELETE FROM TABLE WHERE TABLE.COLUMN1=?");
+        verify(connection).close();
         verify(preparedStatement).setString(1, "abc");
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).close();
         verify(transactionManager).begin("databaseName");
-        verify(transactionManager).safeRollback(connection);
+        // This is tested elsewhere
+        // verify(transactionManager).safeRollback(connection);
         verifyNoMoreInteractions(connection, preparedStatement, resultSet, 
transactionManager);
     }
 
@@ -375,6 +380,7 @@ public class BasePeerImplTest extends Ba
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement(
                 "INSERT INTO TABLE(COLUMN1,COLUMN2,COLUMN3,COLUMN4) VALUES 
(?,someDbFunction,?,?)");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).setObject(
                 1,
@@ -424,6 +430,7 @@ public class BasePeerImplTest extends Ba
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement(
                 "INSERT INTO TABLE(COLUMN2,COLUMN3,COLUMN1,COLUMN4) SELECT 
TABLE.COLUMN1, TABLE.COLUMN2, TABLE.COLUMN3, TABLE.COLUMN4 FROM TABLE WHERE 
TABLE.COLUMN2=TABLE.COLUMN3");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).close();
         verify(transactionManager).begin(databaseMap.getName());
@@ -462,6 +469,7 @@ public class BasePeerImplTest extends Ba
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement(
                 "INSERT INTO TABLE(COLUMN1,COLUMN4) VALUES (?,?)");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).setObject(
                 1,
@@ -473,7 +481,8 @@ public class BasePeerImplTest extends Ba
                 Types.INTEGER);
         verify(preparedStatement).close();
         verify(transactionManager).begin(databaseMap.getName());
-        verify(transactionManager).safeRollback(connection);
+        // This is tested elsewhere
+        // verify(transactionManager).safeRollback(connection);
         verifyNoMoreInteractions(connection, preparedStatement, resultSet, 
transactionManager);
     }
 
@@ -495,6 +504,7 @@ public class BasePeerImplTest extends Ba
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement(
                 "INSERT INTO TABLE(COLUMN1,COLUMN4) VALUES (?,?)");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).setObject(
                 1,
@@ -540,6 +550,7 @@ public class BasePeerImplTest extends Ba
                 + "COLUMN3=?,"
                 + "COLUMN4=? "
                 + "WHERE TABLE.COLUMN4=?");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).setObject(
                 1,
@@ -583,6 +594,7 @@ public class BasePeerImplTest extends Ba
                 + "COLUMN2=someDbFunction,"
                 + "COLUMN3=? "
                 + "WHERE TABLE.COLUMN4=?");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).setObject(
                 1,
@@ -620,6 +632,7 @@ public class BasePeerImplTest extends Ba
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement(
                 "SELECT * from TABLE WHERE Column1=? AND 
COLUMN2=:notExistingKey AND COLUMN3=?");
+        verify(connection).close();
         verify(preparedStatement).setObject(1, 1, 1);
         verify(preparedStatement).setObject(2, "3", 3);
         verify(preparedStatement).executeUpdate();
@@ -644,6 +657,7 @@ public class BasePeerImplTest extends Ba
 
         // verify mock (verification order not relevant)
         verify(connection).prepareStatement("SELECT * from TABLE");
+        verify(connection).close();
         verify(preparedStatement).executeUpdate();
         verify(preparedStatement).close();
         verify(transactionManager).begin("postgresql");

Modified: 
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/orm-reference/connections-transactions.xml
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/orm-reference/connections-transactions.xml?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/orm-reference/connections-transactions.xml
 (original)
+++ 
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/orm-reference/connections-transactions.xml
 Mon Aug 27 08:57:56 2018
@@ -122,6 +122,22 @@ finally
     </p>
 
     <p>
+      As of Torque 4.1 and the advent of the try-with-resources construct, 
+      this can be further simplified. Simply use TorqueConnection instead,
+      which handles the automatic closing and possibly rollback internally:
+    </p>
+
+<source>
+try (TorqueConnection connection = Transaction.begin())
+{
+    // do something with connection, e.g.
+    // someObject.save(connection);
+
+    Transaction.commit(connection);
+}
+</source>
+
+    <p>
       If the database supports transactions and autocommit is turned off,
       all database operations are executed in a single transaction. 
       This has the following effect: For example, you execute two saves in the

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm
 Mon Aug 27 08:57:56 2018
@@ -58,6 +58,7 @@ import org.apache.torque.om.ColumnAccess
 #end##
 import $torqueGen.option("torque.om.criteriaClass");
 #if ($torqueGen.booleanOption("torque.om.addSaveMethods") && 
$saveMethodsInDbObjects == "true")
+import org.apache.torque.util.TorqueConnection;
 import org.apache.torque.util.Transaction;
 #end
 import org.apache.commons.lang.ObjectUtils;

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
 Mon Aug 27 08:57:56 2018
@@ -58,24 +58,13 @@
      */
     ${getterAccessModifier} $fieldType ${getter}(Criteria criteria) throws 
TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(criteria.getDbName()))
         {
-            connection = Transaction.begin(criteria.getDbName());
-
             $fieldType result = ${getter}(criteria, connection);
 
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
 #end

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/saveMethods.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/saveMethods.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/saveMethods.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/saveMethods.vm
 Mon Aug 27 08:57:56 2018
@@ -94,25 +94,17 @@
     public void save(${methodInputParamWithComma}String dbName) 
             throws $saveException
     {
-        Connection con = null;
 #if ($torqueGen.booleanOption("torque.om.complexObjectModel"))
-        try
+        try (TorqueConnection con = Transaction.begin(dbName))
         {
-            con = Transaction.begin(dbName);
             save(${toSaveVariableWithComma}con);
             Transaction.commit(con);
         }
-        catch(TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw e;
-        }
 #else
         if (${toSaveVariableWithDot}isModified())
         {
-            try
+            try (TorqueConnection con = Transaction.begin(dbName))
             {
-                con = Torque.getConnection(dbName);
 #if ($torqueGen.booleanOption("torque.om.trackSaving"))
                 ${toSaveVariableWithDot}setSaving(true);
 #end
@@ -125,14 +117,14 @@
                 {
                     ${peerClassName}.doUpdate($toSaveVariable, con);
                 }
+                Transaction.commit(con);
             }
+#if ($torqueGen.booleanOption("torque.om.trackSaving"))
             finally
             {
-#if ($torqueGen.booleanOption("torque.om.trackSaving"))
                 ${toSaveVariableWithDot}setSaving(false);
-#end
-                Torque.closeConnection(con);
             }
+#end
         }
 #end
     }

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoin.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoin.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoin.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoin.vm
 Mon Aug 27 08:57:56 2018
@@ -55,23 +55,13 @@
     public List<${dbObjectClassName}> ${peerJoinSelectMethod}(Criteria 
criteria)
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             List<${dbObjectClassName}> result
                     = $peerClassName.${peerJoinSelectMethod}(criteria, 
connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoinAllExcept.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoinAllExcept.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoinAllExcept.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelectJoinAllExcept.vm
 Mon Aug 27 08:57:56 2018
@@ -54,23 +54,13 @@
     protected List<${dbObjectClassName}> 
${peerJoinAllExceptSelectMethod}(Criteria criteria)
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             List<${dbObjectClassName}> result
                     = 
$peerClassName.${peerJoinAllExceptSelectMethod}(criteria, connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
 Mon Aug 27 08:57:56 2018
@@ -85,23 +85,13 @@
             int chunkSize) 
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             List<${fieldType}> result 
                     = ${filler}(toFill, chunkSize, connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /** 

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm
 Mon Aug 27 08:57:56 2018
@@ -85,25 +85,15 @@
             int chunkSize)
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             List<${fieldContainedType}> result = ${filler}(
                     toFill,
                     chunkSize,
                     connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /** 

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm
 Mon Aug 27 08:57:56 2018
@@ -70,6 +70,7 @@ import org.apache.torque.om.ObjectKey;
 import org.apache.torque.om.SimpleKey;
 import org.apache.torque.om.ComboKey;
 import org.apache.torque.map.TableMap;
+import org.apache.torque.util.TorqueConnection;
 import org.apache.torque.util.Transaction;
 import org.apache.torque.util.ColumnValues;
 import org.apache.torque.util.JdbcTypedValue;

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
 Mon Aug 27 08:57:56 2018
@@ -86,10 +86,8 @@
     #end
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             $dbObjectClassName result = retrieveByPK(
     #foreach ($columnElement in $primaryKeyColumnElements)
       #set ( $columnField = $columnElement.getAttribute("field") )
@@ -97,16 +95,8 @@
     #end
                connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -156,22 +146,12 @@
     public $dbObjectClassName retrieveByPK(ObjectKey pk)
         throws TorqueException, NoRowsException, TooManyRowsException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             $dbObjectClassName result = retrieveByPK(pk, connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
 Mon Aug 27 08:57:56 2018
@@ -46,22 +46,12 @@
     public List<${dbObjectClassName}> 
retrieveByTypedPKs(Collection<${columnFieldType}> pks)
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             List<${dbObjectClassName}> result = retrieveByTypedPKs(pks, 
connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**
@@ -110,22 +100,12 @@
     public List<${dbObjectClassName}> 
retrieveByObjectKeys(Collection<ObjectKey> pks)
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             List<${dbObjectClassName}> result = retrieveByObjectKeys(pks, 
connection);
             Transaction.commit(connection);
-            connection = null;
             return result;
         }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
-        }
     }
 
     /**

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm
 Mon Aug 27 08:57:56 2018
@@ -73,20 +73,10 @@
             Collection<${foreignDbObjectClassName}> toSave) 
         throws TorqueException
     {
-        Connection connection = null;
-        try
+        try (TorqueConnection connection = 
Transaction.begin(getDatabaseName()))
         {
-            connection = Transaction.begin(getDatabaseName());
             ${setAndSaveMethodName}(toLinkTo, toSave, connection);
             Transaction.commit(connection);
-            connection = null;
-        }
-        finally
-        {
-            if (connection != null)
-            {
-                Transaction.safeRollback(connection);
-            }
         }
     }
 

Modified: 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/sql/SkipSqlTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/sql/SkipSqlTest.java?rev=1839284&r1=1839283&r2=1839284&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/sql/SkipSqlTest.java
 (original)
+++ 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/sql/SkipSqlTest.java
 Mon Aug 27 08:57:56 2018
@@ -53,5 +53,4 @@ public class SkipSqlTest extends BaseDat
             }
         }
     }
-
 }

Added: 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/TorqueConnectionImplTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/TorqueConnectionImplTest.java?rev=1839284&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/TorqueConnectionImplTest.java
 (added)
+++ 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/TorqueConnectionImplTest.java
 Mon Aug 27 08:57:56 2018
@@ -0,0 +1,78 @@
+package org.apache.torque.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.List;
+
+import org.apache.torque.BaseDatabaseTestCase;
+import org.apache.torque.TorqueException;
+import org.apache.torque.criteria.Criteria;
+import org.apache.torque.test.dbobject.Author;
+import org.apache.torque.test.peer.AuthorPeer;
+import org.apache.torque.test.peer.BookPeer;
+
+/**
+ * Test correct behavior if Transaction is used with the try-with-resources 
pattern
+ *
+ * @author <a href="mailto:t...@apache.org";>Thomas Vandahl</a>
+ */
+public class TorqueConnectionImplTest extends BaseDatabaseTestCase
+{
+    public void testTryWithResources() throws TorqueException
+    {
+        // prepare
+        cleanBookstore();
+        insertBookstoreData();
+
+        try(TorqueConnection con = Transaction.begin())
+        {
+                       BookPeer.doSelect(new Criteria(), con);
+                       Transaction.commit(con);
+
+                       @SuppressWarnings("resource")
+            TorqueConnectionImpl conImpl = (TorqueConnectionImpl)con;
+            assertTrue(conImpl.isCommitted());
+               }
+        catch (TorqueException e)
+        {
+                       // expected
+               }
+
+        Author author = new Author();
+        author.setName("AuthorProxy");
+
+        try(TorqueConnection con = Transaction.begin())
+        {
+            author.save(con);
+            throw new TorqueException("Forcing rollback");
+        }
+        catch (TorqueException e)
+        {
+                       // expected
+               }
+
+        Criteria criteria = new Criteria();
+        criteria.where(AuthorPeer.NAME, "AuthorProxy");
+        List<Author> authors = AuthorPeer.doSelect(criteria);
+
+        // Record should not be there
+        assertEquals(0, authors.size());
+    }
+}

Propchange: 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/TorqueConnectionImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org
For additional commands, e-mail: torque-dev-h...@db.apache.org

Reply via email to