DAO layer can return a JDBC ResultSet (Java)

My Java project has a DAO layer using the Java API JDBC; JDBC returns ResultSets to get the information from the database.

Taking into account the programming paradigm with layers, would it be correct to return to business layer the ResultSet object and to extract into it the information?

According to layers definition, the business layer has the responsibility to wrap the information inside domain objects (beans, and so on). But on the other hand, it haven't to see the DAO components.

I attach this code to show my paradigm:

    public static Hashtable<String, TreeSet<String>> getCodesByEditorial(Vector<Integer> familiesVector) throws Exception {
        DriverManager.registerDriver((Driver) Class.forName("ianywhere.ml.jdbcodbc.IDriver").newInstance());    
        Connection con  = DriverManager.getConnection("jdbc:odbc:DSN=DBLIB");
        String sqlQuery = 
            "SELECT re.codigo, li.editorial FROM li_li li " +
            "LEFT OUTER JOIN tl_recambio re ON li.codigo = re.codigo " +
            "WHERE li.editorial IS NOT NULL AND re.familia IN (" + buildFamilies(familiesVector) + ")" ;
        PreparedStatement ps = con.prepareStatement(sqlQuery);
        ResultSet res = ps.executeQuery();

        //THIS CODE SHOULD BE INTO DAO LAYER????
        Hashtable<String, TreeSet<String>> codesHashTree = new Hashtable<String, TreeSet<String>>();  
        while (res.next()) {
            String code = res.getString("CODIGO");
            String editorial = res.getString("EDITORIAL");
            if (editorial != null) {            
                TreeSet<String> bookTreeSet = codesHashTree.get(editorial);
                if (bookTreeSet == null) {
                    bookTreeSet = new TreeSet<String>();
                }
                bookTreeSet.add(code);
                codesHashTree.put(editorial, bookTreeSet);
            }
        }
        con.close();
        return codesHashTree;
    }

No, the business layer shouldn't deal with ResultSets . That layer's duty is to handle the business logic, unrelated to where the data is coming from (in this case the database).

One way to handle this is to return Data Transfer Objects (DTO) from the datasource layer, and then process them in the business layer. This way the data can come from a multitude of sources (database, flat file, web service, other integrations), and the business layer doesn't need to change.


I think it is a bad idea, because JDBC classes ( ResultSet is among them) are designed and intended to be used to retreive data from database. Now, when you're calling DAO method you're most likely expect to get a domain model object or some collection of them (I'm talking about findXXX -like methods).

The common best-practice is not to expose datasource communication to outer layers (most likely, to the service layer). Reasons: you may once replace database with key-value storage, text file or something else. If you stay with returning ResultSet , you'll have to change much signatures.


See first thing

if you have all the DB related logic inside DAO layer like connection object, resultset etc.

then it is obviously better to handle inside DAO layer and close all these opened connection and result set inside DAO layer itself.

if you return result set to Business layer, then you cant close resultset there in DAO layer itself.

it means you are deligating your DAO logic back to Business layer which is intended to do

business related stuff.

and what if some Exception occurs while closing result set , since you have returned to

business layer, it creates the problem in Business layer.

and you need to handle all DAO exceptions inside Business layer.

so avoid using resultset or any DB related things inside Business layer.

and use it purely for Business logic.

链接地址: http://www.djcxy.com/p/76578.html

上一篇: Sitemesh是否与Gaelyk合作? 有人有工作指南吗?

下一篇: DAO层可以返回一个JDBC ResultSet(Java)