Class Database
- Since:
- 1.0.0
- Author:
- Mark Allen
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription@NonNull Optional<Transaction> Gets a reference to the current transaction, if any.@NonNull AmbiguousTimestampBindingStrategyHow should Pyranid bindInstantandOffsetDateTimeparameters when JDBC parameter metadata cannot identify whether the target isTIMESTAMPorTIMESTAMP WITH TIME ZONE?@NonNull DatabaseTypeGets the database type for this database.@NonNull MetricsCollector@NonNull ParameterRedactorGets the configured redactor used for non-secure parameters in diagnostics.@NonNull ZoneId<T> @NonNull Optional<T> participate(@NonNull Transaction transaction, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation in the context of a pre-existing transaction, optionally returning a value.voidparticipate(@NonNull Transaction transaction, @NonNull TransactionalOperation transactionalOperation) Performs an operation in the context of a pre-existing transaction.voidperformHealthCheck(@NonNull Duration timeout) Performs a portable connectivity check using JDBCConnection.isValid(int).@NonNull QueryCreates a fluent builder for executing SQL.voidreadDatabaseMetaData(@NonNull DatabaseMetaDataReader databaseMetaDataReader) Exposes a temporary handle to JDBCDatabaseMetaData, which provides comprehensive vendor-specific information about this database as a whole.<T> @NonNull Optional<T> transaction(@NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value.voidtransaction(@NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally.<T> @NonNull Optional<T> transaction(@NonNull TransactionOptions transactionOptions, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally with the given options, optionally returning a value.voidtransaction(@NonNull TransactionOptions transactionOptions, @NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally with the given options.<T> @NonNull TransactionRetryResult<T> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value, retrying according to the given retry policy.@NonNull TransactionRetryResult<Void> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally, retrying according to the given retry policy.<T> @NonNull TransactionRetryResult<T> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull TransactionOptions transactionOptions, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally with the given options and optionally returns a value, retrying according to the given retry policy.@NonNull TransactionRetryResult<Void> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull TransactionOptions transactionOptions, @NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally with the given options, retrying according to the given retry policy.<T> @NonNull Optional<T> useRawConnection(@NonNull RawConnectionOperation<T> rawConnectionOperation) Performs raw JDBC work with a Pyranid-managedConnection.static @NonNull Database.BuilderwithDataSource(@NonNull DataSource dataSource) Provides aDatabasebuilder for the givenDataSource.
-
Method Details
-
withDataSource
Provides aDatabasebuilder for the givenDataSource. -
currentTransaction
Gets a reference to the current transaction, if any.- Returns:
- the current transaction
-
transaction
Performs an operation transactionally.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation.Nested calls to
transaction(...)are independent transactions with independent JDBC connections; they do not automatically join an outer transaction. Useparticipate(Transaction, TransactionalOperation)to join an existing transaction explicitly. A transaction is scoped to theDataSourceinstance that created it; aDatabaseusing a differentDataSourcefails fast instead of joining it.- Parameters:
transactionalOperation- the operation to perform transactionally
-
transaction
public void transaction(@NonNull TransactionOptions transactionOptions, @NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally with the given options.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation.Nested calls to
transaction(...)are independent transactions with independent JDBC connections; they do not automatically join an outer transaction. Useparticipate(Transaction, TransactionalOperation)to join an existing transaction explicitly. A transaction is scoped to theDataSourceinstance that created it; aDatabaseusing a differentDataSourcefails fast instead of joining it.- Parameters:
transactionOptions- options to apply to the transactiontransactionalOperation- the operation to perform transactionally- Since:
- 4.2.0
-
transaction
public <T> @NonNull Optional<T> transaction(@NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation.Nested calls to
transaction(...)are independent transactions with independent JDBC connections; they do not automatically join an outer transaction. Useparticipate(Transaction, ReturningTransactionalOperation)to join an existing transaction explicitly. A transaction is scoped to theDataSourceinstance that created it; aDatabaseusing a differentDataSourcefails fast instead of joining it.- Type Parameters:
T- the type to be returned- Parameters:
transactionalOperation- the operation to perform transactionally- Returns:
- the result of the transactional operation
-
transaction
public <T> @NonNull Optional<T> transaction(@NonNull TransactionOptions transactionOptions, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally with the given options, optionally returning a value.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation.Nested calls to
transaction(...)are independent transactions with independent JDBC connections; they do not automatically join an outer transaction. Useparticipate(Transaction, ReturningTransactionalOperation)to join an existing transaction explicitly. A transaction is scoped to theDataSourceinstance that created it; aDatabaseusing a differentDataSourcefails fast instead of joining it.- Type Parameters:
T- the type to be returned- Parameters:
transactionOptions- options to apply to the transactiontransactionalOperation- the operation to perform transactionally- Returns:
- the result of the transactional operation
- Since:
- 4.2.0
-
transactionWithRetry
public @NonNull TransactionRetryResult<Void> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally, retrying according to the given retry policy.The entire transaction closure may run more than once. Keep non-idempotent external side effects outside the closure unless they are safe to repeat.
Unlike
transaction(TransactionalOperation)and related transaction methods, retrying methods returnTransactionRetryResultso callers can inspect failures that were recovered before success.This method fails fast if called inside an active transaction for this
Database. Retrying a nested unit cannot restart the outer transaction safely.- Parameters:
retryPolicy- retry policy to applytransactionalOperation- the operation to perform transactionally- Returns:
- retry result containing any failures retried before success
- Since:
- 4.4.0
-
transactionWithRetry
public @NonNull TransactionRetryResult<Void> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull TransactionOptions transactionOptions, @NonNull TransactionalOperation transactionalOperation) Performs an operation transactionally with the given options, retrying according to the given retry policy.The entire transaction closure may run more than once. Keep non-idempotent external side effects outside the closure unless they are safe to repeat.
Unlike
transaction(TransactionOptions, TransactionalOperation)and related transaction methods, retrying methods returnTransactionRetryResultso callers can inspect failures that were recovered before success.This method fails fast if called inside an active transaction for this
Database. Retrying a nested unit cannot restart the outer transaction safely.- Parameters:
retryPolicy- retry policy to applytransactionOptions- options to apply to each transaction attempttransactionalOperation- the operation to perform transactionally- Returns:
- retry result containing any failures retried before success
- Since:
- 4.4.0
-
transactionWithRetry
public <T> @NonNull TransactionRetryResult<T> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value, retrying according to the given retry policy.The entire transaction closure may run more than once. Keep non-idempotent external side effects outside the closure unless they are safe to repeat.
Unlike
transaction(ReturningTransactionalOperation)and related transaction methods, retrying methods returnTransactionRetryResultso callers can inspect failures that were recovered before success.This method fails fast if called inside an active transaction for this
Database. Retrying a nested unit cannot restart the outer transaction safely.- Type Parameters:
T- the type to be returned- Parameters:
retryPolicy- retry policy to applytransactionalOperation- the operation to perform transactionally- Returns:
- retry result containing the successful transaction value and any failures retried before success
- Since:
- 4.4.0
-
transactionWithRetry
public <T> @NonNull TransactionRetryResult<T> transactionWithRetry(@NonNull RetryPolicy retryPolicy, @NonNull TransactionOptions transactionOptions, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally with the given options and optionally returns a value, retrying according to the given retry policy.The entire transaction closure may run more than once. Keep non-idempotent external side effects outside the closure unless they are safe to repeat.
Unlike
transaction(TransactionOptions, ReturningTransactionalOperation)and related transaction methods, retrying methods returnTransactionRetryResultso callers can inspect failures that were recovered before success.This method fails fast if called inside an active transaction for this
Database. Retrying a nested unit cannot restart the outer transaction safely.- Type Parameters:
T- the type to be returned- Parameters:
retryPolicy- retry policy to applytransactionOptions- options to apply to each transaction attempttransactionalOperation- the operation to perform transactionally- Returns:
- retry result containing the successful transaction value and any failures retried before success
- Since:
- 4.4.0
-
participate
public void participate(@NonNull Transaction transaction, @NonNull TransactionalOperation transactionalOperation) Performs an operation in the context of a pre-existing transaction.No commit or rollback on the transaction will occur when
transactionalOperationcompletes.However, if an exception bubbles out of
transactionalOperation, the transaction will be marked as rollback-only.The transaction must have been created by this
Database, or by anotherDatabaseusing the sameDataSourceinstance.If this thread is interrupted while waiting for another participant to release the transaction connection, Pyranid restores the interrupt flag and throws
DatabaseException.- Parameters:
transaction- the transaction in which to participatetransactionalOperation- the operation that should participate in the transaction
-
participate
public <T> @NonNull Optional<T> participate(@NonNull Transaction transaction, @NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation in the context of a pre-existing transaction, optionally returning a value.No commit or rollback on the transaction will occur when
transactionalOperationcompletes.However, if an exception bubbles out of
transactionalOperation, the transaction will be marked as rollback-only.The transaction must have been created by this
Database, or by anotherDatabaseusing the sameDataSourceinstance.If this thread is interrupted while waiting for another participant to release the transaction connection, Pyranid restores the interrupt flag and throws
DatabaseException.- Type Parameters:
T- the type to be returned- Parameters:
transaction- the transaction in which to participatetransactionalOperation- the operation that should participate in the transaction- Returns:
- the result of the transactional operation
-
query
Creates a fluent builder for executing SQL.Named parameters use the
:paramNamesyntax and are bound viaQuery.bind(String, Object). Positional parameters via?are not supported. Pyranid ignores parameter-looking text inside SQL string literals, quoted identifiers, comments, PostgreSQL dollar-quoted strings, and SQL Server-style bracket-quoted identifiers. PostgreSQL JSONB/hstore?,?|, and?&operators are supported; when running againstDatabaseType.POSTGRESQL, Pyranid emits pgjdbc's escaped??form automatically. Unterminated quotes and comments fail fast.Example:
Optional<Employee> employee = database.query("SELECT * FROM employee WHERE id = :id") .bind("id", 42) .fetchObject(Employee.class);- Parameters:
sql- SQL containing:paramNameplaceholders- Returns:
- a fluent builder for binding parameters and executing
- Since:
- 4.0.0
-
performHealthCheck
Performs a portable connectivity check using JDBCConnection.isValid(int).This method borrows a fresh connection from this database's
DataSource, callsConnection.isValid(int), and closes the connection before returning. It does not participate in an active Pyranid transaction, if one exists.JDBC accepts timeout values in whole seconds. Positive sub-second durations are rounded up to one second;
Duration.ZEROpasses a timeout of0to the driver.- Parameters:
timeout- maximum time to wait for driver validation- Throws:
IllegalArgumentException- iftimeoutis negative or too large for JDBC's integer-second timeoutDatabaseException- if connection acquisition fails, validation throws, or the driver reports the connection is not valid- Since:
- 4.2.0
-
readDatabaseMetaData
Exposes a temporary handle to JDBCDatabaseMetaData, which provides comprehensive vendor-specific information about this database as a whole.This method acquires
DatabaseMetaDataon its own newly-borrowed connection, which it manages internally.It does not participate in the active transaction, if one exists.
The connection is closed as soon as
DatabaseMetaDataReader.read(DatabaseMetaData)completes.See
DatabaseMetaDataJavadoc for details. -
useRawConnection
public <T> @NonNull Optional<T> useRawConnection(@NonNull RawConnectionOperation<T> rawConnectionOperation) Performs raw JDBC work with a Pyranid-managedConnection.If called inside a Pyranid transaction, this operation uses the transaction's connection and participates in that transaction. Otherwise, Pyranid borrows a connection for the duration of the callback and closes it afterwards.
The
Connectionpassed torawConnectionOperationis a guarded handle. Normal JDBC operations are delegated to the underlying driver connection, but lifecycle, transaction-management, and connection-wide state methods such asConnection.close(),Connection.commit(),Connection.rollback(),Connection.setAutoCommit(boolean),Connection.setCatalog(String),Connection.setSchema(String), andConnection.setNetworkTimeout(java.util.concurrent.Executor, int)throwIllegalStateException. Use Pyranid transaction APIs instead. JDBC objects created from this handle are also guarded:Statement.getConnection()andDatabaseMetaData.getConnection()return the Pyranid-managed handle, andResultSet.getStatement()returns a guarded statement. Guarded statements, resultsets, and metadata refuse driver-specificunwrap(...)calls that could expose the driver's underlying connection.The connection handle is valid only for the duration of the callback. Do not close it, retain it, or use it after this method returns.
- Type Parameters:
T- the type to be returned- Parameters:
rawConnectionOperation- the raw JDBC operation to perform- Returns:
- the operation result
- Throws:
DatabaseException- if connection acquisition, callback execution, or cleanup fails- Since:
- 4.2.0
-
getDatabaseType
Gets the database type for this database.If
Database.Builder.databaseType(DatabaseType)was not configured and the database type has not already been detected, this method may acquire a connection and inspectDatabaseMetaData. Configure an explicit database type to avoid runtime detection.- Returns:
- the database type
- Throws:
DatabaseException- if automatic database type detection fails- Since:
- 3.0.0
-
getTimeZone
- Since:
- 3.0.0
-
getAmbiguousTimestampBindingStrategy
How should Pyranid bindInstantandOffsetDateTimeparameters when JDBC parameter metadata cannot identify whether the target isTIMESTAMPorTIMESTAMP WITH TIME ZONE?- Returns:
- behavior to use when timestamp target metadata is unavailable or non-identifying
- Since:
- 4.2.0
-
getParameterRedactor
Gets the configured redactor used for non-secure parameters in diagnostics.- Returns:
- the configured parameter redactor
- Since:
- 4.4.0
-
getMetricsCollector
-