2.17.2: 2011-06-17

net.sf.basedb.core
Class QueryExecutor

java.lang.Object
  extended by net.sf.basedb.core.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:

Version:
2.6
Author:
Nicklas
Last modified
$Date: 2007-08-17 09:18:29 +0200 (fr, 17 aug 2007) $

Nested Class Summary
(package private) static interface QueryExecutor.Cancelable
          Interface to be implemented by classes that knows how to cancel a query.
(package private) static class QueryExecutor.CancelableSession
          Cancels the query running in a Hibernate session.
(package private) static class QueryExecutor.CancelableStatelessSession
          Cancels a query running in a stateless Hibernate session.
(package private) static class QueryExecutor.CancelableStatement
          Cancels a statement by calling Statement.cancel().
(package private) static class QueryExecutor.FutureItem<T>
           
(package private) static class QueryExecutor.FutureResultSet
          A callable implementation the calls PreparedStatement.executeQuery()
(package private) static class QueryExecutor.FutureScrollIterator<T>
          Calls Query.scroll(ScrollMode) and returns the result as a ScrollIterator.
(package private) static class QueryExecutor.FutureUpdate
          A callable implementation the calls PreparedStatement.executeUpdate()
 
Field Summary
private static ExecutorService service
           
 
Constructor Summary
QueryExecutor()
           
 
Method Summary
static ResultSet executeQuery(PreparedStatement ps, SessionControl sc)
          Execute a query by calling PreparedStatement.executeQuery().
static int executeUpdate(PreparedStatement ps, SessionControl sc)
          Execute a query by calling PreparedStatement.executeUpdate().
private static
<T> T
getFutureResult(Callable<T> callable, QueryExecutor.Cancelable cancelable, SessionControl sc)
          Run the callable and wait for the result.
(package private) static void init()
          This will create a new thread for each query.
static
<T> T
loadData(Class<T> clazz, org.hibernate.Query query, org.hibernate.StatelessSession session, SessionControl sc)
          Executes a Hibernate query on a stateless session.
static
<T> ScrollIterator<T>
loadIterator(Class<T> clazz, org.hibernate.Query query, org.hibernate.StatelessSession session, SessionControl sc)
          Executes a Hibernate query that returns an iterator on a stateless session.
(package private) static void unload()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

service

private static ExecutorService service
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()

executeQuery

public static ResultSet executeQuery(PreparedStatement ps,
                                     SessionControl sc)
                              throws InterruptedException
Execute a query by calling PreparedStatement.executeQuery().

Parameters:
ps - The prepared statement to execute
sc - A session control object that should be kept alive while the query is executing, or null
Returns:
The result from PreparedStatement.executeQuery()
Throws:
InterruptedException - If this thread was interrupted

executeUpdate

public static int executeUpdate(PreparedStatement ps,
                                SessionControl sc)
                         throws InterruptedException
Execute a query by calling PreparedStatement.executeUpdate().

Parameters:
ps - The prepared statement to execute
sc - A session control object that should be kept alive while the query is executing, or null
Returns:
The result from PreparedStatement.executeUpdate()
Throws:
InterruptedException - If this thread was interrupted

loadIterator

public static <T> ScrollIterator<T> loadIterator(Class<T> clazz,
                                                 org.hibernate.Query query,
                                                 org.hibernate.StatelessSession session,
                                                 SessionControl sc)
                                      throws InterruptedException
Executes a Hibernate query that returns an iterator on a stateless session.

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

loadData

public static <T> T loadData(Class<T> clazz,
                             org.hibernate.Query 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
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

2.17.2: 2011-06-17