views:

222

answers:

3

I've seen a million examples of DAOs, and for the most part they all implement your basic CRUD operations for single entities, with maybe a few methods that return lists (e.g. List getCustomers()).

However, I've never seen an example in which there is a method that updates, deletes, or creates multiple entities, like: void update(List).

Are methods that update multiple entities not typically part of a DAO, or are they just not often used in examples? I've got a requirement in which I've got to do some batch inserting, and calling myDAO.create() a hundred times isn't terribly efficient.

I just want to make sure I'm not missing something before I go ahead and do what seems obvious.

A: 

I think the examples don´t use it often. My DAOs have methods to access, create and update both single entities and collections of records.

fbinder
+2  A: 

I find that batch updates are usually done with tools provided by the database vendor.

I agree that the DAOs that I've seen usually don't have methods for create/update/delete overloaded to take List, but there's no reason why they can't.

The one thought that brings me up short is that DAOs don't own transactions when I write them. They can never know where they're part of a larger unit of work. That's what services do.

My advice would be to leave the DAOs alone and let a separate service layer own the batch operations. Services own the transaction logic. It's also a good place to include logic for "chunking" a large update into pieces. That lets you checkpoint the batch and keep the size of your rollback logs manageable.

duffymo
Hm, it seems like I would still have to have in my DAO a method that can take a list so that I could implement a proper batch update. I could still split the batch up in my service method and have it all run under the same transaction, yes?
Boden
No, I'm suggesting that DAO would still take a single object instance, and service would call DAO in a loop (or loops if you're "chunking" it). The service method would be a single unit of work that would own the transaction. The service might also batch the transaction to cut down on the network traffic.
duffymo
How would that work? If I were using Jdbc, for example, I wouldn't be able to use executeBatch in the service... I would basically be committing a whole bunch of regular updates.
Boden
You'd set auto commit to false, accumulate all the updates into a JDBC batch, execute the batch, the commit or rollback as needed.
duffymo
A: 

void update(List) should act the same as void update(Item), only the List one would update multiple items. If not, it should be unless there are very specific reasons why not to. The overhead of calling a method is not a concern if they do the same thing.

For example, calling update(Item) 1000 times vs calling update(List) 1 and it does the same as update(Item) only a 1000 times means there will be very little performance difference.

Malfist
If I was to implement an update(List) method, I wouldn't have it do an update(Item) for each item in the list. I would use whatever batch update methods my database layer provides. The overhead of 1000 separate updates can be much higher than a single batch update at that level.
Boden