public interface DatabaseQueryProvider
The procedure to send synchronous queries is:
DatabaseFutureResult
object returned from the sendQuery methodprivate void syncRequest(String shortCode) { DatabaseFutureResult futureResult = null; try { futureResult = dbProvider.sendQuery(new SelectLongCodeQueryInfo(shortCode)); } catch (NoDataSourcesAvailableException e) { // ... handle exception ... return; } try { ResultSet rs = futureResult.getResultSet(TIMEOUT); // ... handle result ... } catch (SQLException e) { // ... handle exception ... } catch (TimeoutException e) { // ... handle exception ... } catch (DatabaseQueryException e) { // ... handle exception ... } finally { futureResult.close(); } }Example Synchronous Stored Procedure Call
private void syncRequest(String shortCode, String prefix) {
DatabaseFutureResult futureResult = null;
try {
futureResult = dbProvider.sendQuery(new ExtendLongCodeProcedure(shortCode, prefix));
// for ExtendLongCodeProcedure example see QueryInfo
}
catch (NoDataSourcesAvailableException e) {
// ... handle exception ...
return;
}
try {
CallableStatement cstmt = (CallableStatement) futureResult.getStatement(TIMEOUT);
String result = cstmt.getString(1);
// ... handle result ...
}
catch (SQLException e) {
// ... handle exception ...
}
catch (TimeoutException e) {
// ... handle exception ...
}
catch (DatabaseQueryException e) {
// ... handle exception ...
}
finally {
futureResult.close();
}
}
Modifier and Type | Method and Description |
---|---|
DatabaseQueryActivity |
createActivity()
Create a
DatabaseQueryActivity that can be used to send a single
query with auto-commit on. |
DatabaseTransaction |
createTransaction()
Create and return a
DatabaseTransaction object that can be provided to sendQuery(QueryInfo, DatabaseTransaction)
to associate multiple synchronous queries with a single transaction and then committed or rolled back. |
DatabaseQueryTransactionalActivity |
createTransactionalActivity()
Create a
DatabaseQueryActivity that can be used to send one or more
queries as part of a transaction. |
boolean |
isDataSourceAvailable()
Check if any of the configured data sources are currently available.
|
DatabaseFutureResult |
sendQuery(QueryInfo queryInfo)
Send a request without creating an activity and make the result accessible via a Future-style object.
|
DatabaseFutureResult |
sendQuery(QueryInfo queryInfo,
DatabaseTransaction databaseTransaction)
Send a request without creating an activity and make the result accessible via a Future-style object.
|
DatabaseTransaction |
unmarshalTransaction(byte[] marshaledTransaction)
Return a transaction instance for the given handle.
|
boolean isDataSourceAvailable()
Note this does not guarantee that a subsequent sendQuery(QueryInfo)
invocation will succeed.
DatabaseQueryActivity createActivity() throws StartActivityException
DatabaseQueryActivity
that can be used to send a single
query with auto-commit on. The activity will end when the result event (or timeout event) is fired.StartActivityException
- if the SLEE rejected creation of the activityDatabaseQueryTransactionalActivity createTransactionalActivity() throws StartActivityException
DatabaseQueryActivity
that can be used to send one or more
queries as part of a transaction. When the first query is submitted on this activity, a connection will be retrieved
from the pool and Connection.setAutoCommit(boolean)
will be called on it with an argument of false.
The connection will then be associated with this activity.StartActivityException
- if the SLEE rejected creation of the activityDatabaseFutureResult sendQuery(QueryInfo queryInfo) throws NoDataSourcesAvailableException
Connection.setAutoCommit(boolean)
will be called
on it with an argument of true.queryInfo
- a callback object to set the query string and parameters for the SQL statementNoDataSourcesAvailableException
- if there are no configured data sources currently marked as availableDatabaseFutureResult sendQuery(QueryInfo queryInfo, DatabaseTransaction databaseTransaction) throws NoDataSourcesAvailableException, DatabaseQueryException
DatabaseTransaction
it will be used, otherwise
a connection will be retrieved from the pool and Connection.setAutoCommit(boolean)
will be
called on it with an argument of false. The same connection will then be used for future calls to
this method that pass in the same transaction object.queryInfo
- a callback object to set the query string and parameters for the SQL statementdatabaseTransaction
- the request will be sent on the connection associated with this transactionNoDataSourcesAvailableException
- if there are no configured data sources currently marked as availableDatabaseQueryException
- if there is an error sending the request on the connection associated with the given transactionDatabaseTransaction createTransaction() throws IllegalStateException
DatabaseTransaction
object that can be provided to sendQuery(QueryInfo, DatabaseTransaction)
to associate multiple synchronous queries with a single transaction and then committed or rolled back.
A connection is not allocated for the transaction until the first query is submitted.
DatabaseTransaction
instances may not be stored directly in a CMP field since they contain a
non-serializable reference (the Connection
). Instead the transaction handle can be marshaled
using DatabaseTransaction.marshal()
and unmarshaled using
unmarshalTransaction(byte[])
.
This method is not applicable to transactions for asynchronous requests -- see createTransactionalActivity()
.)IllegalStateException
- if RA entity is stoppingDatabaseTransaction unmarshalTransaction(byte[] marshaledTransaction)
marshaledTransaction
- a handle previously obtained by a call to DatabaseTransaction.marshal()
DatabaseTransaction
instance or null if none exists