User: forder
Date: 00/08/24 03:56:38
Modified: src/main/org/jboss/ejb/plugins/jaws/jdbc
JDBCBeanExistsCommand.java JDBCCommand.java
JDBCCreateEntityCommand.java
JDBCDefinedFinderCommand.java
JDBCDestroyCommand.java JDBCFindByCommand.java
JDBCFindEntitiesCommand.java JDBCFinderCommand.java
JDBCInitCommand.java JDBCLoadEntityCommand.java
JDBCQueryCommand.java JDBCRemoveEntityCommand.java
JDBCStoreEntityCommand.java JDBCUpdateCommand.java
Log:
Attended to thread safety issues pointed out by Rickard.
Revision Changes Path
1.3 +6 -16
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCBeanExistsCommand.java
Index: JDBCBeanExistsCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCBeanExistsCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCBeanExistsCommand.java 2000/08/21 01:23:57 1.2
+++ JDBCBeanExistsCommand.java 2000/08/24 10:56:36 1.3
@@ -19,16 +19,10 @@
* @see <related>
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class JDBCBeanExistsCommand extends JDBCQueryCommand
{
- // Attributes ----------------------------------------------------
-
- private Object idArgument; // the id given to execute()
-
- private boolean result; // the result to be returned by execute()
-
// Constructors --------------------------------------------------
public JDBCBeanExistsCommand(JDBCCommandFactory factory)
@@ -45,15 +39,11 @@
public boolean execute(Object id)
{
- // Save argument so setParameters() can access it
- idArgument = id;
-
- // Assume bean doesn't exist; handleResult() can change this
- result = false;
+ boolean result = false;
try
{
- jdbcExecute();
+ result = ((Boolean)jdbcExecute(id)).booleanValue();
} catch (Exception e)
{
log.exception(e);
@@ -64,19 +54,19 @@
// JDBCQueryCommand overrides ------------------------------------
- protected void setParameters(PreparedStatement stmt)
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
throws Exception
{
- setPrimaryKeyParameters(stmt, 1, idArgument);
+ setPrimaryKeyParameters(stmt, 1, argOrArgs);
}
- protected void handleResult(ResultSet rs) throws Exception
+ protected Object handleResult(ResultSet rs, Object argOrArgs) throws Exception
{
if ( !rs.next() )
{
throw new SQLException("Unable to check for EJB in database");
}
int total = rs.getInt("Total");
- result = (total >= 1);
+ return new Boolean(total >= 1);
}
}
1.6 +34 -12 jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCCommand.java
Index: JDBCCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCCommand.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- JDBCCommand.java 2000/08/24 01:05:58 1.5
+++ JDBCCommand.java 2000/08/24 10:56:36 1.6
@@ -44,7 +44,7 @@
* utility methods that database commands may need to call.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.5 $
+ * @version $Revision: 1.6 $
*/
public abstract class JDBCCommand
{
@@ -88,22 +88,32 @@
* a database connection, preparing a statement, setting its parameters,
* executing the prepared statement, handling the result,
* and cleaning up.
+ *
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method, and passed on to 'hook' methods for
+ * getting SQL and for setting parameters.
+ * @return any result produced by the handling of the result of executing
+ * the prepared statement.
+ * @throws Exception if connection fails, or if any 'hook' method
+ * throws an exception.
*/
- protected void jdbcExecute() throws Exception
+ protected Object jdbcExecute(Object argOrArgs) throws Exception
{
Connection con = null;
PreparedStatement stmt = null;
+ Object result = null;
+
try
{
con = getConnection();
- String theSQL = getSQL();
+ String theSQL = getSQL(argOrArgs);
if (debug)
{
log.debug(name + " command executing: " + theSQL);
}
stmt = con.prepareStatement(theSQL);
- setParameters(stmt);
- executeStatementAndHandleResult(stmt);
+ setParameters(stmt, argOrArgs);
+ result = executeStatementAndHandleResult(stmt, argOrArgs);
} finally
{
if (stmt != null)
@@ -127,6 +137,8 @@
}
}
}
+
+ return result;
}
/**
@@ -151,11 +163,13 @@
* Override if dynamically-generated SQL, based on the arguments
* given to execute(), is needed.
*
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
* @return the SQL to use in the PreparedStatement.
* @throws Exception if an attempt to generate dynamic SQL results in
* an Exception.
*/
- protected String getSQL() throws Exception
+ protected String getSQL(Object argOrArgs) throws Exception
{
return sql;
}
@@ -165,21 +179,29 @@
* Override if parameters need to be set.
*
* @param stmt the PreparedStatement which will be executed by this Command.
- * @throws Exception if the parameter setting code throws an Exception.
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
+ * @throws Exception if parameter setting fails.
*/
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
}
/**
- * Executes the PreparedStatement and handle result of successful execution.
+ * Executes the PreparedStatement and handles result of successful execution.
* This is implemented in subclasses for queries and updates.
*
* @param stmt the PreparedStatement to execute.
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
+ * @return any result produced by the handling of the result of executing
+ * the prepared statement.
* @throws Exception if execution or result handling fails.
*/
- protected abstract void executeStatementAndHandleResult(
- PreparedStatement stmt) throws Exception;
+ protected abstract Object executeStatementAndHandleResult(
+ PreparedStatement stmt,
+ Object argOrArgs) throws Exception;
// ---------- Utility methods for use in subclasses ----------
@@ -332,7 +354,7 @@
// Result transformation required by Oracle, courtesy of Jay Walters
- if (result != null && result instanceof BigDecimal)
+ if (result instanceof BigDecimal)
{
BigDecimal bigDecResult = (BigDecimal)result;
@@ -413,7 +435,7 @@
* Returns the comma-delimited list of primary key column names
* for this entity.
*
- * return comma-delimited list of primary key column names.
+ * @return comma-delimited list of primary key column names.
*/
protected final String getPkColumnList()
{
1.3 +14 -12
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCCreateEntityCommand.java
Index: JDBCCreateEntityCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCCreateEntityCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCCreateEntityCommand.java 2000/08/21 01:23:58 1.2
+++ JDBCCreateEntityCommand.java 2000/08/24 10:56:36 1.3
@@ -39,7 +39,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class JDBCCreateEntityCommand
extends JDBCUpdateCommand
@@ -49,8 +49,6 @@
private JDBCBeanExistsCommand beanExistsCommand;
- private EntityEnterpriseContext ctxArgument;
-
// Constructors --------------------------------------------------
public JDBCCreateEntityCommand(JDBCCommandFactory factory)
@@ -105,9 +103,6 @@
EntityEnterpriseContext ctx)
throws RemoteException, CreateException
{
- // Save ctx for use in setParameters(), handleResult()
- ctxArgument = ctx;
-
try
{
// Extract pk
@@ -153,7 +148,7 @@
try
{
- jdbcExecute();
+ jdbcExecute(ctx);
} catch (Exception e)
{
log.exception(e);
@@ -171,15 +166,17 @@
// JDBCUpdateCommand overrides -----------------------------------
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
+ EntityEnterpriseContext ctx = (EntityEnterpriseContext)argOrArgs;
int idx = 1; // Parameter-index
Iterator iter = metaInfo.getCMPFieldInfos();
while (iter.hasNext())
{
CMPFieldInfo fieldInfo = (CMPFieldInfo)iter.next();
- Object value = getCMPFieldValue(ctxArgument.getInstance(), fieldInfo);
+ Object value = getCMPFieldValue(ctx.getInstance(), fieldInfo);
if (fieldInfo.isEJBReference())
{
@@ -191,10 +188,13 @@
}
}
- protected void handleResult(int rowsAffected) throws Exception
+ protected Object handleResult(int rowsAffected, Object argOrArgs)
+ throws Exception
{
// arguably should check one row went in!!!
+ EntityEnterpriseContext ctx = (EntityEnterpriseContext)argOrArgs;
+
// Store state to be able to do tuned updates
JAWSPersistenceManager.PersistenceContext pCtx =
new JAWSPersistenceManager.PersistenceContext();
@@ -203,8 +203,10 @@
if (metaInfo.isReadOnly()) pCtx.lastRead = System.currentTimeMillis();
// Save initial state for tuned updates
- pCtx.state = getState(ctxArgument);
+ pCtx.state = getState(ctx);
+
+ ctx.setPersistenceContext(pCtx);
- ctxArgument.setPersistenceContext(pCtx);
+ return null;
}
}
1.2 +5 -2
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCDefinedFinderCommand.java
Index: JDBCDefinedFinderCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCDefinedFinderCommand.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JDBCDefinedFinderCommand.java 2000/08/06 02:03:49 1.1
+++ JDBCDefinedFinderCommand.java 2000/08/24 10:56:36 1.2
@@ -22,7 +22,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class JDBCDefinedFinderCommand extends JDBCFinderCommand
{
@@ -73,11 +73,14 @@
// JDBCFinderCommand overrides ------------------------------------
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
+ Object[] args = (Object[])argOrArgs;
+
for (int i = 0; i < parameterArray.length; i++)
{
- stmt.setObject(i+1, argsArgument[parameterArray[i]]);
+ stmt.setObject(i+1, args[parameterArray[i]]);
}
}
}
1.3 +6 -3
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCDestroyCommand.java
Index: JDBCDestroyCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCDestroyCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCDestroyCommand.java 2000/08/21 01:23:58 1.2
+++ JDBCDestroyCommand.java 2000/08/24 10:56:36 1.3
@@ -21,7 +21,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class JDBCDestroyCommand
extends JDBCUpdateCommand
@@ -47,7 +47,7 @@
// Remove it!
try
{
- jdbcExecute();
+ jdbcExecute(null);
} catch (Exception e)
{
log.debug("Could not drop table " +
@@ -58,8 +58,11 @@
// JDBCUpdateCommand overrides -----------------------------------
- protected void handleResult(int rowsAffected) throws Exception
+ protected Object handleResult(int rowsAffected, Object argOrArgs)
+ throws Exception
{
log.debug("Table "+metaInfo.getTableName()+" removed");
+
+ return null;
}
}
1.4 +7 -4
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCFindByCommand.java
Index: JDBCFindByCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCFindByCommand.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JDBCFindByCommand.java 2000/08/23 09:56:30 1.3
+++ JDBCFindByCommand.java 2000/08/24 10:56:36 1.4
@@ -27,7 +27,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public class JDBCFindByCommand extends JDBCFinderCommand
{
@@ -90,16 +90,19 @@
// JDBCFinderCommand overrides -----------------------------------
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
+ Object[] args = (Object[])argOrArgs;
+
if (fieldInfo != null)
{
if (fieldInfo.isEJBReference())
{
- setForeignKey(stmt, 1, fieldInfo, argsArgument[0]);
+ setForeignKey(stmt, 1, fieldInfo, args[0]);
} else
{
- setParameter(stmt, 1, fieldInfo.getJDBCType(), argsArgument[0]);
+ setParameter(stmt, 1, fieldInfo.getJDBCType(), args[0]);
}
}
}
1.2 +29 -21
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCFindEntitiesCommand.java
Index: JDBCFindEntitiesCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCFindEntitiesCommand.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JDBCFindEntitiesCommand.java 2000/08/06 02:03:50 1.1
+++ JDBCFindEntitiesCommand.java 2000/08/24 10:56:36 1.2
@@ -34,7 +34,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class JDBCFindEntitiesCommand implements JPMFindEntitiesCommand
{
@@ -75,31 +75,39 @@
{
String finderName = finderMethod.getName();
- // Do we know a finder command for this method name?
+ JPMFindEntitiesCommand finderCommand = null;
- JPMFindEntitiesCommand finderCommand =
- (JPMFindEntitiesCommand)knownFinderCommands.get(finderName);
-
- // If we didn't get a finder command, see if we can make one
-
- if (finderCommand == null)
- {
- try
+ synchronized(this) {
+ // JF: TODO: get rid of this lazy instantiation, which
+ // requires synchronization, by doing all specific Finder
+ // creation in the constructor (i.e. at deployment time).
+
+ // Do we know a finder command for this method name?
+
+ finderCommand =
+ (JPMFindEntitiesCommand)knownFinderCommands.get(finderName);
+
+ // If we didn't get a finder command, see if we can make one
+
+ if (finderCommand == null)
{
- if (finderName.equals("findAll"))
+ try
{
- finderCommand = factory.createFindAllCommand();
- } else if (finderName.startsWith("findBy"))
+ if (finderName.equals("findAll"))
+ {
+ finderCommand = factory.createFindAllCommand();
+ } else if (finderName.startsWith("findBy"))
+ {
+ finderCommand = factory.createFindByCommand(finderMethod);
+ }
+
+ // Remember the new finder command
+ knownFinderCommands.put(finderName, finderCommand);
+
+ } catch (IllegalArgumentException e)
{
- finderCommand = factory.createFindByCommand(finderMethod);
+ factory.getLog().warning(e.getMessage());
}
-
- // Remember the new finder command
- knownFinderCommands.put(finderName, finderCommand);
-
- } catch (IllegalArgumentException e)
- {
- factory.getLog().warning(e.getMessage());
}
}
1.4 +10 -15
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCFinderCommand.java
Index: JDBCFinderCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCFinderCommand.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JDBCFinderCommand.java 2000/08/23 09:56:31 1.3
+++ JDBCFinderCommand.java 2000/08/24 10:56:36 1.4
@@ -33,17 +33,12 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public abstract class JDBCFinderCommand
extends JDBCQueryCommand
implements JPMFindEntitiesCommand
{
- // Attributes ----------------------------------------------------
-
- protected Object[] argsArgument;
- protected ArrayList result;
-
// Constructors --------------------------------------------------
public JDBCFinderCommand(JDBCCommandFactory factory, String name)
@@ -58,11 +53,11 @@
EntityEnterpriseContext ctx)
throws RemoteException, FinderException
{
- argsArgument = args;
+ Collection result = null;
try
{
- jdbcExecute();
+ result = (Collection)jdbcExecute(args);
} catch (Exception e)
{
log.exception(e);
@@ -74,10 +69,9 @@
// JDBCQueryCommand overrides ------------------------------------
- protected void handleResult(ResultSet rs) throws Exception
+ protected Object handleResult(ResultSet rs, Object argOrArgs) throws Exception
{
- result = new ArrayList();
- int i = 1; // parameter index
+ Collection result = new ArrayList();
if (metaInfo.hasCompositeKey())
{
@@ -86,11 +80,10 @@
{
while (rs.next())
{
- i = 1;
-
Object pk = metaInfo.getPrimaryKeyClass().newInstance();
-
+ int i = 1; // parameter index
Iterator it = metaInfo.getPkFieldInfos();
+
while (it.hasNext())
{
PkFieldInfo pkFieldInfo = (PkFieldInfo)it.next();
@@ -115,8 +108,10 @@
while (rs.next())
{
- result.add(getResultObject(rs, i, jdbcType));
+ result.add(getResultObject(rs, 1, jdbcType));
}
}
+
+ return result;
}
}
1.3 +6 -3
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCInitCommand.java
Index: JDBCInitCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCInitCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCInitCommand.java 2000/08/21 01:23:58 1.2
+++ JDBCInitCommand.java 2000/08/24 10:56:36 1.3
@@ -25,7 +25,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class JDBCInitCommand
extends JDBCUpdateCommand
@@ -82,7 +82,7 @@
// Try to create it
try
{
- jdbcExecute();
+ jdbcExecute(null);
} catch (Exception e)
{
log.debug("Could not create table " +
@@ -93,8 +93,11 @@
// JDBCUpdateCommand overrides -----------------------------------
- protected void handleResult(int rowsAffected) throws Exception
+ protected Object handleResult(int rowsAffected, Object argOrArgs)
+ throws Exception
{
log.debug("Table " + metaInfo.getTableName() + " created");
+
+ return null;
}
}
1.3 +22 -20
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCLoadEntityCommand.java
Index: JDBCLoadEntityCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCLoadEntityCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCLoadEntityCommand.java 2000/08/21 01:23:58 1.2
+++ JDBCLoadEntityCommand.java 2000/08/24 10:56:36 1.3
@@ -34,16 +34,12 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class JDBCLoadEntityCommand
extends JDBCQueryCommand
implements JPMLoadEntityCommand
{
- // Attributes ----------------------------------------------------
-
- private EntityEnterpriseContext ctxArgument;
-
// Constructors --------------------------------------------------
public JDBCLoadEntityCommand(JDBCCommandFactory factory)
@@ -90,14 +86,11 @@
public void execute(EntityEnterpriseContext ctx)
throws RemoteException
{
- // Save the argument for use by setParameters() and handleResult()
- ctxArgument = ctx;
-
- if ( !metaInfo.isReadOnly() || isTimedOut() )
+ if ( !metaInfo.isReadOnly() || isTimedOut(ctx) )
{
try
{
- jdbcExecute();
+ jdbcExecute(ctx);
} catch (Exception e)
{
throw new ServerException("Load failed", e);
@@ -107,15 +100,22 @@
// JDBCQueryCommand overrides ------------------------------------
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
- setPrimaryKeyParameters(stmt, 1, ctxArgument.getId());
+ EntityEnterpriseContext ctx = (EntityEnterpriseContext)argOrArgs;
+
+ setPrimaryKeyParameters(stmt, 1, ctx.getId());
}
- protected void handleResult(ResultSet rs) throws Exception
+ protected Object handleResult(ResultSet rs, Object argOrArgs) throws Exception
{
+ EntityEnterpriseContext ctx = (EntityEnterpriseContext)argOrArgs;
+
if (!rs.next())
- throw new NoSuchObjectException("Entity "+ctxArgument.getId()+" not
found");
+ {
+ throw new NoSuchObjectException("Entity "+ctx.getId()+" not found");
+ }
// Set values
int idx = 1;
@@ -186,7 +186,7 @@
Object ref = finder.invoke(home, new Object[] { pk });
// Set found entity
- setCMPFieldValue(ctxArgument.getInstance(), fieldInfo, ref);
+ setCMPFieldValue(ctx.getInstance(), fieldInfo, ref);
} catch (Exception e)
{
throw new ServerException("Could not restore reference", e);
@@ -196,7 +196,7 @@
// Load primitive
// TODO: this probably needs to be fixed for BLOB's etc.
- setCMPFieldValue(ctxArgument.getInstance(),
+ setCMPFieldValue(ctx.getInstance(),
fieldInfo,
getResultObject(rs, idx++, jdbcType));
}
@@ -204,17 +204,19 @@
// Store state to be able to do tuned updates
JAWSPersistenceManager.PersistenceContext pCtx =
-
(JAWSPersistenceManager.PersistenceContext)ctxArgument.getPersistenceContext();
+ (JAWSPersistenceManager.PersistenceContext)ctx.getPersistenceContext();
if (metaInfo.isReadOnly()) pCtx.lastRead = System.currentTimeMillis();
- pCtx.state = getState(ctxArgument);
+ pCtx.state = getState(ctx);
+
+ return null;
}
// Protected -----------------------------------------------------
- protected boolean isTimedOut()
+ protected boolean isTimedOut(EntityEnterpriseContext ctx)
{
JAWSPersistenceManager.PersistenceContext pCtx =
-
(JAWSPersistenceManager.PersistenceContext)ctxArgument.getPersistenceContext();
+ (JAWSPersistenceManager.PersistenceContext)ctx.getPersistenceContext();
return (System.currentTimeMillis() - pCtx.lastRead) >
metaInfo.getReadOnlyTimeOut();
}
1.3 +25 -5
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCQueryCommand.java
Index: JDBCQueryCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCQueryCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCQueryCommand.java 2000/08/18 03:21:01 1.2
+++ JDBCQueryCommand.java 2000/08/24 10:56:37 1.3
@@ -19,7 +19,7 @@
* Provides a Template Method implementation for
* <code>executeStatementAndHandleResult</code>.
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public abstract class JDBCQueryCommand extends JDBCCommand
{
@@ -38,15 +38,25 @@
/**
* Template Method that executes the PreparedStatement and calls
* <code>handleResult</code> on the resulting ResultSet.
+ *
+ * @param stmt the prepared statement, with its parameters already set.
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
+ * @return any result produced by the handling of the result of executing
+ * the prepared statement.
+ * @throws Exception if execution or result handling fails.
*/
- protected void executeStatementAndHandleResult(PreparedStatement stmt)
+ protected Object executeStatementAndHandleResult(PreparedStatement stmt,
+ Object argOrArgs)
throws Exception
{
ResultSet rs = null;
+ Object result = null;
+
try
{
rs = stmt.executeQuery();
- handleResult(rs);
+ result = handleResult(rs, argOrArgs);
} finally
{
if (rs != null)
@@ -60,10 +70,20 @@
}
}
}
+
+ return result;
}
/**
- * Handle the result of successful execution of the query.
+ * Handles the result of successful execution of the query.
+ *
+ * @param rs the result set from the query.
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
+ * @return any result produced by the handling of the result of executing
+ * the prepared statement.
+ * @throws Exception if execution or result handling fails.
*/
- protected abstract void handleResult(ResultSet rs) throws Exception;
+ protected abstract Object handleResult(ResultSet rs, Object argOrArgs)
+ throws Exception;
}
1.3 +10 -13
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCRemoveEntityCommand.java
Index: JDBCRemoveEntityCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCRemoveEntityCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCRemoveEntityCommand.java 2000/08/21 01:23:58 1.2
+++ JDBCRemoveEntityCommand.java 2000/08/24 10:56:37 1.3
@@ -24,18 +24,12 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class JDBCRemoveEntityCommand
extends JDBCUpdateCommand
implements JPMRemoveEntityCommand
{
- // Attributes ----------------------------------------------------
-
- private EntityEnterpriseContext ctxArgument;
-
- // Static --------------------------------------------------------
-
// Constructors --------------------------------------------------
public JDBCRemoveEntityCommand(JDBCCommandFactory factory)
@@ -53,12 +47,10 @@
public void execute(EntityEnterpriseContext ctx)
throws RemoteException, RemoveException
{
- ctxArgument = ctx;
-
try
{
// Remove from DB
- jdbcExecute();
+ jdbcExecute(ctx);
} catch (Exception e)
{
throw new RemoveException("Could not remove "+ctx.getId());
@@ -67,16 +59,21 @@
// JDBCUpdateCommand overrides -----------------------------------
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
- setPrimaryKeyParameters(stmt, 1, ctxArgument.getId());
+ EntityEnterpriseContext ctx = (EntityEnterpriseContext)argOrArgs;
+
+ setPrimaryKeyParameters(stmt, 1, ctx.getId());
}
- protected void handleResult(int rowsAffected) throws Exception
+ protected Object handleResult(int rowsAffected, Object argOrArgs)
+ throws Exception
{
if (rowsAffected == 0)
{
throw new RemoveException("Could not remove entity");
}
+ return null;
}
}
1.4 +50 -29
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCStoreEntityCommand.java
Index: JDBCStoreEntityCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCStoreEntityCommand.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JDBCStoreEntityCommand.java 2000/08/21 01:23:58 1.3
+++ JDBCStoreEntityCommand.java 2000/08/24 10:56:37 1.4
@@ -32,30 +32,23 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Joe Shevland</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Sebastien Alborini</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public class JDBCStoreEntityCommand
extends JDBCUpdateCommand
implements JPMStoreEntityCommand
{
- // Attributes ----------------------------------------------------
-
- private EntityEnterpriseContext ctxArgument;
- private boolean tuned;
- private Object[] currentState;
- private boolean[] dirtyField; // only used for tuned updates
-
// Constructors --------------------------------------------------
public JDBCStoreEntityCommand(JDBCCommandFactory factory)
{
super(factory, "Store");
- tuned = metaInfo.hasTunedUpdates();
+ boolean tuned = metaInfo.hasTunedUpdates();
// If we don't have tuned updates, create static SQL
if (!tuned)
{
- setSQL(makeSQL());
+ setSQL(makeSQL(null));
}
}
@@ -75,22 +68,26 @@
return;
}
- ctxArgument = ctx;
- currentState = getState(ctx);
+ ExecutionState es = new ExecutionState();
+ es.ctx = ctx;
+ es.currentState = getState(ctx);
boolean dirty = false;
+
+ boolean tuned = metaInfo.hasTunedUpdates();
+
// For tuned updates, need to see which fields have changed
if (tuned)
{
- dirtyField = new boolean[currentState.length];
+ es.dirtyField = new boolean[es.currentState.length];
Object[] oldState =
((JAWSPersistenceManager.PersistenceContext)ctx.getPersistenceContext()).state;
- for (int i = 0; i < currentState.length; i++)
+ for (int i = 0; i < es.currentState.length; i++)
{
- dirtyField[i] = changed(currentState[i], oldState[i]);
- dirty |= dirtyField[i];
+ es.dirtyField[i] = changed(es.currentState[i], oldState[i]);
+ dirty |= es.dirtyField[i];
}
}
@@ -99,7 +96,7 @@
try
{
// Update db
- jdbcExecute();
+ jdbcExecute(es);
} catch (Exception e)
{
@@ -114,13 +111,19 @@
* Returns dynamically-generated SQL if this entity
* has tuned updates, otherwise static SQL.
*/
- protected String getSQL() throws Exception
+ protected String getSQL(Object argOrArgs) throws Exception
{
- return tuned ? makeSQL() : super.getSQL();
+ boolean tuned = metaInfo.hasTunedUpdates();
+
+ return tuned ? makeSQL(argOrArgs) : super.getSQL(argOrArgs);
}
- protected void setParameters(PreparedStatement stmt) throws Exception
+ protected void setParameters(PreparedStatement stmt, Object argOrArgs)
+ throws Exception
{
+ ExecutionState es = (ExecutionState)argOrArgs;
+ boolean tuned = metaInfo.hasTunedUpdates();
+
int idx = 1;
Iterator iter = metaInfo.getCMPFieldInfos();
int i = 0;
@@ -128,32 +131,38 @@
{
CMPFieldInfo fieldInfo = (CMPFieldInfo)iter.next();
- if (!tuned || dirtyField[i])
+ if (!tuned || es.dirtyField[i])
{
if (fieldInfo.isEJBReference())
{
- idx = setForeignKey(stmt, idx, fieldInfo, currentState[i]);
+ idx = setForeignKey(stmt, idx, fieldInfo, es.currentState[i]);
} else
{
- setParameter(stmt, idx++, fieldInfo.getJDBCType(), currentState[i]);
+ setParameter(stmt, idx++, fieldInfo.getJDBCType(),
es.currentState[i]);
}
}
i++;
}
- setPrimaryKeyParameters(stmt, idx, ctxArgument.getId());
+ setPrimaryKeyParameters(stmt, idx, es.ctx.getId());
}
- protected void handleResult(int rowsAffected) throws Exception
+ protected Object handleResult(int rowsAffected, Object argOrArgs)
+ throws Exception
{
+ ExecutionState es = (ExecutionState)argOrArgs;
+ boolean tuned = metaInfo.hasTunedUpdates();
+
if (tuned)
{
// Save current state for tuned updates
JAWSPersistenceManager.PersistenceContext pCtx =
-
(JAWSPersistenceManager.PersistenceContext)ctxArgument.getPersistenceContext();
- pCtx.state = currentState;
+
(JAWSPersistenceManager.PersistenceContext)es.ctx.getPersistenceContext();
+ pCtx.state = es.currentState;
}
+
+ return null;
}
// Protected -----------------------------------------------------
@@ -166,8 +175,11 @@
/**
* Used to create static SQL (tuned = false) or dynamic SQL (tuned = true).
*/
- protected String makeSQL()
+ protected String makeSQL(Object argOrArgs)
{
+ ExecutionState es = (ExecutionState)argOrArgs; // NB: null if tuned
+ boolean tuned = metaInfo.hasTunedUpdates();
+
String sql = "UPDATE "+metaInfo.getTableName()+" SET ";
Iterator iter = metaInfo.getCMPFieldInfos();
int i = 0;
@@ -176,7 +188,7 @@
{
CMPFieldInfo fieldInfo = (CMPFieldInfo)iter.next();
- if (!tuned || dirtyField[i++])
+ if (!tuned || es.dirtyField[i++])
{
if (fieldInfo.isEJBReference())
{
@@ -199,5 +211,14 @@
}
sql += " WHERE "+getPkColumnWhereList();
return sql;
+ }
+
+ // Inner Classes -------------------------------------------------
+
+ protected static class ExecutionState
+ {
+ public EntityEnterpriseContext ctx;
+ public Object[] currentState;
+ public boolean[] dirtyField; // only used for tuned updates
}
}
1.3 +18 -4
jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCUpdateCommand.java
Index: JDBCUpdateCommand.java
===================================================================
RCS file:
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/jaws/jdbc/JDBCUpdateCommand.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDBCUpdateCommand.java 2000/08/21 01:23:58 1.2
+++ JDBCUpdateCommand.java 2000/08/24 10:56:37 1.3
@@ -16,7 +16,7 @@
* Provides a Template Method implementation for
* <code>executeStatementAndHandleResult</code>.
* @author <a href="mailto:[EMAIL PROTECTED]">Justin Forder</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public abstract class JDBCUpdateCommand extends JDBCCommand
{
@@ -35,8 +35,15 @@
/**
* Template Method that executes the PreparedStatement and calls
* <code>handleResult</code> on the integer result.
+ *
+ * @param stmt the prepared statement, with its parameters already set.
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
+ * @return the result from <code>handleResult</code>.
+ * @throws Exception if execution or result handling fails.
*/
- protected void executeStatementAndHandleResult(PreparedStatement stmt)
+ protected Object executeStatementAndHandleResult(PreparedStatement stmt,
+ Object argOrArgs)
throws Exception
{
int rowsAffected = stmt.executeUpdate();
@@ -46,11 +53,18 @@
log.debug("Rows affected = " + rowsAffected);
}
- handleResult(rowsAffected);
+ return handleResult(rowsAffected, argOrArgs);
}
/**
* Handle the result of successful execution of the update.
+
+ * @param rs the result set from the query.
+ * @param argOrArgs argument or array of arguments passed in from
+ * subclass execute method.
+ * @return any result needed by the subclass <code>execute</code>.
+ * @throws Exception if result handling fails.
*/
- protected abstract void handleResult(int rowsAffected) throws Exception;
+ protected abstract Object handleResult(int rowsAffected, Object argOrArgs)
+ throws Exception;
}