Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:13:01+00:00 2026-05-16T09:13:01+00:00

The following are methods I currently have in an Abstract DAO class. If there

  • 0

The following are methods I currently have in an Abstract DAO class. If there are concurrent calls, are they safe as they are or should synchronization be used? I know synchronization should be used if there is a reference to an attribute outside the scope of a method but it’s unclear to me how should things be handled with an outside ressource.

public Connection getConnection() {
    // Call to singleton handling JDBC stuff
    return Database.getInstance().getCon();
}

public boolean isConnectionAvailable(){     
    if( getConnection() != null ){
        return true;
    }

    return false;
}

public PreparedStatement getPreparedStatement( String sqlStatement ){
    Connection connection = getConnection();
    PreparedStatement pS = null;

    if( connection != null ){
        try {
            pS = connection.prepareStatement( sqlStatement );
        } catch (SQLException e) {
            return null;
        }
    }

    return pS;
}

Edit: I might reformulate this question to include information on writing DAOs as it’s what’s important here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:13:02+00:00Added an answer on May 16, 2026 at 9:13 am

    I don’t agree with this implementation at all.

    First, DAOs should be given their connection information by the services that own units of work and transactions.

    Second, I don’t see an interface.

    Third, I don’t see model or domain objects.

    Fourth, prepared statements should only be part of the internal implementation. If they’re leaking out of your DAO, you’re doing it wrong.

    Fifth, passing a prepared statement out of the object makes responsibility for closing it and cleaning up far less clear. Your DAO will die a resource leak death in no time.

    Here’s an interface for a generic DAO. You’ll notice that it’s all CRUD operations, with no mention of connnections or any interfaces from java.sql package:

    package persistence;
    
    import java.io.Serializable;
    import java.util.List;
    
    public interface GenericDao<T, K extends Serializable>
    {
        T find(K id);
        List<T> find();
        List<T> find(T example);
        List<T> find(String queryName, String [] paramNames, Object [] bindValues);
    
        K save(T instance);
        void update(T instance);
        void delete(T instance);
    }
    

    You can go a long way with this. It’s a better abstraction. The T is your business object type, and K is the primary key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a question about class design. Currently I have the following structure: abstract Base Repository
I have the following methods in my Authentication class that is called by my
I have the following methods: public <T> T fromJson( Reader jsonData, Class<T> clazz )
I have the following methods in my simple Custom Parser Class, when I execute
There's the following class: public abstract class AbstractWriter<T extends BE> { protected final T
Within my code a have the following abstract superclass public abstract class AbstractClass<Type extends
Let's say I have the following class hierarchy (base interface included): IAction -> (abstract)
I have the following setup: abstract class AParentLy{ private $a; private $b; function foo(){
I have the following class hierarchy: public abstract class BaseData { //some properties }
For simplicity's sake, lets say I have the following Abstract Base Controller Class: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.