This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch version3
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/version3 by this push:
     new 09006fd  EMPIREDB-362 optimization
09006fd is described below

commit 09006fdf5d772e80fad92c6d3c235f07300fa081
Author: Rainer Döbele <[email protected]>
AuthorDate: Thu Jan 20 16:10:58 2022 +0100

    EMPIREDB-362
    optimization
---
 .../org/apache/empire/jsf2/app/WebApplication.java |  23 +-
 .../org/apache/empire/jsf2/app/WebDBContext.java   |   4 +-
 .../jsf2/pageelements/BeanListPageElement.java     |   2 +-
 .../main/java/org/apache/empire/db/DBContext.java  |   2 +-
 .../main/java/org/apache/empire/db/DBUtils.java    | 334 +++++++--------------
 .../apache/empire/db/context/DBContextBase.java    |  34 ++-
 6 files changed, 161 insertions(+), 238 deletions(-)

diff --git 
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebApplication.java 
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebApplication.java
index 2115243..9956457 100644
--- 
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebApplication.java
+++ 
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebApplication.java
@@ -402,13 +402,14 @@ public abstract class WebApplication
      * 
      * @return
      */
-    protected Connection getConnection(DBDatabase db)
+    protected synchronized Connection getConnection(DBDatabase db)
     {
         // Get From Pool
         try
         { // Obtain a connection
             Connection conn = getAppDataSource(db).getConnection();
             conn.setAutoCommit(false);
+            log.trace("Connection {} obtained from pool", conn.hashCode());
             return conn;
         }
         catch (SQLException e)
@@ -421,7 +422,7 @@ public abstract class WebApplication
     /**
      * releases a connection from the connection pool
      */
-    protected void releaseConnection(DBDatabase db, Connection conn, boolean 
commit)
+    protected synchronized void releaseConnection(DBDatabase db, Connection 
conn, boolean commit)
     {
         try
         { // release connection
@@ -429,6 +430,7 @@ public abstract class WebApplication
             {
                 return;
             }
+            log.trace("releasing Connection {}", conn.hashCode());
             // Commit or rollback connection depending on the exit code
             if (commit)
             { // success: commit all changes
@@ -528,11 +530,22 @@ public abstract class WebApplication
         }
     }
 
+    /**
+     * Releases all connections attached to the current request
+     * If an error is detected in the faces message list, a rollback will 
automatically be performed insteamd of a commmit
+     * @param fc the FacesContext
+     */
     public void releaseAllConnections(final FacesContext fc)
     {
         releaseAllConnections(fc, !hasError(fc));
     }
 
+    /**
+     * Releases the connection associated with a database from the request
+     * @param fc the FacesContext
+     * @param db the DBDatabase
+     * @param commit when true changes are committed otherwise they are rolled 
back
+     */
     public void releaseConnection(final FacesContext fc, DBDatabase db, 
boolean commit)
     {
         @SuppressWarnings("unchecked")
@@ -551,6 +564,12 @@ public abstract class WebApplication
         }
     }
 
+    /**
+     * Releases the connection associated with a database from the request
+     * If an error is detected in the faces message list, a rollback will 
automatically be performed insteamd of a commmit
+     * @param fc the FacesContext
+     * @param db the DBDatabase
+     */
     public void releaseConnection(final FacesContext fc, DBDatabase db)
     {
         releaseConnection(fc, db, !hasError(fc));
diff --git 
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebDBContext.java 
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebDBContext.java
index 4e5cdd7..b383ab7 100644
--- a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebDBContext.java
+++ b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/WebDBContext.java
@@ -54,7 +54,7 @@ public class WebDBContext<DB extends DBDatabase> extends 
DBContextBase
      * IMPORTANT: Do not hold the connection!
      */
     @Override
-    public synchronized Connection getConnection()
+    public Connection getConnection()
     {
         if (conn==null)
         {   // get a new connection
@@ -82,7 +82,7 @@ public class WebDBContext<DB extends DBDatabase> extends 
DBContextBase
             log.info("No Connection to rollback changes");
     }
     
-    public synchronized void releaseConnection(boolean commitPerformed)
+    public void releaseConnection(boolean commitPerformed)
     {
         this.conn = null;
         // commit or rollback?
diff --git 
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
 
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
index abe0789..309ea44 100644
--- 
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
+++ 
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
@@ -256,7 +256,7 @@ public class BeanListPageElement<T> extends 
ListPageElement<T> implements ListIt
         { // Negative count means: loadItems should load all items.
             countCmd.clearSelect();
             countCmd.select(rowset.count());
-            int count = 
context.getUtils().querySingleInt(countCmd.getSelect(), 
countCmd.getParamValues(), 0);
+            int count = context.getUtils().querySingleInt(countCmd, 0);
             lti.init(count, pageSize);
         }
         else
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBContext.java 
b/empire-db/src/main/java/org/apache/empire/db/DBContext.java
index d8f660d..d7b86aa 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBContext.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBContext.java
@@ -11,7 +11,7 @@ public interface DBContext
     
     Connection getConnection();
     
-    DBUtils getUtils();
+    <T extends DBUtils> T getUtils();
     
     void commit();
 
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBUtils.java 
b/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
index a946020..912876f 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
@@ -63,15 +63,96 @@ public class DBUtils implements DBContextAware
     {
         return driver.createCommand(db);
     }
-    
-    /*
-    // Sequences
-    public Object getNextSequenceValue(String seqName)
+    /**
+     * Executes an update, insert or delete SQL-Statement.<BR>
+     * We recommend to use a DBCommand object in order to build the sqlCmd.<BR>
+     * <P>
+     * @param sqlCmd the SQL-Command
+     * @param sqlParams a list of objects to replace sql parameters
+     * @param setGenKeys object to set the generated keys for
+     * @return the row count for insert, update or delete or 0 for SQL 
statements that return nothing
+     */
+    public int executeSQL(String sqlCmd, Object[] sqlParams, 
DBDatabaseDriver.DBSetGenKeys setGenKeys)
+    {
+        try 
+        {   // Debug
+            if (log.isInfoEnabled())
+                log.info("Executing: " + sqlCmd);
+            // execute SQL
+            long start = System.currentTimeMillis();
+            int affected = driver.executeSQL(sqlCmd, sqlParams, 
context.getConnection(), setGenKeys);
+            // number of affected records
+            if (affected < 0)
+                throw new UnexpectedReturnValueException(affected, 
"driver.executeSQL()");
+            // Log
+            long execTime = (System.currentTimeMillis() - start);
+            if (log.isInfoEnabled())
+                log.info("executeSQL affected {} Records in {} ms ", affected, 
execTime);
+            else if (execTime>=longRunndingStmtThreshold)
+                log.warn("Long running statement took {} seconds for statement 
{}.", execTime / 1000, sqlCmd);
+            // Return number of affected records
+            return affected;
+            
+        } catch (SQLIntegrityConstraintViolationException sqle) {
+            // ConstraintViolation
+            throw new ConstraintViolationException(driver, sqlCmd, sqle);
+        } catch (SQLException sqle) {
+            // Other error
+            throw new StatementFailedException(driver, sqlCmd, sqle);
+        }    
+    }
+
+    /**
+     * Executes an SQLStatment
+     * @param sqlCmd the SQL-Command
+     * @param sqlParams a list of objects to replace sql parameters
+     */
+    public final int executeSQL(String sqlCmd, Object[] sqlParams)
+    {
+        return executeSQL(sqlCmd, sqlParams, null); 
+    }
+
+    /**
+     * Executes an Insert statement from a command object
+     * @param cmd the command object containing the insert command
+     * @return the number of records that have been inserted with the supplied 
statement
+     */
+    public final int executeInsert(DBCommand cmd)
+    {
+        return executeSQL(cmd.getInsert(), cmd.getParamValues()); 
+    }
+
+    /**
+     * Executes an InsertInfo statement from a command object
+     * @param table the table into which to insert the selected data
+     * @param cmd the command object containing the selection command 
+     * @return the number of records that have been inserted with the supplied 
statement
+     */
+    public final int executeInsertInto(DBTable table, DBCommand cmd)
+    {
+        return executeSQL(cmd.getInsertInto(table), cmd.getParamValues()); 
+    }
+
+    /**
+     * Executes an Update statement from a command object
+     * @param cmd the command object containing the update command
+     * @return the number of records that have been updated with the supplied 
statement
+     */
+    public final int executeUpdate(DBCommand cmd)
+    {
+        return executeSQL(cmd.getUpdate(), cmd.getParamValues()); 
+    }
+
+    /**
+     * Executes a Delete statement from a command object
+     * @param from the database table from which to delete records
+     * @param cmd the command object containing the delete constraints
+     * @return the number of records that have been deleted with the supplied 
statement
+     */
+    public final int executeDelete(DBTable from, DBCommand cmd)
     {
-        // Ask driver
-        return driver.getNextSequenceValue(this, seqName, 1, 
context.getConnection());
+        return executeSQL(cmd.getDelete(from), cmd.getParamValues()); 
     }
-    */
 
     /**
      * Returns the value of the first row/column of a sql-query as an object.
@@ -80,31 +161,23 @@ public class DBUtils implements DBContextAware
      * @param sqlCmd the SQL-Command
      * @param sqlParams list of query parameter values
      * @param dataType the expected data type
-     * @param conn a valid connection to the database.
      * 
      * @return the value of the first column in the first row of the query 
      */
-    public Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType 
dataType)
+    public Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType 
dataType, boolean forceResult)
     {
         // Debug
         long start = System.currentTimeMillis();
         if (log.isDebugEnabled())
             log.debug("Executing: " + sqlCmd);
-        /*
-        // Get the next Value
-        rs = driver.executeQuery(sqlCmd, sqlParams, false, 
context.getConnection());
-        if (rs == null)
-            throw new UnexpectedReturnValueException(rs, 
"driver.executeQuery()");
-        // Check Result
-        if (rs.next() == false)
-        {   // no result
-            log.debug("querySingleValue returned no result");
-            return ObjectUtils.NO_VALUE;
-        }
         // Read value
-        Object result = driver.getResultValue(rs, 1, dataType);
-        */
         Object result = driver.querySingleValue(sqlCmd, sqlParams, dataType, 
context.getConnection());
+        if (result==ObjectUtils.NO_VALUE)
+        {   if (forceResult)
+                throw new QueryNoResultException(sqlCmd);
+            else
+                result = null;
+        }
         // Debug
         long queryTime = (System.currentTimeMillis() - start);
         if (log.isDebugEnabled())
@@ -114,23 +187,19 @@ public class DBUtils implements DBContextAware
         // done
         return result;
     }
-
+    
     /**
      * Returns the value of the first row/column of a sql-query as an object.
      * If the query does not return a result a QueryNoResultException is thrown
      * 
-     * @param sqlCmd the SQL-Command
-     * @param sqlParams list of query parameter values
-     * @param conn a valid connection to the database.
+     * @param cmd the Command object that contains the select statement
+     * @param dataType the expected data type
      * 
      * @return the value of the first column in the first row of the query 
      */
-    public final Object querySingleValue(String sqlCmd, Object[] sqlParams)
+    public final Object querySingleValue(DBCommand cmd, DataType dataType, 
boolean forceResult)
     {
-        Object value = querySingleValue(sqlCmd, sqlParams, DataType.UNKNOWN);
-        if (value==ObjectUtils.NO_VALUE)
-            throw new QueryNoResultException(sqlCmd);
-        return value;
+        return querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
dataType, forceResult);
     }
     
     /**
@@ -138,17 +207,12 @@ public class DBUtils implements DBContextAware
      * If the query does not return a result a QueryNoResultException is thrown
      * 
      * @param cmd the Command object that contains the select statement
-     * @param dataType the expected data type
-     * @param conn a valid connection to the database.
      * 
      * @return the value of the first column in the first row of the query 
      */
-    public final Object querySingleValue(DBCommand cmd, DataType dataType)
+    public final Object querySingleValue(DBCommand cmd, boolean forceResult)
     {
-        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
dataType);
-        if (value==ObjectUtils.NO_VALUE)
-            throw new QueryNoResultException(cmd.getSelect());
-        return value;
+        return querySingleValue(cmd, DataType.UNKNOWN, forceResult);  
     }
     
     /**
@@ -156,30 +220,12 @@ public class DBUtils implements DBContextAware
      * If the query does not return a result a QueryNoResultException is thrown
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * 
      * @return the value of the first column in the first row of the query 
      */
     public final Object querySingleValue(DBCommand cmd)
     {
-        return querySingleValue(cmd, DataType.UNKNOWN);  
-    }
-    
-    /**
-     * Returns the value of the first row/column of a sql-query as an int.
-     * If the query does not return a result or if the query result is NULL, 
then the defaultValue is returned
-     * 
-     * @param sqlCmd the SQL statement
-     * @param sqlParams list of query parameter values
-     * @param defaultValue the default value if no value was returned by the 
database
-     * @param conn a valid connection to the database.
-     *
-     * @return the value of the first column in the first row of the query 
-     */
-    public final int querySingleInt(String sqlCmd, Object[] sqlParams, int 
defaultValue)
-    { 
-        Object value = querySingleValue(sqlCmd, sqlParams, DataType.INTEGER);
-        return ObjectUtils.getInteger(value, defaultValue);
+        return querySingleValue(cmd, DataType.UNKNOWN, true);  
     }
 
     /**
@@ -188,13 +234,13 @@ public class DBUtils implements DBContextAware
      * 
      * @param cmd the Command object that contains the select statement
      * @param defaultValue the default value if no value was returned by the 
database
-     * @param conn a valid connection to the database.
      *
      * @return the result as a int value
      */
     public final int querySingleInt(DBCommand cmd, int defaultValue)
     { 
-        return querySingleInt(cmd.getSelect(), cmd.getParamValues(), 
defaultValue);
+        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.INTEGER, false);
+        return ObjectUtils.getInteger(value, defaultValue);
     }
 
     /**
@@ -202,34 +248,14 @@ public class DBUtils implements DBContextAware
      * If the query does not return a result a QueryNoResultException is thrown
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      *
      * @return the result as a int value
      */
     public final int querySingleInt(DBCommand cmd)
     { 
-        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.INTEGER);
-        if (ObjectUtils.isEmpty(value))
-            throw new QueryNoResultException(cmd.getSelect());
+        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.INTEGER, true);
         return ObjectUtils.getInteger(value);
     }
