jeudi 30 mai 2019

Single request with many ResultSets vs many requests with one ResultSet each?

I have a Java Web Application using Spring MVC. The objects usually have lists of other objetcts as their properties. We build the objects using a single query returning multiples ResultSets, and treat it in the DAO.

Example: My Deck has a list of cards.

public class Deck{
   private int id;
   private String name;
   private List<Card> cards;

   //Constructor and Getters & Setters
}

public class Card{
   private int id;
   private String name;
}

To build it we use a query thar returns a ResultSet for the deck properties, and other ResultSets to build the cards and put the list in the deck.

Deck deck = null;

List<Card> cards = new ArrayList<Card>();
this.ps = this.conecction.prepareStatement("SP_SELECT_DECK ?");
this.ps.setInt(1, 1);

boolean isResultSet = this.ps.execute();

int nResult = 0;

do{
    if(isResultSet){
        this.rs = this.ps.getResultSet();

        while(this.rs.next()){
             switch (nResult) {
         case 0:
                     //Build the deck using the ResultSet
             deck = new DeckBuilder().newInstance(rs);
             break;                                      case 1:
                     //Add a new card built using the resultset
             cards.add(new CardBuilder().newInstance(rs));
             break;
         }
    }

        this.rs.close();
   }else{
       break;
   }            
   nResult++;
} while (this.ps.getMoreResults());

deck.setCards(cards);

Is this the better practice? Or is it better to do a query with the deck informations, return to the java application and call another query to fill the list of cards?

The question is about Patterns, Good Practices, Performance and Network Traffic

Aucun commentaire:

Enregistrer un commentaire