db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer JdbcAccessImpl.java ResultSetAndStatement.java
Date Sun, 09 Oct 2005 23:50:45 GMT
arminw      2005/10/09 16:50:45

  Modified:    src/java/org/apache/ojb/broker/accesslayer Tag:
                        OJB_1_0_RELEASE JdbcAccessImpl.java
                        ResultSetAndStatement.java
  Log:
  fix bug in statement handling when materialize object, code cleanup
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.22.2.8  +42 -45    db-ojb/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java
  
  Index: JdbcAccessImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java,v
  retrieving revision 1.22.2.7
  retrieving revision 1.22.2.8
  diff -u -r1.22.2.7 -r1.22.2.8
  --- JdbcAccessImpl.java	5 Oct 2005 18:01:25 -0000	1.22.2.7
  +++ JdbcAccessImpl.java	9 Oct 2005 23:50:45 -0000	1.22.2.8
  @@ -107,17 +107,18 @@
               logger.debug("executeDelete: " + obj);
           }
   
  +        StatementManagerIF sm = broker.serviceStatementManager();
           PreparedStatement stmt = null;
           try
           {
  -            stmt = broker.serviceStatementManager().getDeleteStatement(cld);
  +            stmt = sm.getDeleteStatement(cld);
               if (stmt == null)
               {
                   logger.error("getDeleteStatement returned a null statement");
                   throw new PersistenceBrokerException("JdbcAccessImpl: getDeleteStatement
returned a null statement");
               }
   
  -            broker.serviceStatementManager().bindDelete(stmt, cld, obj);
  +            sm.bindDelete(stmt, cld, obj);
               if (logger.isDebugEnabled())
                   logger.debug("executeDelete: " + stmt);
   
  @@ -157,7 +158,7 @@
           }
           finally
           {
  -            broker.serviceStatementManager().closeResources(stmt, null);
  +            sm.closeResources(stmt, null);
           }
       }
   
  @@ -173,15 +174,15 @@
               logger.debug("executeDelete (by Query): " + query);
           }
   
  +        StatementManagerIF sm = broker.serviceStatementManager();
           PreparedStatement stmt = null;
  -
           try
           {
               final String sql = this.broker.serviceSqlGenerator().getPreparedDeleteStatement(query,
cld).getStatement();
  -            stmt = broker.serviceStatementManager().getPreparedStatement(cld, sql,
  +            stmt = sm.getPreparedStatement(cld, sql,
                       false, StatementManagerIF.FETCH_SIZE_NOT_APPLICABLE, cld.getDeleteProcedure()!=null);
   
  -            broker.serviceStatementManager().bindStatement(stmt, query, cld, 1);
  +            sm.bindStatement(stmt, query, cld, 1);
               if (logger.isDebugEnabled())
                   logger.debug("executeDelete (by Query): " + stmt);
   
  @@ -197,7 +198,7 @@
           }
           finally
           {
  -            broker.serviceStatementManager().closeResources(stmt, null);
  +            sm.closeResources(stmt, null);
           }
       }
   
  @@ -212,18 +213,18 @@
           {
               logger.debug("executeInsert: " + obj);
           }
  -
  +        StatementManagerIF sm = broker.serviceStatementManager();
           PreparedStatement stmt = null;
           try
           {
  -            stmt = broker.serviceStatementManager().getInsertStatement(cld);
  +            stmt = sm.getInsertStatement(cld);
               if (stmt == null)
               {
                   logger.error("getInsertStatement returned a null statement");
                   throw new PersistenceBrokerException("getInsertStatement returned a null
statement");
               }
   
  -            broker.serviceStatementManager().bindInsert(stmt, cld, obj);
  +            sm.bindInsert(stmt, cld, obj);
               if (logger.isDebugEnabled())
                   logger.debug("executeInsert: " + stmt);
   
  @@ -261,7 +262,7 @@
                               .append("=")
                               .append(fields[i].getPersistentField().get(obj));
                   }
  -                msg.append("], object was " + obj);
  +                msg.append("], object was ").append(obj);
                   msg.append(", exception message is [").append(e.getMessage()).append("]");
                   msg.append(", SQL code [").append(stateCode).append("]");
               }
  @@ -286,7 +287,7 @@
           }
           finally
           {
  -            broker.serviceStatementManager().closeResources(stmt, null);
  +            sm.closeResources(stmt, null);
           }
       }
   
  @@ -313,16 +314,16 @@
           {
               scrollable = true;
           }
  +        StatementManagerIF sm = broker.serviceStatementManager();
           ResultSetAndStatement retval = null;
  -        SelectStatement sql = null;
  -
  +        SelectStatement sql;
           try
           {
               final int queryFetchSize = query.getFetchSize();
               sql = broker.serviceSqlGenerator().getPreparedSelectStatement(query, cld);
               final boolean isStoredProcedure = isStoredProcedure(sql.getStatement());
               final PreparedStatement stmt;
  -            stmt = broker.serviceStatementManager().getPreparedStatement(cld, sql.getStatement()
,
  +            stmt = sm.getPreparedStatement(cld, sql.getStatement() ,
                       scrollable, queryFetchSize, isStoredProcedure);
   
               ResultSet rs;
  @@ -331,7 +332,7 @@
                   // Query implemented as a stored procedure, which must return a result
set.
                   // Query sytax is: { ?= call PROCEDURE_NAME(?,...,?)}
                   getPlatform().registerOutResultSet((CallableStatement) stmt, 1);
  -                broker.serviceStatementManager().bindStatement(stmt, query, cld, 2);
  +                sm.bindStatement(stmt, query, cld, 2);
   
                   if (logger.isDebugEnabled())
                       logger.debug("executeQuery: " + stmt);
  @@ -341,7 +342,7 @@
               }
               else
               {
  -                broker.serviceStatementManager().bindStatement(stmt, query, cld, 1);
  +                sm.bindStatement(stmt, query, cld, 1);
   
                   if (logger.isDebugEnabled())
                       logger.debug("executeQuery: " + stmt);
  @@ -349,7 +350,7 @@
                   rs = stmt.executeQuery();
               }
   
  -            retval = new ResultSetAndStatement(getPlatform(), stmt, rs, sql);
  +            retval = new ResultSetAndStatement(sm, stmt, rs, sql);
               return retval;
           }
           catch (PersistenceBrokerException e)
  @@ -410,11 +411,11 @@
       {
           if (logger.isDebugEnabled()) logger.debug("executeSQL: " + sql);
           final boolean isStoredprocedure = isStoredProcedure(sql);
  -        StatementManagerIF stmtMan = broker.serviceStatementManager();
  +        StatementManagerIF sm = broker.serviceStatementManager();
           ResultSetAndStatement retval = null;
           try
           {
  -            final PreparedStatement stmt = stmtMan.getPreparedStatement(cld, sql,
  +            final PreparedStatement stmt = sm.getPreparedStatement(cld, sql,
                       scrollable, StatementManagerIF.FETCH_SIZE_NOT_EXPLICITLY_SET, isStoredprocedure);
   
               ResultSet rs;
  @@ -423,19 +424,19 @@
                   // Query implemented as a stored procedure, which must return a result
set.
                   // Query sytax is: { ?= call PROCEDURE_NAME(?,...,?)}
                   getPlatform().registerOutResultSet((CallableStatement) stmt, 1);
  -                stmtMan.bindValues(stmt, values, 2);
  +                sm.bindValues(stmt, values, 2);
                   stmt.execute();
                   rs = (ResultSet) ((CallableStatement) stmt).getObject(1);
               }
               else
               {
  -                stmtMan.bindValues(stmt, values, 1);
  +                sm.bindValues(stmt, values, 1);
                   rs = stmt.executeQuery();
               }
   
               // as we return the resultset for further operations, we cannot release the
statement yet.
               // that has to be done by the JdbcAccess-clients (i.e. RsIterator, ProxyRsIterator
and PkEnumeration.)
  -            retval = new ResultSetAndStatement(getPlatform(), stmt, rs, new SelectStatement()
  +            retval = new ResultSetAndStatement(sm, stmt, rs, new SelectStatement()
               {
                   public Query getQueryInstance()
                   {
  @@ -510,13 +511,13 @@
           int result;
           int index;
           PreparedStatement stmt = null;
  -        StatementManagerIF stmtMan = broker.serviceStatementManager();
  +        StatementManagerIF sm = broker.serviceStatementManager();
           try
           {
  -            stmt = stmtMan.getPreparedStatement(cld, sqlStatement,
  +            stmt = sm.getPreparedStatement(cld, sqlStatement,
                       Query.NOT_SCROLLABLE, StatementManagerIF.FETCH_SIZE_NOT_APPLICABLE,
isStoredProcedure(sqlStatement));
  -            index = stmtMan.bindValues(stmt, values1, 1);
  -            index = stmtMan.bindValues(stmt, values2, index);
  +            index = sm.bindValues(stmt, values1, 1);
  +            sm.bindValues(stmt, values2, index);
               result = stmt.executeUpdate();
           }
           catch (PersistenceBrokerException e)
  @@ -539,7 +540,7 @@
           }
           finally
           {
  -            stmtMan.closeResources(stmt, null);
  +            sm.closeResources(stmt, null);
           }
           return result;
       }
  @@ -563,22 +564,22 @@
           {
               return;
           }
  -        
  +
  +        StatementManagerIF sm = broker.serviceStatementManager();
           // BRJ: preserve current locking values
           // locking values will be restored in case of exception
           ValueContainer[] oldLockingValues;
           oldLockingValues = cld.getCurrentLockingValues(obj);
  -
           try
           {           
  -            stmt = broker.serviceStatementManager().getUpdateStatement(cld);
  +            stmt = sm.getUpdateStatement(cld);
               if (stmt == null)
               {
                   logger.error("getUpdateStatement returned a null statement");
                   throw new PersistenceBrokerException("getUpdateStatement returned a null
statement");
               }
   
  -            broker.serviceStatementManager().bindUpdate(stmt, cld, obj);
  +            sm.bindUpdate(stmt, cld, obj);
               if (logger.isDebugEnabled())
                   logger.debug("executeUpdate: " + stmt);
   
  @@ -626,7 +627,7 @@
           }
           finally
           {           
  -            broker.serviceStatementManager().closeResources(stmt, null);
  +            sm.closeResources(stmt, null);
           }
       }
   
  @@ -643,11 +644,12 @@
           throws PersistenceBrokerException
       {
           final StatementManagerIF sm = broker.serviceStatementManager();
  -        ResultSet rs = null;
  -        PreparedStatement stmt = null;
           SelectStatement sql = broker.serviceSqlGenerator().getPreparedSelectByPkStatement(cld);
  +        ResultSetAndStatement rs_stmt = null;
           try
           {
  +            ResultSet rs;
  +            PreparedStatement stmt;
               stmt = sm.getSelectByPKStatement(cld);
               if (stmt == null)
               {
  @@ -660,13 +662,12 @@
               */
               sm.bindSelect(stmt, oid, cld, false);
               rs = stmt.executeQuery();
  +            rs_stmt = new ResultSetAndStatement(sm, stmt, rs, sql);
   
               // data available read object, else return null
               if (rs.next())
               {
                   Map row = new HashMap();
  -                ResultSetAndStatement rs_stmt = new ResultSetAndStatement(getPlatform(),
stmt, rs, sql);
  -
                   cld.getRowReader().readObjectArrayFrom(rs_stmt, row);
                   return cld.getRowReader().readObjectFrom(row);
               }
  @@ -678,23 +679,19 @@
           catch (PersistenceBrokerException e)
           {
               logger.error(
  -                "PersistenceBrokerException during the execution of materializeObject:
"
  -                    + e.getMessage(),
  -                e);
  +                "PersistenceBrokerException during the execution of materializeObject:
" + e.getMessage(), e);
               throw e;
           }
           catch (SQLException e)
           {
               String msg = "SQLException during the execution of materializeObject (for a
"
  -                + cld.getClassOfObject().getName()
  -                + "): "
  -                + e.getMessage();
  +                    + cld.getClassOfObject().getName() + "): " + e.getMessage();
               logger.error(msg,e);
               throw new PersistenceBrokerSQLException(msg, e);
           }
           finally
           {
  -            broker.serviceStatementManager().closeResources(stmt, rs);
  +            if(rs_stmt != null) rs_stmt.close();
           }
       }
   
  
  
  
  1.13.2.2  +21 -52    db-ojb/src/java/org/apache/ojb/broker/accesslayer/ResultSetAndStatement.java
  
  Index: ResultSetAndStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ResultSetAndStatement.java,v
  retrieving revision 1.13.2.1
  retrieving revision 1.13.2.2
  diff -u -r1.13.2.1 -r1.13.2.2
  --- ResultSetAndStatement.java	5 Oct 2005 18:01:25 -0000	1.13.2.1
  +++ ResultSetAndStatement.java	9 Oct 2005 23:50:45 -0000	1.13.2.2
  @@ -15,16 +15,13 @@
    * limitations under the License.
    */
   
  +import java.sql.ResultSet;
  +import java.sql.Statement;
  +
   import org.apache.ojb.broker.accesslayer.sql.SelectStatement;
  -import org.apache.ojb.broker.platforms.Platform;
  -import org.apache.ojb.broker.platforms.PlatformException;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  -import java.sql.ResultSet;
  -import java.sql.Statement;
  -import java.sql.SQLException;
  -
   /**
    * Intern used wrapper for {@link Statement} and {@link ResultSet} instances.
    *
  @@ -32,69 +29,41 @@
    */
   public class ResultSetAndStatement
   {
  -	private static Logger log = LoggerFactory.getLogger(ResultSetAndStatement.class);
  +    private static Logger log = LoggerFactory.getLogger(ResultSetAndStatement.class);
   
  -	private final Platform m_platform;
  +	private final StatementManagerIF manager;
       private boolean isClosed;
       /*
       arminw: declare final to avoid stmt/rs leaking in use
       by re-setting these fields.
       */
  -	public final ResultSet m_rs;
  -	public final Statement m_stmt;
  +    public final ResultSet m_rs;
  +    public final Statement m_stmt;
       public final SelectStatement m_sql;
   
  -	public ResultSetAndStatement(Platform platform, Statement stmt, ResultSet rs, SelectStatement
sql)
  -	{
  -		m_platform = platform;
  +	public ResultSetAndStatement(StatementManagerIF manager, Statement stmt, ResultSet rs,
SelectStatement sql)
  +    {
  +		this.manager = manager;
           m_stmt = stmt;
           m_rs = rs;
           m_sql = sql;
           isClosed = false;
  -	}
  +    }
   
  -	/**
  -	 * do a platform specific resource release.
  +    /**
  +     * do a platform specific resource release.
        * <br/>
        * Note: This method must be called after usage
        * of this class.
  -	 */
  -	public void close()
  -	{
  -		if(isClosed) return;
  -
  -        try
  -		{
  -			// this will close the result set
  -            m_platform.beforeStatementClose(m_stmt, m_rs);
  -		}
  -		catch (PlatformException e)
  -		{
  -			log.error("Platform dependent operation failed", e);
  -		}
  -        // we want to close the statement in any case
  -        finally
  +     */
  +    public void close()
  +    {
  +        if(!isClosed)
           {
  -            try
  -            {
  -                isClosed = true;
  -                //close statement on wrapped statement class, or real statement
  -                if (m_stmt != null)
  -                {
  -                    m_stmt.close();
  -                }
  -                m_platform.afterStatementClose(m_stmt, m_rs);
  -            }
  -            catch (SQLException ignored)
  -            {
  -                if (log.isDebugEnabled()) log.debug("Statement closing failed", ignored);
  -            }
  -            catch (PlatformException e)
  -            {
  -                log.error("Platform dependent operation failed", e);
  -            }
  +            manager.closeResources(m_stmt, m_rs);
  +            isClosed = true;
           }
  -	}
  +    }
   
       protected void finalize() throws Throwable
       {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message