-
-    /**
-     * Returns the value of the first row/column of a sql-query as a long.
-     * If the query does not return a result or if the query result is NULL, 
then the defaultValue is returned
-     * 
-     * @param sqlCmd the SQL statement
-     * @param sqlParams list of query parameter values
-     * @param defaultValue the default value
-     * @param conn a valid connection to the database.
-     * 
-     * @return the result as a long value
-     */
-    public final long querySingleLong(String sqlCmd, Object[] sqlParams, long 
defaultValue)
-    { 
-        Object value = querySingleValue(sqlCmd, sqlParams, DataType.INTEGER);
-        return ObjectUtils.getLong(value, defaultValue);
-     }
     
     /**
      * Returns the value of the first row/column of a sql-query as a long.
@@ -237,13 +263,13 @@ public class DBUtils implements DBContextAware
      * 
      * @param cmd the Command object that contains the select statement
      * @param defaultValue the default value
-     * @param conn a valid connection to the database.
      * 
      * @return the result as a long value
      */
     public final long querySingleLong(DBCommand cmd, long defaultValue)
     { 
-        return querySingleLong(cmd.getSelect(), cmd.getParamValues(), 
defaultValue);
+        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.INTEGER, false);
+        return ObjectUtils.getLong(value, defaultValue);
     }
 
     /**
@@ -251,15 +277,12 @@ public class DBUtils implements DBContextAware
      * If the query does not return a result a QueryNoResultException is thrown
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      *
      * @return the result as a long value
      */
     public final long querySingleLong(DBCommand cmd)
     { 
-        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.INTEGER);
-        if (ObjectUtils.isEmpty(value))
-            throw new QueryNoResultException(cmd.getSelect());
+        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.INTEGER, true);
         return ObjectUtils.getLong(value);
     }
     
