stephenh 2002/07/27 05:21:02
Modified: src/java/org/apache/torque/util BasePeer.java
src/java/org/apache/torque/oid IDBroker.java
src/templates/om Object.vm ObjectWithManager.vm Peer.vm
src/java/org/apache/torque Torque.java
Added: src/java/org/apache/torque/util Transaction.java
Log:
- Refactored transaction methods out of BasePeer and into new o.a.t.util.Transaction
helper class.
- Refactored closeConnection out of BasePeer and into Torque to be with the
Torque.getConnection method.
- Added a Transaction.beginOptional method to handle the logic of conditionally
using transactions to simply the logic in BasePeer (e.g. places where code was
duplication due to one having transactions and the other not).
Revision Changes Path
1.50 +499 -587
jakarta-turbine-torque/src/java/org/apache/torque/util/BasePeer.java
Index: BasePeer.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/util/BasePeer.java,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -r1.49 -r1.50
--- BasePeer.java 19 Jul 2002 21:10:51 -0000 1.49
+++ BasePeer.java 27 Jul 2002 12:21:02 -0000 1.50
@@ -54,14 +54,6 @@
* <http://www.apache.org/>.
*/
-import com.workingdogs.village.Column;
-import com.workingdogs.village.DataSet;
-import com.workingdogs.village.KeyDef;
-import com.workingdogs.village.QueryDataSet;
-import com.workingdogs.village.Record;
-import com.workingdogs.village.Schema;
-import com.workingdogs.village.TableDataSet;
-
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -72,29 +64,34 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
-import java.util.ArrayList;
-import java.util.Collections;
-import javax.naming.Context;
-import javax.naming.InitialContext;
+
+import org.apache.commons.collections.StringStack;
+import org.apache.log4j.Category;
import org.apache.torque.Torque;
-import org.apache.torque.om.NumberKey;
-import org.apache.torque.om.ObjectKey;
-import org.apache.torque.om.SimpleKey;
-import org.apache.torque.om.StringKey;
+import org.apache.torque.TorqueException;
import org.apache.torque.adapter.DB;
-import org.apache.torque.oid.IdGenerator;
import org.apache.torque.map.ColumnMap;
import org.apache.torque.map.DatabaseMap;
import org.apache.torque.map.MapBuilder;
import org.apache.torque.map.TableMap;
+import org.apache.torque.oid.IdGenerator;
+import org.apache.torque.om.NumberKey;
+import org.apache.torque.om.ObjectKey;
+import org.apache.torque.om.SimpleKey;
+import org.apache.torque.om.StringKey;
-//!! no good.
-import org.apache.commons.collections.StringStack;
-import org.apache.torque.TorqueException;
-import org.apache.log4j.Category;
+import com.workingdogs.village.Column;
+import com.workingdogs.village.DataSet;
+import com.workingdogs.village.KeyDef;
+import com.workingdogs.village.QueryDataSet;
+import com.workingdogs.village.Record;
+import com.workingdogs.village.Schema;
+import com.workingdogs.village.TableDataSet;
/**
* This is the base class for all Peer classes in the system. Peer
@@ -107,6 +104,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Frank Y. Kim</a>
* @author <a href="mailto:[EMAIL PROTECTED]">John D. McNally</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Brett McLaughlin</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Stephen Haberman</a>
* @version $Id$
*/
public abstract class BasePeer implements java.io.Serializable
@@ -130,24 +128,7 @@
/** Hashtable that contains the cached mapBuilders. */
private static Hashtable mapBuilders = new Hashtable(5);
- protected static Category category =
- Category.getInstance(BasePeer.class.getName());
-
- protected static final Context ctx = getInitialContext();
-
- private static Context getInitialContext()
- {
- Context ctx = null;
- try
- {
- ctx = new InitialContext();
- }
- catch (javax.naming.NamingException e)
- {
- category.error(e);
- }
- return ctx;
- }
+ protected static Category category = Category.getInstance(BasePeer.class);
/**
* Converts a hashtable to a byte array for storage/serialization.
@@ -266,31 +247,19 @@
catch (Exception e)
{
category.error(e);
- throw new Error("Error in BasePeer.initTableSchema(" + tableName
- + "): " + e.getMessage());
+ throw new Error(
+ "Error in BasePeer.initTableSchema("
+ + tableName
+ + "): "
+ + e.getMessage());
}
finally
{
- closeConnection(con);
+ Torque.closeConnection(con);
}
return schema;
}
- public static void closeConnection(Connection con)
- {
- if (con != null)
- {
- try
- {
- con.close();
- }
- catch(Exception e)
- {
- category.error("Error while closing connection.", e);
- }
- }
- }
-
/**
* Creates a Column array for a table based on its Schema.
*
@@ -312,8 +281,8 @@
catch (Exception e)
{
category.error(e);
- throw new Error("Error in BasePeer.initTableColumns(): " +
- e.getMessage());
+ throw new Error(
+ "Error in BasePeer.initTableColumns(): " + e.getMessage());
}
return columns;
}
@@ -343,8 +312,9 @@
* @param columnNames A String[].
* @return A String[].
*/
- public static String[] initCriteriaKeys(String tableName,
- String[] columnNames)
+ public static String[] initCriteriaKeys(
+ String tableName,
+ String[] columnNames)
{
String[] keys = new String[columnNames.length];
for (int i = 0; i < columnNames.length; i++)
@@ -355,107 +325,6 @@
}
/**
- * Begin a transaction. This method will fallback gracefully to
- * return a normal connection, if the database being accessed does
- * not support transactions.
- *
- * @param dbName Name of database.
- * @return The Connection for the transaction.
- * @exception TorqueException
- */
- public static Connection beginTransaction(String dbName)
- throws TorqueException
- {
- Connection con = Torque.getConnection(dbName);
- try
- {
- if (con.getMetaData().supportsTransactions())
- {
- con.setAutoCommit(false);
- }
- }
- catch (SQLException e)
- {
- throw new TorqueException(e);
- }
- return con;
- }
-
- /**
- * Commit a transaction. This method takes care of releasing the
- * connection after the commit. in databases that do not support
- * transactions, it only returns the connection.
- *
- * @param con The Connection for the transaction.
- * @exception TorqueException
- */
- public static void commitTransaction(Connection con)
- throws TorqueException
- {
- try
- {
- if (con.getMetaData().supportsTransactions())
- {
- con.commit();
- con.setAutoCommit(true);
- }
- }
- catch (SQLException e)
- {
- throw new TorqueException(e);
- }
- finally
- {
- closeConnection(con);
- }
- }
-
- /**
- * Roll back a transaction in databases that support transactions.
- * It also releases the connection. in databases that do not support
- * transactions, this method will log the attempt and release the
- * connection.
- *
- * @param con The Connection for the transaction.
- * @exception TorqueException
- */
- public static void rollBackTransaction(Connection con)
- throws TorqueException
- {
- if (con == null)
- {
- throw new NullPointerException("Connection object was null. " +
- "This could be due to a misconfiguration of the " +
- "DataSourceFactory. Check the logs and Torque.properties " +
- "to better determine the cause.");
- }
-
- try
- {
- if (con.getMetaData().supportsTransactions())
- {
- con.rollback();
- con.setAutoCommit(true);
- }
- else
- {
- category.error("An attempt was made to rollback a transaction "
- + "but the database did not allow the operation to be "
- + "rolled back.");
- }
- }
- catch (SQLException e)
- {
- throw new TorqueException(e);
- }
- finally
- {
- closeConnection(con);
- }
- }
-
-
- /**
* Convenience method that uses straight JDBC to delete multiple
* rows. Village throws an Exception when multiple rows are
* deleted.
@@ -466,9 +335,12 @@
* @param value The value of the column.
* @exception TorqueException
*/
- public static void deleteAll(Connection con, String table, String column,
- int value)
- throws TorqueException
+ public static void deleteAll(
+ Connection con,
+ String table,
+ String column,
+ int value)
+ throws TorqueException
{
Statement statement = null;
try
@@ -476,7 +348,8 @@
statement = con.createStatement();
StringBuffer query = new StringBuffer();
- query.append("DELETE FROM ")
+ query
+ .append("DELETE FROM ")
.append(table)
.append(" WHERE ")
.append(column)
@@ -516,7 +389,7 @@
* @exception TorqueException
*/
public static void deleteAll(String table, String column, int value)
- throws TorqueException
+ throws TorqueException
{
Connection con = null;
try
@@ -527,7 +400,7 @@
}
finally
{
- closeConnection(con);
+ Torque.closeConnection(con);
}
}
@@ -538,39 +411,22 @@
* @param criteria The criteria to use.
* @exception TorqueException
*/
- public static void doDelete(Criteria criteria)
- throws TorqueException
+ public static void doDelete(Criteria criteria) throws TorqueException
{
Connection con = null;
- String dbName = criteria.getDbName();
-
- // Do the work, assuring the database connection is released.
- if (criteria.isUseTransaction())
+ try
{
- // For PostgreSQL and LOBs.
- try
- {
- con = beginTransaction(criteria.getDbName());
- doDelete(criteria, con);
- commitTransaction(con);
- }
- catch (Exception e)
- {
- rollBackTransaction(con);
- throw new TorqueException(e);
- }
+ con =
+ Transaction.beginOptional(
+ criteria.getDbName(),
+ criteria.isUseTransaction());
+ doDelete(criteria, con);
+ Transaction.commit(con);
}
- else
+ catch (TorqueException e)
{
- try
- {
- con = Torque.getConnection(dbName);
- doDelete(criteria, con);
- }
- finally
- {
- closeConnection(con);
- }
+ Transaction.rollback(con);
+ throw new TorqueException(e);
}
}
@@ -583,10 +439,10 @@
* @exception TorqueException
*/
public static void doDelete(Criteria criteria, Connection con)
- throws TorqueException
+ throws TorqueException
{
- DB db = Torque.getDB( criteria.getDbName() );
- DatabaseMap dbMap = Torque.getDatabaseMap( criteria.getDbName() );
+ DB db = Torque.getDB(criteria.getDbName());
+ DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
// Set up a list of required tables and add extra entries to
// criteria if directed to delete all related records.
@@ -604,10 +460,12 @@
if (tableName2 != null)
{
tables.add(
- new StringBuffer(tableNames[i].length() +
- tableName2.length() + 1)
- .append(tableName2).append(' ').append(tableNames[i])
- .toString());
+ new StringBuffer(
+ tableNames[i].length() + tableName2.length() + 1)
+ .append(tableName2)
+ .append(' ')
+ .append(tableNames[i])
+ .toString());
}
else
{
@@ -615,7 +473,7 @@
}
}
- if ( criteria.isCascade() )
+ if (criteria.isCascade())
{
// This steps thru all the columns in the database.
TableMap[] tableMaps = dbMap.getTables();
@@ -632,8 +490,9 @@
&& key.equals(columnMaps[j].getRelatedName()))
{
tables.add(tableMaps[i].getName());
- criteria.add(columnMaps[j].getFullyQualifiedName(),
- criteria.getValue(key));
+ criteria.add(
+ columnMaps[j].getFullyQualifiedName(),
+ criteria.getValue(key));
}
}
}
@@ -651,19 +510,23 @@
ColumnMap colMap = columnMaps[j];
if (colMap.isPrimaryKey())
{
- kd.addAttrib( colMap.getColumnName() );
+ kd.addAttrib(colMap.getColumnName());
}
- String key = new StringBuffer(colMap.getTableName())
- .append('.').append(colMap.getColumnName()).toString();
+ String key =
+ new StringBuffer(colMap.getTableName())
+ .append('.')
+ .append(colMap.getColumnName())
+ .toString();
if (criteria.containsKey(key))
{
if (criteria.getComparison(key).equals(Criteria.CUSTOM))
{
- whereClause.add( criteria.getString(key) );
+ whereClause.add(criteria.getString(key));
}
else
{
- whereClause.add(SqlExpression.build(
+ whereClause.add(
+ SqlExpression.build(
colMap.getColumnName(),
criteria.getValue(key),
criteria.getComparison(key),
@@ -738,42 +601,29 @@
* have a primary key).
* @exception TorqueException
*/
- public static ObjectKey doInsert(Criteria criteria)
- throws TorqueException
+ public static ObjectKey doInsert(Criteria criteria) throws TorqueException
{
Connection con = null;
ObjectKey id = null;
- boolean doTransaction = criteria.isUseTransaction();
-
try
{
- // Get a connection to the db.
- if (doTransaction)
- {
- con = beginTransaction(criteria.getDbName());
- }
- else
- {
- con = Torque.getConnection(criteria.getDbName());
- }
+ con =
+ Transaction.beginOptional(
+ criteria.getDbName(),
+ criteria.isUseTransaction());
id = doInsert(criteria, con);
+ Transaction.commit(con);
}
- finally
+ catch (TorqueException e)
{
- if (doTransaction)
- {
- commitTransaction(con);
- }
- else
- {
- closeConnection(con);
- }
+ Transaction.rollback(con);
+ throw e;
}
+
return id;
}
-
/**
* Method to perform inserts based on values and keys in a
* Criteria.
@@ -809,15 +659,16 @@
Iterator keys = criteria.keySet().iterator();
if (keys.hasNext())
{
- tableName = criteria.getTableName((String)keys.next());
+ tableName = criteria.getTableName((String) keys.next());
}
else
{
- throw new TorqueException("Database insert attempted without " +
- "anything specified to insert");
+ throw new TorqueException(
+ "Database insert attempted without "
+ + "anything specified to insert");
}
- DatabaseMap dbMap = Torque.getDatabaseMap( criteria.getDbName() );
+ DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
TableMap tableMap = dbMap.getTable(tableName);
Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
IdGenerator keyGen = tableMap.getIdGenerator();
@@ -836,8 +687,8 @@
{
if (keyGen == null)
{
- throw new TorqueException("IdGenerator for table '" +
- tableName + "' is null");
+ throw new TorqueException(
+ "IdGenerator for table '" + tableName + "' is null");
}
// If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
// before the insert.
@@ -848,8 +699,9 @@
{
if (pk.getType() instanceof Number)
{
- id = new NumberKey(keyGen.getIdAsBigDecimal
- (con, keyInfo));
+ id =
+ new NumberKey(
+ keyGen.getIdAsBigDecimal(con, keyInfo));
}
else
{
@@ -924,20 +776,24 @@
* @param criteria A Criteria.
* @exception TorqueException
*/
- private static void insertOrUpdateRecord(Record rec,
- String tableName,
- Criteria criteria)
+ private static void insertOrUpdateRecord(
+ Record rec,
+ String tableName,
+ Criteria criteria)
throws TorqueException
{
DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
- ColumnMap[] columnMaps = dbMap.getTable( tableName ).getColumns();
+ ColumnMap[] columnMaps = dbMap.getTable(tableName).getColumns();
boolean shouldSave = false;
for (int j = 0; j < columnMaps.length; j++)
{
ColumnMap colMap = columnMaps[j];
- String key = new StringBuffer(colMap.getTableName())
- .append('.').append(colMap.getColumnName()).toString();
+ String key =
+ new StringBuffer(colMap.getTableName())
+ .append('.')
+ .append(colMap.getColumnName())
+ .toString();
if (criteria.containsKey(key))
{
// A village Record.setValue( String, Object ) would
@@ -959,52 +815,61 @@
}
else if (obj instanceof Integer)
{
- rec.setValue(colMap.getColumnName(),
- criteria.getInt(key));
+ rec.setValue(
+ colMap.getColumnName(),
+ criteria.getInt(key));
}
else if (obj instanceof BigDecimal)
{
rec.setValue(colMap.getColumnName(), (BigDecimal) obj);
}
- else if ( obj instanceof Boolean)
+ else if (obj instanceof Boolean)
{
- rec.setValue(colMap.getColumnName(),
- criteria.getBoolean(key) ? 1 : 0);
+ rec.setValue(
+ colMap.getColumnName(),
+ criteria.getBoolean(key) ? 1 : 0);
}
else if (obj instanceof java.util.Date)
{
- rec.setValue(colMap.getColumnName(),
- (java.util.Date) obj);
+ rec.setValue(
+ colMap.getColumnName(),
+ (java.util.Date) obj);
}
else if (obj instanceof Float)
{
- rec.setValue(colMap.getColumnName(),
- criteria.getFloat(key));
+ rec.setValue(
+ colMap.getColumnName(),
+ criteria.getFloat(key));
}
else if (obj instanceof Double)
{
- rec.setValue(colMap.getColumnName(),
- criteria.getDouble(key));
+ rec.setValue(
+ colMap.getColumnName(),
+ criteria.getDouble(key));
}
else if (obj instanceof Byte)
{
- rec.setValue(colMap.getColumnName(),
- ((Byte) obj).byteValue());
+ rec.setValue(
+ colMap.getColumnName(),
+ ((Byte) obj).byteValue());
}
else if (obj instanceof Long)
{
- rec.setValue(colMap.getColumnName(),
- criteria.getLong(key));
+ rec.setValue(
+ colMap.getColumnName(),
+ criteria.getLong(key));
}
else if (obj instanceof Short)
{
- rec.setValue(colMap.getColumnName(),
- ((Short) obj).shortValue());
- }
- else if (obj instanceof Hashtable )
- {
- rec.setValue(colMap.getColumnName(),
- hashtableToByteArray((Hashtable) obj));
+ rec.setValue(
+ colMap.getColumnName(),
+ ((Short) obj).shortValue());
+ }
+ else if (obj instanceof Hashtable)
+ {
+ rec.setValue(
+ colMap.getColumnName(),
+ hashtableToByteArray((Hashtable) obj));
}
else if (obj instanceof byte[])
{
@@ -1016,10 +881,10 @@
throw new TorqueException(e);
}
shouldSave = true;
- }
+ }
}
- if ( shouldSave )
+ if (shouldSave)
{
try
{
@@ -1032,7 +897,7 @@
}
else
{
- throw new TorqueException("No changes to save");
+ throw new TorqueException("No changes to save");
}
}
@@ -1043,7 +908,7 @@
* @exception TorqueException Trouble creating the query string.
*/
public static String createQueryString(Criteria criteria)
- throws TorqueException
+ throws TorqueException
{
Query query = new Query();
DB db = Torque.getDB(criteria.getDbName());
@@ -1084,12 +949,12 @@
}
else
{
- tableName = columnName.substring(parenPos + 1,
- columnName.indexOf('.') );
+ tableName =
+ columnName.substring(parenPos + 1, columnName.indexOf('.'));
// functions may contain qualifiers so only take the last
// word as the table name.
int lastSpace = tableName.lastIndexOf(' ');
- if (lastSpace != -1 )
+ if (lastSpace != -1)
{
tableName = tableName.substring(lastSpace + 1);
}
@@ -1098,10 +963,12 @@
if (tableName2 != null)
{
fromClause.add(
- new StringBuffer(tableName.length() +
- tableName2.length() + 1)
- .append(tableName2).append(' ').append(tableName)
- .toString());
+ new StringBuffer(
+ tableName.length() + tableName2.length() + 1)
+ .append(tableName2)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
@@ -1112,8 +979,8 @@
Iterator it = aliases.keySet().iterator();
while (it.hasNext())
{
- String key = (String) it.next();
- selectClause.add((String) aliases.get(key) + " AS " + key);
+ String key = (String) it.next();
+ selectClause.add((String) aliases.get(key) + " AS " + key);
}
Iterator critKeys = criteria.keySet().iterator();
@@ -1121,21 +988,23 @@
{
String key = (String) critKeys.next();
Criteria.Criterion criterion =
- (Criteria.Criterion) criteria.getCriterion(key);
+ (Criteria.Criterion) criteria.getCriterion(key);
Criteria.Criterion[] someCriteria =
- criterion.getAttachedCriterion();
+ criterion.getAttachedCriterion();
String table = null;
for (int i = 0; i < someCriteria.length; i++)
{
String tableName = someCriteria[i].getTable();
table = criteria.getTableForAlias(tableName);
- if ( table != null )
+ if (table != null)
{
fromClause.add(
- new StringBuffer(tableName.length() +
- table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
+ new StringBuffer(
+ tableName.length() + table.length() + 1)
+ .append(table)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
@@ -1143,10 +1012,14 @@
table = tableName;
}
- boolean ignorCase = ((criteria.isIgnoreCase() ||
- someCriteria[i].isIgnoreCase()) &&
- (dbMap.getTable(table).getColumn(
- someCriteria[i].getColumn()).getType() instanceof String));
+ boolean ignorCase =
+ ((criteria.isIgnoreCase()
+ || someCriteria[i].isIgnoreCase())
+ && (dbMap
+ .getTable(table)
+ .getColumn(someCriteria[i].getColumn())
+ .getType()
+ instanceof String));
someCriteria[i].setIgnoreCase(ignorCase);
}
@@ -1177,26 +1050,30 @@
if (table != null)
{
fromClause.add(
- new StringBuffer(tableName.length() +
- table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString());
+ new StringBuffer(
+ tableName.length() + table.length() + 1)
+ .append(table)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
fromClause.add(tableName);
}
- int dot = join2.indexOf('.');
+ int dot = join2.indexOf('.');
tableName = join2.substring(0, dot);
table = criteria.getTableForAlias(tableName);
if (table != null)
{
fromClause.add(
- new StringBuffer(tableName.length() +
- table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
+ new StringBuffer(
+ tableName.length() + table.length() + 1)
+ .append(table)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
@@ -1204,10 +1081,13 @@
table = tableName;
}
- boolean ignorCase = (criteria.isIgnoreCase() &&
- (dbMap.getTable(table).getColumn(
- join2.substring(dot + 1, join2.length()))
- .getType() instanceof String));
+ boolean ignorCase =
+ (criteria.isIgnoreCase()
+ && (dbMap
+ .getTable(table)
+ .getColumn(join2.substring(dot + 1, join2.length()))
+ .getType()
+ instanceof String));
whereClause.add(
SqlExpression.buildInnerJoin(join1, join2, ignorCase, db));
@@ -1215,14 +1095,16 @@
}
// need to allow for multiple group bys
- if ( groupBy != null && groupBy.size() > 0)
+ if (groupBy != null && groupBy.size() > 0)
{
for (int i = 0; i < groupBy.size(); i++)
{
String groupByColumn = groupBy.get(i);
if (groupByColumn.indexOf('.') == -1)
{
- throwMalformedColumnNameException("group by", groupByColumn);
+ throwMalformedColumnNameException(
+ "group by",
+ groupByColumn);
}
groupByClause.add(groupByColumn);
@@ -1245,40 +1127,45 @@
String orderByColumn = orderBy.get(i);
if (orderByColumn.indexOf('.') == -1)
{
- throwMalformedColumnNameException("order by",
- orderByColumn);
+ throwMalformedColumnNameException(
+ "order by",
+ orderByColumn);
}
- String table = orderByColumn.substring(0,
- orderByColumn.indexOf('.') );
+ String table =
+ orderByColumn.substring(0, orderByColumn.indexOf('.'));
// See if there's a space (between the column list and sort
// order in ORDER BY table.column DESC).
int spacePos = orderByColumn.indexOf(' ');
String columnName;
if (spacePos == -1)
{
- columnName = orderByColumn
- .substring(orderByColumn.indexOf('.') + 1);
+ columnName =
+ orderByColumn.substring(orderByColumn.indexOf('.') + 1);
}
else
{
- columnName = orderByColumn
- .substring(orderByColumn.indexOf('.') + 1, spacePos);
+ columnName =
+ orderByColumn.substring(
+ orderByColumn.indexOf('.') + 1,
+ spacePos);
}
ColumnMap column = dbMap.getTable(table).getColumn(columnName);
- if ( column.getType() instanceof String )
+ if (column.getType() instanceof String)
{
if (spacePos == -1)
{
- orderByClause.add(db.ignoreCaseInOrderBy(orderByColumn));
+ orderByClause.add(
+ db.ignoreCaseInOrderBy(orderByColumn));
}
else
{
- orderByClause.add(db.ignoreCaseInOrderBy(
+ orderByClause.add(
+ db.ignoreCaseInOrderBy(
orderByColumn.substring(0, spacePos))
- + orderByColumn.substring(spacePos) );
+ + orderByColumn.substring(spacePos));
}
- selectClause.add( db.ignoreCaseInOrderBy(
- table + '.' + columnName) );
+ selectClause.add(
+ db.ignoreCaseInOrderBy(table + '.' + columnName));
}
else
{
@@ -1293,19 +1180,23 @@
String limitString = null;
if (offset > 0 && db.supportsNativeOffset())
{
- switch(db.getLimitStyle())
+ switch (db.getLimitStyle())
{
- case DB.LIMIT_STYLE_MYSQL:
- limitString = new StringBuffer().append(offset)
- .append(", ")
- .append(limit)
- .toString();
+ case DB.LIMIT_STYLE_MYSQL :
+ limitString =
+ new StringBuffer()
+ .append(offset)
+ .append(", ")
+ .append(limit)
+ .toString();
break;
- case DB.LIMIT_STYLE_POSTGRES:
- limitString = new StringBuffer().append(limit)
- .append(", ")
- .append(offset)
- .toString();
+ case DB.LIMIT_STYLE_POSTGRES :
+ limitString =
+ new StringBuffer()
+ .append(limit)
+ .append(", ")
+ .append(offset)
+ .toString();
break;
}
@@ -1328,10 +1219,10 @@
{
switch (db.getLimitStyle())
{
- case DB.LIMIT_STYLE_ORACLE:
+ case DB.LIMIT_STYLE_ORACLE :
whereClause.add("rownum <= " + limitString);
break;
- default:
+ default :
query.setLimit(limitString);
}
}
@@ -1341,7 +1232,6 @@
return sql;
}
-
/**
* Returns all results.
*
@@ -1349,36 +1239,34 @@
* @return List of Record objects.
* @exception TorqueException
*/
- public static List doSelect(Criteria criteria)
- throws TorqueException
+ public static List doSelect(Criteria criteria) throws TorqueException
{
+ Connection con = null;
List results = null;
- if (criteria.isUseTransaction())
+
+ try
{
- Connection con = beginTransaction(criteria.getDbName());
- try
- {
- results = executeQuery(createQueryString(criteria),
- criteria.getOffset(),
- criteria.getLimit(),
- criteria.isSingleRecord(), con );
- commitTransaction(con);
- }
- catch (Exception e)
- {
- // make sure to return connection
- rollBackTransaction(con);
- throw new TorqueException(e);
- }
+ con =
+ Transaction.beginOptional(
+ criteria.getDbName(),
+ criteria.isUseTransaction());
+
+ results =
+ executeQuery(
+ createQueryString(criteria),
+ criteria.getOffset(),
+ criteria.getLimit(),
+ criteria.isSingleRecord(),
+ con);
+
+ Transaction.commit(con);
}
- else
+ catch (Exception e)
{
- results = executeQuery(createQueryString(criteria),
- criteria.getOffset(),
- criteria.getLimit(),
- criteria.getDbName(),
- criteria.isSingleRecord() );
+ Transaction.rollback(con);
+ throw new TorqueException(e);
}
+
return results;
}
@@ -1391,10 +1279,12 @@
* @exception TorqueException
*/
public static List doSelect(Criteria criteria, Connection con)
- throws TorqueException
+ throws TorqueException
{
- return executeQuery(createQueryString(criteria),
- criteria.isSingleRecord(), con);
+ return executeQuery(
+ createQueryString(criteria),
+ criteria.isSingleRecord(),
+ con);
}
/**
@@ -1406,8 +1296,7 @@
* @return List of Record objects.
* @exception TorqueException
*/
- public static List executeQuery(String queryString)
- throws TorqueException
+ public static List executeQuery(String queryString) throws TorqueException
{
return executeQuery(queryString, Torque.getDefaultDB(), false);
}
@@ -1423,7 +1312,7 @@
* @exception TorqueException
*/
public static List executeQuery(String queryString, String dbName)
- throws TorqueException
+ throws TorqueException
{
return executeQuery(queryString, dbName, false);
}
@@ -1438,10 +1327,11 @@
* @return List of Record objects.
* @exception TorqueException
*/
- public static List executeQuery(String queryString,
- String dbName,
- boolean singleRecord)
- throws TorqueException
+ public static List executeQuery(
+ String queryString,
+ String dbName,
+ boolean singleRecord)
+ throws TorqueException
{
return executeQuery(queryString, 0, -1, dbName, singleRecord);
}
@@ -1456,15 +1346,15 @@
* @return List of Record objects.
* @exception TorqueException
*/
- public static List executeQuery(String queryString,
- boolean singleRecord,
- Connection con)
+ public static List executeQuery(
+ String queryString,
+ boolean singleRecord,
+ Connection con)
throws TorqueException
{
return executeQuery(queryString, 0, -1, singleRecord, con);
}
-
/**
* Method for performing a SELECT.
*
@@ -1477,12 +1367,13 @@
* @return List of Record objects.
* @exception TorqueException
*/
- public static List executeQuery(String queryString,
- int start,
- int numberOfResults,
- String dbName,
- boolean singleRecord)
- throws TorqueException
+ public static List executeQuery(
+ String queryString,
+ int start,
+ int numberOfResults,
+ String dbName,
+ boolean singleRecord)
+ throws TorqueException
{
Connection db = null;
List results = null;
@@ -1490,12 +1381,17 @@
{
db = Torque.getConnection(dbName);
// execute the query
- results = executeQuery(queryString, start, numberOfResults,
- singleRecord, db);
+ results =
+ executeQuery(
+ queryString,
+ start,
+ numberOfResults,
+ singleRecord,
+ db);
}
finally
{
- closeConnection(db);
+ Torque.closeConnection(db);
}
return results;
}
@@ -1512,11 +1408,12 @@
* @return List of Record objects.
* @exception TorqueException
*/
- public static List executeQuery(String queryString,
- int start,
- int numberOfResults,
- boolean singleRecord,
- Connection con)
+ public static List executeQuery(
+ String queryString,
+ int start,
+ int numberOfResults,
+ boolean singleRecord,
+ Connection con)
throws TorqueException
{
QueryDataSet qds = null;
@@ -1525,11 +1422,13 @@
{
// execute the query
long startTime = System.currentTimeMillis();
- qds = new QueryDataSet( con, queryString );
- category.debug("Elapsed time="
- + (System.currentTimeMillis() - startTime) + " ms");
- results = getSelectResults(qds, start, numberOfResults,
- singleRecord);
+ qds = new QueryDataSet(con, queryString);
+ category.debug(
+ "Elapsed time="
+ + (System.currentTimeMillis() - startTime)
+ + " ms");
+ results =
+ getSelectResults(qds, start, numberOfResults, singleRecord);
}
catch (Exception e)
{
@@ -1553,24 +1452,24 @@
/**
* Returns all records in a QueryDataSet as a List of Record
- * objects. Used for functionality like util.db.LargeSelect.
+ * objects. Used for functionality like util.LargeSelect.
*
* @see #getSelectResults(QueryDataSet, int, int, boolean)
*/
public static List getSelectResults(QueryDataSet qds)
- throws TorqueException
+ throws TorqueException
{
- return getSelectResults( qds, 0, -1, false);
+ return getSelectResults(qds, 0, -1, false);
}
/**
* Returns all records in a QueryDataSet as a List of Record
- * objects. Used for functionality like util.db.LargeSelect.
+ * objects. Used for functionality like util.LargeSelect.
*
* @see #getSelectResults(QueryDataSet, int, int, boolean)
*/
public static List getSelectResults(QueryDataSet qds, boolean singleRecord)
- throws TorqueException
+ throws TorqueException
{
return getSelectResults(qds, 0, -1, singleRecord);
}
@@ -1578,14 +1477,15 @@
/**
* Returns numberOfResults records in a QueryDataSet as a List
* of Record objects. Starting at record 0. Used for
- * functionality like util.db.LargeSelect.
+ * functionality like util.LargeSelect.
*
* @see #getSelectResults(QueryDataSet, int, int, boolean)
*/
- public static List getSelectResults(QueryDataSet qds,
- int numberOfResults,
- boolean singleRecord)
- throws TorqueException
+ public static List getSelectResults(
+ QueryDataSet qds,
+ int numberOfResults,
+ boolean singleRecord)
+ throws TorqueException
{
List results = null;
if (numberOfResults != 0)
@@ -1598,7 +1498,7 @@
/**
* Returns numberOfResults records in a QueryDataSet as a List
* of Record objects. Starting at record start. Used for
- * functionality like util.db.LargeSelect.
+ * functionality like util.LargeSelect.
*
* @param qds The <code>QueryDataSet</code> to extract results
* from.
@@ -1611,11 +1511,12 @@
* @return A <code>List</code> of <code>Record</code> objects.
* @exception TorqueException If any <code>Exception</code> occurs.
*/
- public static List getSelectResults(QueryDataSet qds,
- int start,
- int numberOfResults,
- boolean singleRecord)
- throws TorqueException
+ public static List getSelectResults(
+ QueryDataSet qds,
+ int start,
+ int numberOfResults,
+ boolean singleRecord)
+ throws TorqueException
{
List results;
try
@@ -1659,7 +1560,7 @@
* @exception TorqueException
*/
private static ColumnMap getPrimaryKey(Criteria criteria)
- throws TorqueException
+ throws TorqueException
{
// Assume all the keys are for the same table.
String key = (String) criteria.keys().nextElement();
@@ -1710,35 +1611,22 @@
* set clause.
* @exception TorqueException
*/
- public static void doUpdate(Criteria updateValues)
- throws TorqueException
+ public static void doUpdate(Criteria updateValues) throws TorqueException
{
- boolean doTransaction = updateValues.isUseTransaction();
- Connection db = null;
+ Connection con = null;
try
{
- // Get a connection to the db.
- if (doTransaction)
- {
- db = beginTransaction(updateValues.getDbName());
- }
- else
- {
- db = Torque.getConnection(updateValues.getDbName());
- }
-
- doUpdate(updateValues, db);
+ con =
+ Transaction.beginOptional(
+ updateValues.getDbName(),
+ updateValues.isUseTransaction());
+ doUpdate(updateValues, con);
+ Transaction.commit(con);
}
- finally
+ catch (TorqueException e)
{
- if (doTransaction)
- {
- commitTransaction(db);
- }
- else
- {
- closeConnection(db);
- }
+ Transaction.rollback(con);
+ throw e;
}
}
@@ -1769,8 +1657,9 @@
if (pk != null && updateValues.containsKey(pk.getFullyQualifiedName()))
{
selectCriteria = new Criteria(2);
- selectCriteria.put(pk.getFullyQualifiedName(),
- updateValues.remove(pk.getFullyQualifiedName()));
+ selectCriteria.put(
+ pk.getFullyQualifiedName(),
+ updateValues.remove(pk.getFullyQualifiedName()));
}
else
{
@@ -1796,33 +1685,22 @@
* @exception TorqueException
*/
public static void doUpdate(Criteria selectCriteria, Criteria updateValues)
- throws TorqueException
+ throws TorqueException
{
- boolean doTransaction = updateValues.isUseTransaction();
Connection db = null;
try
{
- // Get a connection to the db.
- if (doTransaction)
- {
- db = beginTransaction(selectCriteria.getDbName());
- }
- else
- {
- db = Torque.getConnection(selectCriteria.getDbName());
- }
+ db =
+ Transaction.beginOptional(
+ selectCriteria.getDbName(),
+ updateValues.isUseTransaction());
doUpdate(selectCriteria, updateValues, db);
+ Transaction.commit(db);
}
- finally
+ catch (TorqueException e)
{
- if (doTransaction)
- {
- commitTransaction(db);
- }
- else
- {
- closeConnection(db);
- }
+ Transaction.rollback(db);
+ throw e;
}
}
@@ -1842,16 +1720,16 @@
* @param con A Connection.
* @exception TorqueException
*/
- public static void doUpdate(Criteria selectCriteria,
- Criteria updateValues,
- Connection con)
+ public static void doUpdate(
+ Criteria selectCriteria,
+ Criteria updateValues,
+ Connection con)
throws TorqueException
{
- DB db = Torque.getDB( selectCriteria.getDbName() );
- DatabaseMap dbMap =
- Torque.getDatabaseMap( selectCriteria.getDbName() );
+ DB db = Torque.getDB(selectCriteria.getDbName());
+ DatabaseMap dbMap = Torque.getDatabaseMap(selectCriteria.getDbName());
- // Set up a list of required tables. StringStack.add()
+ // Set up a list of required tables. StringStack.add()
// only adds element if it is unique.
StringStack tables = new StringStack();
Iterator it = selectCriteria.keySet().iterator();
@@ -1867,25 +1745,31 @@
DatabaseMap tempDbMap = dbMap;
ColumnMap[] columnMaps =
- tempDbMap.getTable( tables.get(i) ).getColumns();
+ tempDbMap.getTable(tables.get(i)).getColumns();
for (int j = 0; j < columnMaps.length; j++)
{
ColumnMap colMap = columnMaps[j];
if (colMap.isPrimaryKey())
{
- kd.addAttrib( colMap.getColumnName() );
+ kd.addAttrib(colMap.getColumnName());
}
- String key = new StringBuffer(colMap.getTableName())
- .append('.').append(colMap.getColumnName()).toString();
+ String key =
+ new StringBuffer(colMap.getTableName())
+ .append('.')
+ .append(colMap.getColumnName())
+ .toString();
if (selectCriteria.containsKey(key))
{
- if (selectCriteria.getComparison(key).equals(Criteria.CUSTOM))
+ if (selectCriteria
+ .getComparison(key)
+ .equals(Criteria.CUSTOM))
{
whereClause.add(selectCriteria.getString(key));
}
else
{
- whereClause.add(SqlExpression.build(
+ whereClause.add(
+ SqlExpression.build(
colMap.getColumnName(),
selectCriteria.getValue(key),
selectCriteria.getComparison(key),
@@ -1898,7 +1782,7 @@
try
{
// Get affected records.
- tds = new TableDataSet(con, tables.get(i), kd );
+ tds = new TableDataSet(con, tables.get(i), kd);
String sqlSnippet = whereClause.toString(" AND ");
category.debug("BasePeer.doUpdate: whereClause=" + sqlSnippet);
tds.where(sqlSnippet);
@@ -1911,8 +1795,10 @@
for (int j = 0; j < tds.size(); j++)
{
Record rec = tds.getRecord(j);
- BasePeer
- .insertOrUpdateRecord(rec, tables.get(i), updateValues);
+ BasePeer.insertOrUpdateRecord(
+ rec,
+ tables.get(i),
+ updateValues);
}
}
catch (Exception e)
@@ -1945,8 +1831,7 @@
* @return The number of rows affected.
* @exception TorqueException
*/
- public static int executeStatement(String stmt)
- throws TorqueException
+ public static int executeStatement(String stmt) throws TorqueException
{
return executeStatement(stmt, Torque.getDefaultDB());
}
@@ -1962,7 +1847,7 @@
* @exception TorqueException, a generic exception.
*/
public static int executeStatement(String stmt, String dbName)
- throws TorqueException
+ throws TorqueException
{
Connection db = null;
int rowCount = -1;
@@ -1973,7 +1858,7 @@
}
finally
{
- closeConnection(db);
+ Torque.closeConnection(db);
}
return rowCount;
}
@@ -2030,17 +1915,17 @@
* @exception TorqueException Couldn't handle multiple records.
*/
protected static void handleMultipleRecords(DataSet ds)
- throws TorqueException
+ throws TorqueException
{
- throw new TorqueException("Criteria expected single Record and " +
- "Multiple Records were selected");
+ throw new TorqueException(
+ "Criteria expected single Record and "
+ + "Multiple Records were selected");
}
/**
* @deprecated Use the better-named handleMultipleRecords() instead.
*/
- protected static void handleMultiple(DataSet ds)
- throws TorqueException
+ protected static void handleMultiple(DataSet ds) throws TorqueException
{
handleMultipleRecords(ds);
}
@@ -2052,8 +1937,7 @@
*
* @return A MapBuilder.
*/
- public static MapBuilder getMapBuilder()
- throws TorqueException
+ public static MapBuilder getMapBuilder() throws TorqueException
{
return getMapBuilder(DEFAULT_MAP_BUILDER.trim());
}
@@ -2095,20 +1979,20 @@
// is a relatively fast operation.
synchronized (mb)
{
- if (!mb.isBuilt())
- {
- try
- {
- mb.doBuild();
- }
- catch (Exception e)
- {
- // need to think about whether we'd want to remove
- // the MapBuilder from the cache if it can't be
- // built correctly...? pgo
- throw e;
- }
- }
+ if (!mb.isBuilt())
+ {
+ try
+ {
+ mb.doBuild();
+ }
+ catch (Exception e)
+ {
+ // need to think about whether we'd want to remove
+ // the MapBuilder from the cache if it can't be
+ // built correctly...? pgo
+ throw e;
+ }
+ }
}
return mb;
}
@@ -2117,11 +2001,11 @@
// Have to catch possible exceptions because method is
// used in initialization of Peers. Log the exception and
// return null.
- String message = "BasePeer.MapBuilder failed trying to instantiate: "
- + name;
+ String message =
+ "BasePeer.MapBuilder failed trying to instantiate: " + name;
if (category == null)
{
- System.out.println (message);
+ System.out.println(message);
e.printStackTrace();
}
else
@@ -2146,7 +2030,7 @@
StringBuffer qry = new StringBuffer();
List params = new ArrayList(criteria.size());
- createPreparedStatement (criteria, qry, params);
+ createPreparedStatement(criteria, qry, params);
PreparedStatement stmt = null;
try
@@ -2162,7 +2046,9 @@
}
else if (param instanceof NumberKey)
{
- stmt.setBigDecimal(i + 1, ((NumberKey) param).getBigDecimal());
+ stmt.setBigDecimal(
+ i + 1,
+ ((NumberKey) param).getBigDecimal());
}
else
{
@@ -2216,11 +2102,11 @@
try
{
- v = doPSSelect(criteria,con);
+ v = doPSSelect(criteria, con);
}
finally
{
- closeConnection(con);
+ Torque.closeConnection(con);
}
return v;
@@ -2230,9 +2116,10 @@
* Create a new PreparedStatement. It builds a string representation
* of a query and a list of PreparedStatement parameters.
*/
- public static void createPreparedStatement(Criteria criteria,
- StringBuffer queryString,
- List params)
+ public static void createPreparedStatement(
+ Criteria criteria,
+ StringBuffer queryString,
+ List params)
throws TorqueException
{
DB db = Torque.getDB(criteria.getDbName());
@@ -2254,7 +2141,7 @@
for (int i = 0; i < modifiers.size(); i++)
{
- selectModifiers.add( modifiers.get(i) );
+ selectModifiers.add(modifiers.get(i));
}
for (int i = 0; i < select.size(); i++)
@@ -2273,8 +2160,8 @@
}
else
{
- tableName = columnName.substring(parenPos + 1,
- columnName.indexOf('.') );
+ tableName =
+ columnName.substring(parenPos + 1, columnName.indexOf('.'));
// functions may contain qualifiers so only take the last
// word as the table name.
int lastSpace = tableName.lastIndexOf(' ');
@@ -2287,10 +2174,11 @@
if (tableName2 != null)
{
fromClause.add(
- new StringBuffer(tableName.length() +
- tableName2.length() + 1)
- .append(tableName2).append(' ').append(tableName)
- .toString() );
+ new StringBuffer(tableName.length() + tableName2.length() + 1)
+ .append(tableName2)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
@@ -2301,8 +2189,8 @@
Iterator it = aliases.keySet().iterator();
while (it.hasNext())
{
- String key = (String) it.next();
- selectClause.add((String) aliases.get(key) + " AS " + key);
+ String key = (String) it.next();
+ selectClause.add((String) aliases.get(key) + " AS " + key);
}
Iterator critKeys = criteria.keySet().iterator();
@@ -2310,9 +2198,9 @@
{
String key = (String) critKeys.next();
Criteria.Criterion criterion =
- (Criteria.Criterion) criteria.getCriterion(key);
+ (Criteria.Criterion) criteria.getCriterion(key);
Criteria.Criterion[] someCriteria =
- criterion.getAttachedCriterion();
+ criterion.getAttachedCriterion();
String table = null;
for (int i = 0; i < someCriteria.length; i++)
@@ -2321,9 +2209,11 @@
table = criteria.getTableForAlias(tableName);
if (table != null)
{
- fromClause.add(new StringBuffer(tableName.length()
- + table.length() + 1)
- .append(table).append(' ').append(tableName)
+ fromClause.add(
+ new StringBuffer(tableName.length() + table.length() + 1)
+ .append(table)
+ .append(' ')
+ .append(tableName)
.toString());
}
else
@@ -2332,18 +2222,21 @@
table = tableName;
}
- boolean ignorCase = ((criteria.isIgnoreCase()
+ boolean ignorCase =
+ ((criteria.isIgnoreCase()
|| someCriteria[i].isIgnoreCase())
- && (dbMap.getTable(table).getColumn(
- someCriteria[i].getColumn()).getType()
- instanceof String));
+ && (dbMap
+ .getTable(table)
+ .getColumn(someCriteria[i].getColumn())
+ .getType()
+ instanceof String));
someCriteria[i].setIgnoreCase(ignorCase);
}
criterion.setDB(db);
StringBuffer sb = new StringBuffer();
- criterion.appendPsTo(sb,params);
+ criterion.appendPsTo(sb, params);
whereClause.add(sb.toString());
}
@@ -2353,8 +2246,8 @@
{
for (int i = 0; i < join.size(); i++)
{
- String join1 = (String)join.get(i);
- String join2 = (String)criteria.getJoinR().get(i);
+ String join1 = (String) join.get(i);
+ String join2 = (String) criteria.getJoinR().get(i);
if (join1.indexOf('.') == -1)
{
throwMalformedColumnNameException("join", join1);
@@ -2366,12 +2259,14 @@
String tableName = join1.substring(0, join1.indexOf('.'));
String table = criteria.getTableForAlias(tableName);
- if ( table != null )
+ if (table != null)
{
- fromClause.add(new StringBuffer(tableName.length()
- + table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
+ fromClause.add(
+ new StringBuffer(tableName.length() + table.length() + 1)
+ .append(table)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
@@ -2383,10 +2278,12 @@
table = criteria.getTableForAlias(tableName);
if (table != null)
{
- fromClause.add(new StringBuffer(tableName.length()
- + table.length() + 1)
- .append(table).append(' ').append(tableName)
- .toString() );
+ fromClause.add(
+ new StringBuffer(tableName.length() + table.length() + 1)
+ .append(table)
+ .append(' ')
+ .append(tableName)
+ .toString());
}
else
{
@@ -2394,17 +2291,20 @@
table = tableName;
}
- boolean ignorCase = (criteria.isIgnoreCase()
- && (dbMap.getTable(table).getColumn(
- join2.substring(dot + 1, join2.length()))
- .getType() instanceof String));
+ boolean ignorCase =
+ (criteria.isIgnoreCase()
+ && (dbMap
+ .getTable(table)
+ .getColumn(join2.substring(dot + 1, join2.length()))
+ .getType()
+ instanceof String));
- whereClause.add(SqlExpression.buildInnerJoin(
- join1, join2, ignorCase, db));
+ whereClause.add(
+ SqlExpression.buildInnerJoin(join1, join2, ignorCase, db));
}
}
- if ( orderBy != null && orderBy.size() > 0)
+ if (orderBy != null && orderBy.size() > 0)
{
// Check for each String/Character column and apply
// toUpperCase().
@@ -2413,41 +2313,45 @@
String orderByColumn = orderBy.get(i);
if (orderByColumn.indexOf('.') == -1)
{
- throwMalformedColumnNameException("order by",
- orderByColumn);
+ throwMalformedColumnNameException(
+ "order by",
+ orderByColumn);
}
- String table = orderByColumn.substring(0,
- orderByColumn.indexOf('.'));
+ String table =
+ orderByColumn.substring(0, orderByColumn.indexOf('.'));
// See if there's a space (between the column list and sort
// order in ORDER BY table.column DESC).
int spacePos = orderByColumn.indexOf(' ');
String columnName;
if (spacePos == -1)
{
- columnName = orderByColumn
- .substring(orderByColumn.indexOf('.') + 1);
+ columnName =
+ orderByColumn.substring(orderByColumn.indexOf('.') + 1);
}
else
{
- columnName = orderByColumn
- .substring(orderByColumn.indexOf('.') + 1, spacePos);
+ columnName =
+ orderByColumn.substring(
+ orderByColumn.indexOf('.') + 1,
+ spacePos);
}
ColumnMap column = dbMap.getTable(table).getColumn(columnName);
- if ( column.getType() instanceof String )
+ if (column.getType() instanceof String)
{
if (spacePos == -1)
{
- orderByClause.add(db.ignoreCaseInOrderBy(
- orderByColumn));
+ orderByClause.add(
+ db.ignoreCaseInOrderBy(orderByColumn));
}
else
{
- orderByClause.add(db.ignoreCaseInOrderBy(
+ orderByClause.add(
+ db.ignoreCaseInOrderBy(
orderByColumn.substring(0, spacePos))
+ orderByColumn.substring(spacePos));
}
- selectClause.add( db.ignoreCaseInOrderBy(table + '.'
- + columnName) );
+ selectClause.add(
+ db.ignoreCaseInOrderBy(table + '.' + columnName));
}
else
{
@@ -2462,19 +2366,23 @@
String limitString = null;
if (offset > 0 && db.supportsNativeOffset())
{
- switch(db.getLimitStyle())
+ switch (db.getLimitStyle())
{
- case DB.LIMIT_STYLE_MYSQL:
- limitString = new StringBuffer().append(offset)
- .append(", ")
- .append(limit)
- .toString();
+ case DB.LIMIT_STYLE_MYSQL :
+ limitString =
+ new StringBuffer()
+ .append(offset)
+ .append(", ")
+ .append(limit)
+ .toString();
break;
- case DB.LIMIT_STYLE_POSTGRES:
- limitString = new StringBuffer().append(limit)
- .append(", ")
- .append(offset)
- .toString();
+ case DB.LIMIT_STYLE_POSTGRES :
+ limitString =
+ new StringBuffer()
+ .append(limit)
+ .append(", ")
+ .append(offset)
+ .toString();
break;
}
@@ -2497,22 +2405,22 @@
{
switch (db.getLimitStyle())
{
- case DB.LIMIT_STYLE_ORACLE:
- whereClause.add("rownum <= " + limitString);
- break;
- /* Don't have a Sybase install to validate this against. (dlr)
- case DB.LIMIT_STYLE_SYBASE:
- query.setRowcount(limitString);
- break;
- */
- default:
- query.setLimit(limitString);
+ case DB.LIMIT_STYLE_ORACLE :
+ whereClause.add("rownum <= " + limitString);
+ break;
+ /* Don't have a Sybase install to validate this against. (dlr)
+ case DB.LIMIT_STYLE_SYBASE:
+ query.setRowcount(limitString);
+ break;
+ */
+ default :
+ query.setLimit(limitString);
}
}
String sql = query.toString();
category.debug(sql);
- queryString.append (sql);
+ queryString.append(sql);
}
/**
@@ -2527,12 +2435,16 @@
* @param criteriaPhrase a String, one of "select", "join", or "order by"
* @param columnName a String containing the offending column name
*/
- private static void throwMalformedColumnNameException
- (String criteriaPhrase, String columnName)
+ private static void throwMalformedColumnNameException(
+ String criteriaPhrase,
+ String columnName)
throws TorqueException
{
- throw new TorqueException("Malformed column name in Criteria " +
- criteriaPhrase + ": '" + columnName +
- "' is not of the form 'table.column'");
+ throw new TorqueException(
+ "Malformed column name in Criteria "
+ + criteriaPhrase
+ + ": '"
+ + columnName
+ + "' is not of the form 'table.column'");
}
}
1.1
jakarta-turbine-torque/src/java/org/apache/torque/util/Transaction.java
Index: Transaction.java
===================================================================
package org.apache.torque.util;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Category;
import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
/**
* Refactored begin/commit/rollback transaction methods away from
* the <code>BasePeer</code>.
*
* <p>
* This can be used to handle cases where transaction support is optional.
* The second parameter of beginOptionalTransaction will determine with a
transaction
* is used or not. If a transaction is not used, the commit and rollback methods
* do not have any effect. Instead it simply makes the logic easier to follow
* by cutting down on the if statements based solely on whether a transaction
* is needed or not.
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen Haberman</a>
* @version $Revision: 1.1 $
*/
public class Transaction
{
/** The log. */
private static Category category = Category.getInstance(Transaction.class);
/**
* Begin a transaction. This method will fallback gracefully to
* return a normal connection, if the database being accessed does
* not support transactions.
*
* @param dbName Name of database.
* @return The Connection for the transaction.
* @throws TorqueException
*/
public static Connection begin(String dbName) throws TorqueException
{
return Transaction.beginOptional(dbName, true);
}
/**
* Begin a transaction. This method will fallback gracefully to
* return a normal connection, if the database being accessed does
* not support transactions.
*
* @param dbName Name of database.
* @param useTransaction If false, a transaction won't be used.
* @return The Connection for the transaction.
* @throws TorqueException
*/
public static Connection beginOptional(String dbName, boolean useTransaction)
throws TorqueException
{
Connection con = Torque.getConnection(dbName);
try
{
if (con.getMetaData().supportsTransactions() && useTransaction)
{
con.setAutoCommit(false);
}
}
catch (SQLException e)
{
throw new TorqueException(e);
}
return con;
}
/**
* Commit a transaction. This method takes care of releasing the
* connection after the commit. In databases that do not support
* transactions, it only returns the connection.
*
* @param con The Connection for the transaction.
* @throws TorqueException
*/
public static void commit(Connection con) throws TorqueException
{
if (con == null)
{
throw new NullPointerException(
"Connection object was null. "
+ "This could be due to a misconfiguration of the "
+ "DataSourceFactory. Check the logs and Torque.properties "
+ "to better determine the cause.");
}
try
{
if (con.getMetaData().supportsTransactions()
&& con.getAutoCommit() == false)
{
con.commit();
con.setAutoCommit(true);
}
}
catch (SQLException e)
{
throw new TorqueException(e);
}
finally
{
Torque.closeConnection(con);
}
}
/**
* Roll back a transaction in databases that support transactions.
* It also releases the connection. In databases that do not support
* transactions, this method will log the attempt and release the
* connection.
*
* @param con The Connection for the transaction.
* @throws TorqueException
*/
public static void rollback(Connection con) throws TorqueException
{
if (con == null)
{
throw new NullPointerException(
"Connection object was null. "
+ "This could be due to a misconfiguration of the "
+ "DataSourceFactory. Check the logs and Torque.properties "
+ "to better determine the cause.");
}
try
{
if (con.getMetaData().supportsTransactions()
&& con.getAutoCommit() == false)
{
con.rollback();
con.setAutoCommit(true);
}
}
catch (SQLException e)
{
category.error(
"An attempt was made to rollback a transaction "
+ "but the database did not allow the operation to be "
+ "rolled back.",
e);
throw new TorqueException(e);
}
finally
{
Torque.closeConnection(con);
}
}
/**
* Roll back a transaction without throwing errors if they occur.
*
* @param con The Connection for the transaction.
* @see safeRollback
*/
public static void safeRollback(Connection con)
{
try
{
Transaction.rollback(con);
}
catch (TorqueException e)
{
category.error("An error occured during rollback.", e);
}
}
}
1.18 +8 -30
jakarta-turbine-torque/src/java/org/apache/torque/oid/IDBroker.java
Index: IDBroker.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/oid/IDBroker.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- IDBroker.java 21 May 2002 19:51:45 -0000 1.17
+++ IDBroker.java 27 Jul 2002 12:21:02 -0000 1.18
@@ -59,16 +59,17 @@
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.Hashtable;
+import java.util.Iterator;
import java.util.List;
-import org.apache.log4j.Category;
+
import org.apache.commons.configuration.Configuration;
+import org.apache.log4j.Category;
import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.torque.map.DatabaseMap;
import org.apache.torque.map.TableMap;
-import org.apache.torque.util.BasePeer;
+import org.apache.torque.util.Transaction;
//!!
// NOTE:
@@ -653,16 +654,7 @@
.getBoolean(DB_IDBROKER_USENEWCONNECTION, true))
{
String databaseName = dbMap.getName();
- // Get a connection to the db by starting a
- // transaction.
- if (transactionsSupported)
- {
- connection = BasePeer.beginTransaction(databaseName);
- }
- else
- {
- connection = Torque.getConnection(databaseName);
- }
+ connection = Transaction.beginOptional(dbMap.getName(),
transactionsSupported);
}
// Write the current value of quantity of keys to grab
@@ -682,26 +674,12 @@
BigDecimal newNextId = nextId.add(quantity);
updateNextId(connection, tableName, newNextId.toString() );
- if (transactionsSupported && connection != null)
- {
- BasePeer.commitTransaction(connection);
- }
+ Transaction.commit(connection);
}
catch (Exception e)
{
- if (transactionsSupported && connection != null)
- {
- BasePeer.rollBackTransaction(connection);
- }
+ Transaction.rollback(connection);
throw e;
- }
- finally
- {
- if (!transactionsSupported && connection != null)
- {
- // Return the connection to the pool.
- BasePeer.closeConnection(connection);
- }
}
List availableIds = (List) ids.get(tableName);
1.42 +5 -14 jakarta-turbine-torque/src/templates/om/Object.vm
Index: Object.vm
===================================================================
RCS file: /home/cvs/jakarta-turbine-torque/src/templates/om/Object.vm,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -r1.41 -r1.42
--- Object.vm 23 Jul 2002 17:47:25 -0000 1.41
+++ Object.vm 27 Jul 2002 12:21:02 -0000 1.42
@@ -18,6 +18,7 @@
import org.apache.torque.om.*;
import org.apache.torque.util.BasePeer;
import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Transaction;
import java.sql.Connection;
#if ($addSaveMethod)
import org.apache.commons.lang.ObjectUtils;
@@ -917,24 +918,14 @@
#if ($complexObjectModel)
try
{
- con = BasePeer.beginTransaction(dbName);
+ con = Transaction.begin(dbName);
save(con);
+ Transaction.commit(con);
}
catch(TorqueException e)
{
- try
- {
- BasePeer.rollBackTransaction(con);
- }
- catch (Exception rollBackEx)
- {
- throw new TorqueException(e.getMessage()
- + "; a rollback exception ocurred when rolling back the error: "
- + rollBackEx.getMessage(), e);
- }
- throw e;
+ Transaction.safeRollback(con);
}
- BasePeer.commitTransaction(con);
#else
if (isModified())
@@ -956,7 +947,7 @@
}
finally
{
- BasePeer.closeConnection(con);
+ Torque.closeConnection(con);
}
}
1.19 +5 -15 jakarta-turbine-torque/src/templates/om/ObjectWithManager.vm
Index: ObjectWithManager.vm
===================================================================
RCS file: /home/cvs/jakarta-turbine-torque/src/templates/om/ObjectWithManager.vm,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- ObjectWithManager.vm 23 Jul 2002 17:47:25 -0000 1.18
+++ ObjectWithManager.vm 27 Jul 2002 12:21:02 -0000 1.19
@@ -19,6 +19,7 @@
import org.apache.torque.om.*;
import org.apache.torque.util.BasePeer;
import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Transaction;
import java.sql.Connection;
#if ($addSaveMethod)
import org.apache.commons.lang.ObjectUtils;
@@ -914,25 +915,14 @@
#if ($complexObjectModel)
try
{
- con = BasePeer.beginTransaction(dbName);
+ con = Transaction.begin(dbName);
save(con);
+ Transaction.commit(con);
}
catch(TorqueException e)
{
- try
- {
- BasePeer.rollBackTransaction(con);
- }
- catch (Exception rollBackEx)
- {
- throw new TorqueException(e.getMessage()
- + "; a rollback exception ocurred when rolling back the error:
"
- + rollBackEx.getMessage(),
- e);
- }
- throw e;
+ Transaction.safeRollback(con);
}
- BasePeer.commitTransaction(con);
#else
if (isModified())
@@ -954,7 +944,7 @@
}
finally
{
- BasePeer.closeConnection(con);
+ Torque.closeConnection(con);
}
}
1.35 +3 -12 jakarta-turbine-torque/src/templates/om/Peer.vm
Index: Peer.vm
===================================================================
RCS file: /home/cvs/jakarta-turbine-torque/src/templates/om/Peer.vm,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- Peer.vm 16 Jul 2002 18:53:38 -0000 1.34
+++ Peer.vm 27 Jul 2002 12:21:02 -0000 1.35
@@ -864,10 +864,7 @@
}
finally
{
- if (db != null)
- {
- closeConnection(db);
- }
+ Torque.closeConnection(db);
}
return(retVal);
}
@@ -922,10 +919,7 @@
}
finally
{
- if (db != null)
- {
- closeConnection(db);
- }
+ Torque.closeConnection(db);
}
return(retVal);
}
@@ -1012,10 +1006,7 @@
}
finally
{
- if (db != null)
- {
- closeConnection(db);
- }
+ Torque.closeConnection(db);
}
return(retVal);
}
1.69 +38 -15 jakarta-turbine-torque/src/java/org/apache/torque/Torque.java
Index: Torque.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/Torque.java,v
retrieving revision 1.68
retrieving revision 1.69
diff -u -r1.68 -r1.69
--- Torque.java 26 Jul 2002 14:48:23 -0000 1.68
+++ Torque.java 27 Jul 2002 12:21:02 -0000 1.69
@@ -54,34 +54,36 @@
* <http://www.apache.org/>.
*/
+import java.io.File;
import java.io.IOException;
import java.sql.Connection;
-import java.io.File;
-import java.util.HashMap;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.Iterator;
-import java.util.Map;
import java.util.List;
-import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Map;
import java.util.Properties;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.NullEnumeration;
+import org.apache.stratum.lifecycle.Configurable;
+import org.apache.stratum.lifecycle.Disposable;
+import org.apache.stratum.lifecycle.Initializable;
import org.apache.torque.adapter.DB;
import org.apache.torque.adapter.DBFactory;
+import org.apache.torque.dsfactory.DataSourceFactory;
+import org.apache.torque.manager.AbstractBaseManager;
import org.apache.torque.map.DatabaseMap;
import org.apache.torque.map.TableMap;
-import org.apache.torque.oid.IDGeneratorFactory;
import org.apache.torque.oid.IDBroker;
+import org.apache.torque.oid.IDGeneratorFactory;
import org.apache.torque.util.BasePeer;
-import org.apache.torque.manager.AbstractBaseManager;
-import org.apache.torque.dsfactory.DataSourceFactory;
-import org.apache.commons.configuration.Configuration;
-import org.apache.commons.configuration.PropertiesConfiguration;
-import org.apache.stratum.lifecycle.Configurable;
-import org.apache.stratum.lifecycle.Initializable;
-import org.apache.stratum.lifecycle.Disposable;
/**
* The implementation of Torque.
@@ -930,7 +932,7 @@
* Returns database adapter for a specific connection pool.
*
* @param name A pool name.
- * @return The corresponding database adapter.
+ * @return The corresponding database adapter.
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -968,4 +970,25 @@
{
return getDefaultDB();
}
+
+ /**
+ * Closes a connection.
+ *
+ * @param con A Connection to close.
+ */
+ public static void closeConnection(Connection con)
+ {
+ if (con != null)
+ {
+ try
+ {
+ con.close();
+ }
+ catch (SQLException e)
+ {
+ category.error("Error occured while closing connection.", e);
+ }
+ }
+ }
+
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>