Function-based DAO template extraction¶
Function-based DAO template extraction is a refactoring technique used to decouple database resource management (connection handling, transactions) from business logic execution. By utilizing functional interfaces, this pattern abstracts the repetitive "boilerplate" code required for database operations into reusable templates.^[600-developer__java__java8__java8-lambda01.md]
Core Components¶
The implementation relies on a custom Functional Interface and a template class that handles the lifecycle of database connections.
Functional Interface¶
A custom Functional Interface, BiFunctionUnchecked, is defined to accept a Connection and a parameter Map while returning a result. This interface is designed to handle checked exceptions (specifically SQLException) which are common in DAO operations but not natively supported by standard Java functional interfaces like Function.^[600-developer__java__java8__java8-lambda01.md]
@FunctionalInterface
public interface BiFunctionUnchecked<K, T, V> {
V applyDao(K k, T t) throws SQLException;
}
Template Methods¶
Template methods, typically housed in a class such as DefaultBO, manage the acquisition and release of database connections within try-finally blocks. These methods accept the Functional Interface as a parameter, allowing the specific data access logic to be injected.^[600-developer__java__java8__java8-lambda01.md]
The templates generally cover two types of operations:
- Read Operations: Methods like
readorreadOptionalretrieve a read-only connection, execute the injected DAO function, and ensure the connection is closed.^[600-developer__java__java8__java8-lambda01.md] - Write Operations: Methods like
writeorwriteOptionalretrieve a connection, disable auto-commit, execute the function, commit the transaction, and handle rollback in case of exceptions.^[600-developer__java__java8__java8-lambda01.md]
Implementation Details¶
In this pattern, DAO methods are refactored into static methods that accept the Connection as an argument, removing the need for the DAO to manage the connection itself^[600-developer__java__java8__java8-lambda01.md].
The business logic layer invokes the template method using a method reference (e.g., ClassName::staticMethodName). This syntax conforms to the "Class-Static Method" format, passing the specific DAO logic directly into the template workflow^[600-developer__java__java8__java8-lambda01.md].
Benefits¶
- Separation of Concerns: The logic for opening/closing connections and managing transactions is separated from the SQL execution logic^[600-developer__java__java8__java8-lambda01.md].
- Reduction of Boilerplate: Repetitive
try-catch-finallyblocks for connection handling are centralized in a single template class^[600-developer__java__java8__java8-lambda01.md]. - Type Safety: The use of generics in the template methods ensures type safety across different data access operations^[600-developer__java__java8__java8-lambda01.md].
Related Concepts¶
- Functional Interface
- [[Template Method Pattern]]
- [[DAO Pattern]]
Sources¶
^[600-developer__java__java8__java8-lambda01.md]