@@ -267,32 +290,15 @@ public class DBUtils implements DBContextAware
      * Returns the value of the first row/column of a sql-query as a string.
      * If the query does not return a result or if the query result is NULL, 
then the defaultValue is returned
      * 
-     * @param sqlCmd the SQL statement
-     * @param sqlParams list of query parameter values
-     * @param defaultValue the default value if no value was returned by the 
database
-     * @param conn a valid connection to the database.
-     *
-     * @return the result as a String object
-     */
-    public final String querySingleString(String sqlCmd, Object[] sqlParams, 
String defaultValue)
-    { 
-        Object value = querySingleValue(sqlCmd, sqlParams, DataType.VARCHAR);
-        return (ObjectUtils.isEmpty(value) ? defaultValue : value.toString());
-    }
-    
-    /**
-     * Returns the value of the first row/column of a sql-query as a string.
-     * If the query does not return a result or if the query result is NULL, 
then the defaultValue is returned
-     * 
      * @param cmd the Command object that contains the select statement
      * @param defaultValue the default value if no value was returned by the 
database
-     * @param conn a valid connection to the database.
      *
      * @return the result as a String object, if no result a empty String
      */
     public final String querySingleString(DBCommand cmd, String defaultValue)
     { 
-        return querySingleString(cmd.getSelect(), cmd.getParamValues(), 
defaultValue);
+        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.VARCHAR, false);
+        return StringUtils.toString(value, defaultValue);
     }
     
     /**
@@ -301,16 +307,13 @@ public class DBUtils implements DBContextAware
      * If the query result is NULL an empty string is returned.
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      *
      * @return the result as a String object, if no result a empty String
      */
     public final String querySingleString(DBCommand cmd)
     { 
-        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.VARCHAR);
-        if (value==ObjectUtils.NO_VALUE)
-            throw new QueryNoResultException(cmd.getSelect());
-        return StringUtils.toString(value, "");
+        Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), 
DataType.VARCHAR, true);
+        return StringUtils.toString(value);
     }
     
     /**
@@ -321,7 +324,6 @@ public class DBUtils implements DBContextAware
      * @param <T> the type for the list
      * @param sqlCmd the SQL statement
      * @param dataType the expected data type
-     * @param conn a valid connection to the database.
      * @param maxRows maximum number of rows or -1 for all rows
      * 
      * @return the number of elements that have been added to the collection 
@@ -373,7 +375,6 @@ public class DBUtils implements DBContextAware
      * @param c the class type for the list 
      * @param <T> the type for the list
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * 
      * @return the number of elements that have been added to the collection 
      */
