Interface DatabaseQueryProvider
-
public interface DatabaseQueryProvider
Defines the provider interface used by services to send queries using the Database Query resource adaptor.Synchronous queries
The procedure to send synchronous queries is:
- create a QueryInfo object that contains a copy of the values to set as parameters on the statement
- send the query using the sendQuery(QueryInfo) method on the DatabaseQueryProvider
- wait for the result by calling getResult() on the
DatabaseFutureResult
object returned from the sendQuery method - handle failures by catching the exceptions thrown by getResult()
private 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 Callprivate 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(); } }- Author:
- OpenCloud
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description DatabaseQueryActivity
createActivity()
Create aDatabaseQueryActivity
that can be used to send a single query with auto-commit on.DatabaseTransaction
createTransaction()
Create and return aDatabaseTransaction
object that can be provided tosendQuery(QueryInfo, DatabaseTransaction)
to associate multiple synchronous queries with a single transaction and then committed or rolled back.DatabaseQueryTransactionalActivity
createTransactionalActivity()
Create aDatabaseQueryActivity
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.
-
-
-
Method Detail
-
isDataSourceAvailable
boolean isDataSourceAvailable()
Check if any of the configured data sources are currently available.Note this does not guarantee that a subsequent
sendQuery(QueryInfo)
invocation will succeed.- Returns:
- true if there are currently one or more data sources available, false if there are none.
-
createActivity
DatabaseQueryActivity createActivity() throws StartActivityException
Create aDatabaseQueryActivity
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.- Returns:
- a new DatabaseQueryActivity
- Throws:
StartActivityException
- if the SLEE rejected creation of the activity
-
createTransactionalActivity
DatabaseQueryTransactionalActivity createTransactionalActivity() throws StartActivityException
Create aDatabaseQueryActivity
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 andConnection.setAutoCommit(boolean)
will be called on it with an argument of false. The connection will then be associated with this activity.- Returns:
- a new DatabaseQueryActivity
- Throws:
StartActivityException
- if the SLEE rejected creation of the activity- Since:
- 1.2
-
sendQuery
DatabaseFutureResult sendQuery(QueryInfo queryInfo) throws NoDataSourcesAvailableException
Send a request without creating an activity and make the result accessible via a Future-style object. A connection will be retrieved from the pool andConnection.setAutoCommit(boolean)
will be called on it with an argument of true.- Parameters:
queryInfo
- a callback object to set the query string and parameters for the SQL statement- Returns:
- an object that will return the query result when it is available
- Throws:
NoDataSourcesAvailableException
- if there are no configured data sources currently marked as available
-
sendQuery
DatabaseFutureResult sendQuery(QueryInfo queryInfo, DatabaseTransaction databaseTransaction) throws NoDataSourcesAvailableException, DatabaseQueryException
Send a request without creating an activity and make the result accessible via a Future-style object. If a connection is already associated with the givenDatabaseTransaction
it will be used, otherwise a connection will be retrieved from the pool andConnection.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.- Parameters:
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 transaction- Returns:
- an object that will return the query result when it is available
- Throws:
NoDataSourcesAvailableException
- 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 transaction- Since:
- 1.2
-
createTransaction
DatabaseTransaction createTransaction() throws IllegalStateException
Create and return aDatabaseTransaction
object that can be provided tosendQuery(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 (theConnection
). Instead the transaction handle can be marshaled usingDatabaseTransaction.marshal()
and unmarshaled usingunmarshalTransaction(byte[])
. This method is not applicable to transactions for asynchronous requests -- seecreateTransactionalActivity()
.)- Returns:
- a DatabaseTransaction instance
- Throws:
IllegalStateException
- if RA entity is stopping- Since:
- 1.2
-
unmarshalTransaction
DatabaseTransaction unmarshalTransaction(byte[] marshaledTransaction)
Return a transaction instance for the given handle. If the resource adaptor does not have a record of the given handle this method returns null.- Parameters:
marshaledTransaction
- a handle previously obtained by a call toDatabaseTransaction.marshal()
- Returns:
- a valid
DatabaseTransaction
instance or null if none exists - Since:
- 1.2
-
-