Scenario
I have an application where some operations require the authentication of an admin.
Example Steps:
- I need to validate the admin's username & password
- insert a row into a MySQL table (if credentials are valid).
One Trip Approach
If I use a single Connection
to the database, with multiple PrepearedStatements
, I'm able to achieve my goals.
However, the abstraction of the application is damaged. Using this approach, in my DAO (database access object) method, I have to accept the arguments for the row insertion + the arguments for the admin validation.
It does save another connection to the database, but on the other side, the DAO that handles insertion shouldn't know or care about user validation.
An Example signature of the method would be this:
insertData(String data,String metadata,String username,String password)
Two Trip Approach
Using this approach, I am using 2 connections to the database. First one validates the users username & password, and if they are valid, opens another connection, and inserts the row into the database.
This approach uses an extra connection, but the DAO method now accepts only arguments that are relevant for the actual data insertion.
An Example signature of the method would be this:
insertData(String data,String metadata)
Question
So my question is - which way is better? Should I sacrifice abstraction for database trips, or do several database trips for the sake of abstraction?
AuthenticatedConnection
from that layer and make only the one business-relevant query.AuthenticatedConnection
?