@@ -389,7 +390,6 @@ public class DBUtils implements DBContextAware
      * @param c the class type for the list 
      * @param <T> the type for the list
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * 
      * @return a list of the values of the first column of an sql query 
      */
@@ -406,7 +406,6 @@ public class DBUtils implements DBContextAware
      * The array is filled with the values of the first column.
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * @return a list of values of type Object 
      */
     public final List<Object> querySimpleList(DBCommand cmd)
@@ -419,7 +418,6 @@ public class DBUtils implements DBContextAware
      * The option list is filled with the values of the first and second 
column.
      * 
      * @param sqlCmd the SQL statement
-     * @param conn a valid connection to the database.
      * @return an Options object containing a set a of values and their 
corresponding names 
      */
     public int queryOptionList(String sqlCmd, Object[] sqlParams, Options 
result)
@@ -467,7 +465,6 @@ public class DBUtils implements DBContextAware
      * The option list is filled with the values of the first and second 
column.
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * @return an Options object containing a set a of values and their 
corresponding names 
      */
     public final int queryOptionList(DBCommand cmd, Options result)
@@ -480,7 +477,6 @@ public class DBUtils implements DBContextAware
      * The option list is filled with the values of the first and second 
column.
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * @return an Options object containing a set a of values and their 
corresponding names 
      */
     public final Options queryOptionList(DBCommand cmd)
