Skip to content

Generic template methods for read/write operations

Generic template methods provide a way to abstract repetitive database interaction logic, specifically managing connections, transactions, and resource cleanup. By passing the specific data access logic as a parameter (using functional interfaces), these templates ensure that infrastructure code—such as handling Connection objects—is written only once^[600-developer__java__java8__java8-lambda01.md].

Implementation Pattern

The core implementation typically relies on a custom Functional Interface, such as BiFunctionUnchecked, which accepts parameters (like a Connection and a parameter Map) and returns a result while allowing checked exceptions (like SQLException)^[600-developer__java__java8__java8-lambda01.md]. The generic methods are usually defined as static utility methods that accept this Functional Interface alongside the parameters^[600-developer__java__java8__java8-lambda01.md].

These templates are categorized into two main types of operations: Read and Write^[600-developer__java__java8__java8-lambda01.md].

Read Operations

Read operations are designed to fetch data from the database. The generic method handles the acquisition of a read connection and ensures the connection is closed after execution^[600-developer__java__java8__java8-lambda01.md].

Key characteristics include: * Acquiring a connection via DBPool.getInstance().getReadConnection()^[600-developer__java__java8__java8-lambda01.md]. * Invoking the provided DAO function using the acquired connection^[600-developer__java__java8__java8-lambda01.md]. * Ensuring resources are released in a finally block^[600-developer__java__java8__java8-lambda01.md]. * Optionally wrapping the result in an Optional (e.g., readOptional) to handle nullability^[600-developer__java__java8__java8-lambda01.md].

Write Operations

Write operations are designed to modify data and require transaction management to ensure data integrity^[600-developer__java__java8__java8-lambda01.md].

Key characteristics include: * Acquiring a connection configured for writing^[600-developer__java__java8__java8-lambda01.md]. * Disabling auto-commit to manually control the transaction (conn.setAutoCommit(false))^[600-developer__java__java8__java8-lambda01.md]. * Explicitly committing the transaction after the function executes successfully^[600-developer__java__java8__java8-lambda01.md]. * Rollback logic: If an exception occurs, the transaction is rolled back (conn.rollback()) before re-throwing the exception^[600-developer__java__java8__java8-lambda01.md]. * Ensuring the connection is closed in the finally block^[600-developer__java__java8__java8-lambda01.md].

Usage Example

When invoking these templates, the specific DAO logic (e.g., BeanDao::currentSql) is passed as a method reference^[600-developer__java__java8__java8-lambda01.md].

// Calling the generic read template with a specific DAO method
return DefaultBO.read(map, BeanDao::currentSql);

This replaces verbose try-catch-finally blocks in the service layer with a single line, delegating the resource management to the template^[600-developer__java__java8__java8-lambda01.md].

  • [[Functional interfaces]]
  • [[Method references]]
  • [[Connection pooling]]
  • [[Template Method pattern]]

Sources

^[600-developer__java__java8__java8-lambda01.md]