Class StatementDecoratorInterceptor.StatementProxy<T extends java.sql.Statement>

  • Type Parameters:
    T - The type of Statement being proxied.
    All Implemented Interfaces:
    java.lang.reflect.InvocationHandler
    Direct Known Subclasses:
    StatementCache.CachedStatement
    Enclosing class:
    StatementDecoratorInterceptor

    protected class StatementDecoratorInterceptor.StatementProxy<T extends java.sql.Statement>
    extends java.lang.Object
    implements java.lang.reflect.InvocationHandler
    Invocation handler behind a statement proxy: it keeps the handle contract the class javadoc describes - lifecycle answered from its own state, close failures passed on, the borrow's connection handle reported instead of the driver's - and wraps the result sets of the calls named in StatementDecoratorInterceptor.RESULTSET_TYPES.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean closed
      Indicates whether this statement has been closed.
      protected T delegate
      The delegated statement, nulled once a close has been forwarded.
    • Constructor Summary

      Constructors 
      Constructor Description
      StatementProxy​(T delegate, java.lang.String sql)
      Constructs a StatementProxy.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void closeInvoked()
      Called when the close method is invoked on the statement.
      boolean equals​(java.lang.Object obj)  
      java.lang.Object getActualProxy()
      Returns the actual proxy object.
      java.lang.Object getConnection()
      Returns the connection proxy most recently bound through setConnection(Object).
      java.lang.reflect.Constructor<?> getConstructor()
      Returns the constructor used to create this proxy.
      T getDelegate()
      Returns the delegated statement.
      java.lang.String getSql()
      Returns the SQL text.
      int hashCode()  
      java.lang.Object invoke​(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)  
      void setActualProxy​(java.lang.Object proxy)
      Sets the actual proxy object.
      void setConnection​(java.lang.Object proxy)
      Binds the connection handle that getConnection() will report for this statement.
      void setConstructor​(java.lang.reflect.Constructor<?> constructor)
      Sets the constructor for creating proxies.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • closed

        protected volatile boolean closed
        Indicates whether this statement has been closed. A statement can be closed from a thread other than the one executing on it - the statement finalizer closes tracked statements from whichever thread closes the connection, and the statement cache forces a close from its own purge - so the flag is written by one thread and read by others, and must be volatile to reach them at all.
      • delegate

        protected volatile T extends java.sql.Statement delegate
        The delegated statement, nulled once a close has been forwarded. It is read by threads other than the one that nulls it, so, like closed, volatile.
    • Constructor Detail

      • StatementProxy

        public StatementProxy​(T delegate,
                              java.lang.String sql)
        Constructs a StatementProxy.
        Parameters:
        delegate - the statement to delegate to
        sql - the SQL text
    • Method Detail

      • getDelegate

        public T getDelegate()
        Returns the delegated statement.
        Returns:
        the delegated statement
      • getSql

        public java.lang.String getSql()
        Returns the SQL text.
        Returns:
        the SQL text
      • setConnection

        public void setConnection​(java.lang.Object proxy)
        Binds the connection handle that getConnection() will report for this statement. createDecorator binds the handle of the borrow that creates the statement, and StatementCache seeds the closing borrow's handle when it parks a statement; the binding is kept as a snapshot and is never refreshed on its own. A handle kept past its borrow is inert by design, so anything reusing a statement proxy across borrows must call this method with the new borrower's handle before the proxy is handed out, as StatementCache does on a cache hit.
        Parameters:
        proxy - the connection proxy
      • getConnection

        public java.lang.Object getConnection()
        Returns the connection proxy most recently bound through setConnection(Object). While that binding's borrow is current it is a live handle; a statement kept past its borrow answers with the disposed facade it came from. Once the statement is closed the handler refuses the call before it reaches the snapshot, so a stale binding is only visible through diagnostics such as toString().
        Returns:
        the connection proxy
      • setActualProxy

        public void setActualProxy​(java.lang.Object proxy)
        Sets the actual proxy object.
        Parameters:
        proxy - the actual proxy
      • getActualProxy

        public java.lang.Object getActualProxy()
        Returns the actual proxy object.
        Returns:
        the actual proxy
      • getConstructor

        public java.lang.reflect.Constructor<?> getConstructor()
        Returns the constructor used to create this proxy.
        Returns:
        the constructor
      • setConstructor

        public void setConstructor​(java.lang.reflect.Constructor<?> constructor)
        Sets the constructor for creating proxies.
        Parameters:
        constructor - the constructor
      • closeInvoked

        public void closeInvoked()
                          throws java.sql.SQLException
        Called when the close method is invoked on the statement.

        The driver's answer is passed on to the caller: Statement.close() declares SQLException for a database error while closing, and this proxy reports every other call as the delegate answered it, so a failed close must not come back as a success. The swallowed failure this replaces told the application that the statement was closed while the driver had left its resources on the session the pooled connection was about to hand to the next borrower, and nothing along the path recorded or retried it.

        The handle is retired whichever way the driver answered: the application has been told, and a handle whose close failed must not go on answering calls on a statement the driver may have half released. Where no application waits on the close - the cache dropping a statement, see StatementCache.closeStatement(org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CachedStatement) - the failure is answered with a log line instead, so that one statement that will not close cannot stop the rest of that purge.

        Throws:
        java.sql.SQLException - if the underlying statement could not be closed
      • invoke

        public java.lang.Object invoke​(java.lang.Object proxy,
                                       java.lang.reflect.Method method,
                                       java.lang.Object[] args)
                                throws java.lang.Throwable
        Specified by:
        invoke in interface java.lang.reflect.InvocationHandler
        Throws:
        java.lang.Throwable
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object