@@ -497,7 +493,6 @@ public class DBUtils implements DBContextAware
      * Otherwise a DBReader should be used!</p>
      * 
      * @param sqlCmd the SQL statement
-     * @param conn a valid connection to the database.
      * @return a list of object arrays 
      */
     public int queryObjectList(String sqlCmd, Object[] sqlParams, 
Collection<Object[]> result, int maxRows)
@@ -549,7 +544,6 @@ public class DBUtils implements DBContextAware
      * Otherwise a DBReader should be used!</p>
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * @return a list of object arrays 
      */
     public final int queryObjectList(DBCommand cmd, Collection<Object[]> 
result)
@@ -562,7 +556,6 @@ public class DBUtils implements DBContextAware
      * This function should only be used for small lists.
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * @return a list of object arrays 
      */
     public final List<Object[]> queryObjectList(DBCommand cmd)
@@ -578,7 +571,6 @@ public class DBUtils implements DBContextAware
      * 
      * @param sqlCmd the SQL-Command
      * @param sqlParams list of query parameter values
-     * @param conn a valid connection to the database.
      * 
      * @return the values of the first row 
      */
@@ -596,7 +588,6 @@ public class DBUtils implements DBContextAware
      * If the query does not return a result a QueryNoResultException is thrown
      * 
      * @param cmd the Command object that contains the select statement
