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 ResultSetAndStatement.java JdbcAccessImpl.java
Date Thu, 27 Nov 2003 12:06:58 GMT
arminw      2003/11/27 04:06:58

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        ResultSetAndStatement.java JdbcAccessImpl.java
  Log:
  be more strict of Statement and ResultSet handling
  in ResultSetAndStatement class.
  declare Statement/ResultField final to avoid
  re-setting of these fields (could cause resource leaking)
  
  Revision  Changes    Path
  1.7       +42 -17    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ResultSetAndStatement.java	13 May 2003 07:56:24 -0000	1.6
  +++ ResultSetAndStatement.java	27 Nov 2003 12:06:58 -0000	1.7
  @@ -64,19 +64,29 @@
   import java.sql.SQLException;
   
   /**
  + * Intern used wrapper for {@link Statement} and {@link ResultSet} instances.
  + * 
    * @version $Id$
    */
   public class ResultSetAndStatement
   {
   	private static Logger log = LoggerFactory.getLogger(ResultSetAndStatement.class);
   
  -	private Platform m_platform = null;
  -	public ResultSet m_rs = null;
  -	public Statement m_stmt = null;
  +	private final Platform m_platform;
  +    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 ResultSetAndStatement(Platform platform)
  +	public ResultSetAndStatement(Platform platform, Statement stmt, ResultSet rs)
   	{
   		m_platform = platform;
  +        m_stmt = stmt;
  +        m_rs = rs;
  +        isClosed = false;
   	}
   
   	/**
  @@ -84,23 +94,38 @@
   	 */
   	public void close()
   	{
  -		try
  +		if(isClosed) return;
  +
  +        try
   		{
  -			m_platform.beforeStatementClose(m_stmt, m_rs);
  -			//close statement on wrapped statement class, or real statement
  -			if (m_stmt != null)
  -			{
  -				m_stmt.close();
  -			}
  -			m_platform.afterStatementClose(m_stmt, m_rs);
  +			// this will close the result set
  +            m_platform.beforeStatementClose(m_stmt, m_rs);
   		}
   		catch (PlatformException e)
   		{
   			log.error("Platform dependent operation failed", e);
   		}
  -		catch (SQLException ignored)
  -		{
  -			if (log.isDebugEnabled()) log.debug("Statement closing failed", ignored);
  -		}
  +        // we want to close the statement in any case
  +        finally
  +        {
  +            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);
  +            }
  +        }
   	}
   }
  
  
  
  1.14      +8 -10     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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- JdbcAccessImpl.java	16 Nov 2003 20:32:41 -0000	1.13
  +++ JdbcAccessImpl.java	27 Nov 2003 12:06:58 -0000	1.14
  @@ -279,8 +279,6 @@
       {
           if (logger.isDebugEnabled())
               logger.safeDebug("executeQuery", query);
  -
  -        ResultSetAndStatement retval = new ResultSetAndStatement(broker.serviceConnectionManager().getSupportedPlatform());
           /*
   		 * MBAIRD: we should create a scrollable resultset if the start at
   		 * index or end at index is set
  @@ -293,7 +291,7 @@
           {
               scrollable = true;
           }
  -        
  +        ResultSetAndStatement retval = null;
           try
           {
               String sql = broker.serviceSqlGenerator().getPreparedSelectStatement(query,
cld);
  @@ -304,8 +302,9 @@
                   logger.debug("executeQuery: " + stmt);
   
               ResultSet rs = stmt.executeQuery();
  -            retval.m_rs = rs;
  -            retval.m_stmt = stmt;
  +
  +            retval = new ResultSetAndStatement(
  +                    broker.serviceConnectionManager().getSupportedPlatform(), stmt, rs);
               return retval;
           }
           catch (PersistenceBrokerException e)
  @@ -366,8 +365,7 @@
           if (logger.isDebugEnabled())
               logger.debug("executeSQL: " + sqlStatement);
   
  -        ResultSetAndStatement retval =
  -            new ResultSetAndStatement(broker.serviceConnectionManager().getSupportedPlatform());
  +        ResultSetAndStatement retval = null;
           StatementManagerIF stmtMan = broker.serviceStatementManager();
           try
           {
  @@ -376,8 +374,8 @@
               ResultSet 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.m_rs = rs;
  -            retval.m_stmt = stmt;
  +            retval = new ResultSetAndStatement(
  +                    broker.serviceConnectionManager().getSupportedPlatform(), stmt, rs);
               return retval;
           }
           catch (PersistenceBrokerException e)
  
  
  

---------------------------------------------------------------------
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