Package net.sf.basedb.core
Class QueryExecutor
java.lang.Object
net.sf.basedb.core.QueryExecutor
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) $
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static interface
Interface to be implemented by classes that knows how to cancel a query.(package private) static class
Cancels the query running in a Hibernate session.(package private) static class
Cancels a query running in a stateless Hibernate session.(package private) static class
Cancels a statement by callingStatement.cancel()
.(package private) static class
(package private) static class
A callable implementation the callsPreparedStatement.executeQuery()
(package private) static class
CallsQuery.scroll(ScrollMode)
and returns the result as aScrollIterator
.(package private) static class
A callable implementation the callsPreparedStatement.executeUpdate()
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ResultSet
Execute a query by callingPreparedStatement.executeQuery()
.static int
Execute a query by callingPreparedStatement.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, Query<T> query, StatelessSession session, SessionControl sc) Executes a Hibernate query on a stateless session.static <T> ScrollIterator<T>
loadIterator
(Query<T> query, StatelessSession session, SessionControl sc) Executes a Hibernate query that returns an iterator on a stateless session.(package private) static void
unload()
-
Field Details
-
service
-
-
Constructor Details
-
QueryExecutor
public QueryExecutor()
-
-
Method Details
-
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, SQLException Execute a query by callingPreparedStatement.executeQuery()
.- Parameters:
ps
- The prepared statement to executesc
- 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 interruptedSQLException
- If there was an error executing the SQL statement
-
executeUpdate
public static int executeUpdate(PreparedStatement ps, SessionControl sc) throws InterruptedException, SQLException Execute a query by callingPreparedStatement.executeUpdate()
.- Parameters:
ps
- The prepared statement to executesc
- 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 interruptedSQLException
-
loadIterator
public static <T> ScrollIterator<T> loadIterator(Query<T> query, StatelessSession session, SessionControl sc) throws InterruptedException Executes a Hibernate query that returns an iterator on a stateless session.- Parameters:
query
- The query to executesession
- The stateless session, this must be the same session that was used to create the querysc
- 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, Query<T> query, 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 iteratorquery
- The query to executesession
- The stateless session, this must be the same session that was used to create the querysc
- 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 callcancelable
- An object that know how to cancel the running query if this thread is interruptedsc
- 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 querySQLException
-