-     * @param conn a valid connection to the database.
      * 
      * @return the values of the first row 
      */
@@ -604,103 +595,7 @@ public class DBUtils implements DBContextAware
     {
         return querySingleRow(cmd.getSelect(), cmd.getParamValues()); 
     }
-    
-    /**
-     * Executes an update, insert or delete SQL-Statement.<BR>
-     * We recommend to use a DBCommand object in order to build the sqlCmd.<BR>
-     * <P>
-     * @param sqlCmd the SQL-Command
-     * @param sqlParams a list of objects to replace sql parameters
-     * @param conn a valid connection to the database.
-     * @param setGenKeys object to set the generated keys for
-     * @return the row count for insert, update or delete or 0 for SQL 
statements that return nothing
-     */
-    public int executeSQL(String sqlCmd, Object[] sqlParams, 
DBDatabaseDriver.DBSetGenKeys setGenKeys)
-    {
-        try 
-        {   // Debug
-            if (log.isInfoEnabled())
-                log.info("Executing: " + sqlCmd);
-            // execute SQL
-            long start = System.currentTimeMillis();
-            int affected = driver.executeSQL(sqlCmd, sqlParams, 
context.getConnection(), setGenKeys);
-            // number of affected records
-            if (affected < 0)
-                throw new UnexpectedReturnValueException(affected, 
"driver.executeSQL()");
-            // Log
-            long execTime = (System.currentTimeMillis() - start);
-            if (log.isInfoEnabled())
-                log.info("executeSQL affected {} Records in {} ms ", affected, 
execTime);
-            else if (execTime>=longRunndingStmtThreshold)
-                log.warn("Long running statement took {} seconds for statement 
{}.", execTime / 1000, sqlCmd);
-            // Return number of affected records
-            return affected;
-            
-        } catch (SQLIntegrityConstraintViolationException sqle) {
-            // ConstraintViolation
-            throw new ConstraintViolationException(driver, sqlCmd, sqle);
-        } catch (SQLException sqle) {
-            // Other error
-            throw new StatementFailedException(driver, sqlCmd, sqle);
-        }    
-    }
-
-    /**
-     * Executes an SQLStatment
-     * @param sqlCmd the SQL-Command
-     * @param sqlParams a list of objects to replace sql parameters
-     */
-    public final int executeSQL(String sqlCmd, Object[] sqlParams)
-    {
-        return executeSQL(sqlCmd, sqlParams, null); 
-    }
-
-    /**
-     * Executes an Insert statement from a command object
-     * @param cmd the command object containing the insert command
-     * @param conn a valid connection to the database.
-     * @return the number of records that have been inserted with the supplied 
statement
-     */
-    public final int executeInsert(DBCommand cmd)
-    {
-        return executeSQL(cmd.getInsert(), cmd.getParamValues()); 
-    }
-
-    /**
-     * Executes an InsertInfo statement from a command object
-     * @param table the table into which to insert the selected data
-     * @param cmd the command object containing the selection command 
-     * @param conn a valid connection to the database.
-     * @return the number of records that have been inserted with the supplied 
statement
-     */
-    public final int executeInsertInto(DBTable table, DBCommand cmd)
-    {
-        return executeSQL(cmd.getInsertInto(table), cmd.getParamValues()); 
-    }
-
-    /**
-     * Executes an Update statement from a command object
-     * @param cmd the command object containing the update command
-     * @param conn a valid connection to the database.
-     * @return the number of records that have been updated with the supplied 
statement
-     */
-    public final int executeUpdate(DBCommand cmd)
-    {
-        return executeSQL(cmd.getUpdate(), cmd.getParamValues()); 
-    }
-
-    /**
-     * Executes a Delete statement from a command object
-     * @param from the database table from which to delete records
-     * @param cmd the command object containing the delete constraints
-     * @param conn a valid connection to the database.
-     * @return the number of records that have been deleted with the supplied 
statement
-     */
-    public final int executeDelete(DBTable from, DBCommand cmd)
-    {
-        return executeSQL(cmd.getDelete(from), cmd.getParamValues()); 
-    }
-    
+        
     /**
      * Executes a select SQL-Statement and returns a ResultSet containing the 
query results.<BR>
      * This function returns a JDBC ResultSet.<BR>
@@ -709,7 +604,6 @@ public class DBUtils implements DBContextAware
      * @param sqlCmd the SQL-Command
      * @param sqlParams a list of parameters for parameter queries (may depend 
on driver)
      * @param scrollable true if the reader should be scrollable or false if 
not
-     * @param conn a valid connection to the database.
      * @return the JDBC ResutSet
      */
     public ResultSet executeQuery(String sqlCmd, Object[] sqlParams, boolean 
scrollable)
diff --git 
a/empire-db/src/main/java/org/apache/empire/db/context/DBContextBase.java 
b/empire-db/src/main/java/org/apache/empire/db/context/DBContextBase.java
index cdc9a64..8a142b4 100644
--- a/empire-db/src/main/java/org/apache/empire/db/context/DBContextBase.java
+++ b/empire-db/src/main/java/org/apache/empire/db/context/DBContextBase.java
@@ -17,6 +17,11 @@ import org.apache.empire.exceptions.InvalidArgumentException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * DBContextBase
+ * Basic implementation of the DBContext interface which can be used as a base 
class for own implmentations
+ * @author rainer
+ */
 public abstract class DBContextBase implements DBContext
 {
     // Logger
@@ -25,22 +30,18 @@ public abstract class DBContextBase implements DBContext
     private Map<DBObject, DBRollbackHandler> rollbackHandler;
     
     private DBUtils utils = null;
-    
+
+    @SuppressWarnings("unchecked")
     @Override
-    public DBUtils getUtils()
+    public <T extends DBUtils> T getUtils()
     {
         if (utils==null)
             utils = createUtils();
-        return utils;
-    }
-    
-    protected DBUtils createUtils()
-    {
-        return new DBUtils(this);
+        return ((T)utils);
     }
     
     @Override
-    public synchronized void commit()
+    public void commit()
     {
         try
         {   // Check argument
@@ -68,7 +69,7 @@ public abstract class DBContextBase implements DBContext
      * @param conn a valid database connection
      */
     @Override
-    public synchronized void rollback()
+    public void rollback()
     {
         try
         {   // Check argument
@@ -89,7 +90,7 @@ public abstract class DBContextBase implements DBContext
     }
     
     @Override
-    public synchronized void addRollbackHandler(DBRollbackHandler handler)
+    public void addRollbackHandler(DBRollbackHandler handler)
     {
         if (rollbackHandler==null)
             rollbackHandler = new LinkedHashMap<DBObject, DBRollbackHandler>();
@@ -102,7 +103,7 @@ public abstract class DBContextBase implements DBContext
     }
     
     @Override
-    public synchronized void removeRollbackHandler(DBObject object)
+    public void removeRollbackHandler(DBObject object)
     {
         if (object==null)
             rollbackHandler=null;   // remove all
@@ -144,6 +145,15 @@ public abstract class DBContextBase implements DBContext
             handler.rollback();
         rollbackHandler=null;
     }
+
+    /**
+     * Factory function for Utils creation 
+     * @return the utils implementation
+     */
+    protected DBUtils createUtils()
+    {
+        return new DBUtils(this);
+    }
     
     /**
      * helper to close a connection on discard

Reply via email to