Class QueryExecutor


  • public class QueryExecutor
    extends Object
    Utility class for executing SQL and Hibernate queries. If enabled, this class will use a thread pool to execute a query in separate threads. The benefits are:
    • It is possible to limit the number of concurrent queries.
    • It is possible to abort a running query by calling Thread.interrupt() on the thread that called one of the methods in this class.
    Version:
    2.6
    Author:
    Nicklas
    Last modified
    $Date: 2007-08-17 09:18:29 +0200 (fr, 17 aug 2007) $
    • Constructor Detail

      • QueryExecutor

        public QueryExecutor()
    • Method Detail

      • init

        static void init()
        This will create a new thread for each query. There is no limit on the number of threads.
      • unload

        static void unload()
      • loadIterator

        public static <T> ScrollIterator<T> loadIterator​(org.hibernate.query.Query<T> query,
                                                         org.hibernate.StatelessSession session,
                                                         SessionControl sc)
                                                  throws InterruptedException
        Executes a Hibernate query that returns an iterator on a stateless session.
        Parameters:
        query - The query to execute
        session - The stateless session, this must be the same session that was used to create the query
        sc - A session control object that should be kept alive while the query is executing, or null
        Throws:
        InterruptedException
        Since:
        2.7, 3.12
      • loadData

        public static <T> T loadData​(Class<T> clazz,
                                     org.hibernate.query.Query<T> query,
                                     org.hibernate.StatelessSession session,
                                     SessionControl sc)
                              throws InterruptedException
        Executes a Hibernate query on a stateless session. If the query returns more than one item, only the first one is returned.
        Parameters:
        clazz - The class of items returned by the iterator
        query - The query to execute
        session - The stateless session, this must be the same session that was used to create the query
        sc - A session control object that should be kept alive while the query is executing, or null
        Throws:
        InterruptedException
        Since:
        2.7
      • getFutureResult

        private static <T> T getFutureResult​(Callable<T> callable,
                                             QueryExecutor.Cancelable cancelable,
                                             SessionControl sc)
                                      throws InterruptedException,
                                             SQLException
        Run the callable and wait for the result. Call Thread.interrupt() on this thread to abort the query.
        Type Parameters:
        T - The type of result that is returned by the callable
        Parameters:
        callable - The procedure to call
        cancelable - An object that know how to cancel the running query if this thread is interrupted
        sc - A session control object that should be kept alive while the query is executing, or null
        Returns:
        The result from the call
        Throws:
        InterruptedException - If this thread was interrupted during the query
        SQLException