Hi all.
Coming from EOF I’ve been mentally trained to believe anything threaded will
bite you in the ass in a bad way. Using Cayenne, I’m now writing threaded code
and loving it, but I just wanted to double check with the community; Is it
really OK for me to do stuff like I’m doing in the following code? It’s a
somewhat extreme example since I'd never actually commit for each iteration,
just looking for some good practices, advice or an “ARE YOU CRAZY?” comment :).
public void main( String[] argv ) {
ObjectContext oc = // get my ObjectContext
ExecutorService executorService = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors() );
List<Receipt> receipts = oc.select( new SelectQuery<>( Receipt.class )
);
for( Receipt receipt : receipts ) {
executorService.execute( () -> {
receipt.setCreationDate( new Date() );
oc.commitChanges();
} );
}
executorService.shutdown();
}
Cheers,
- hugi
// Hugi Thordarson
// http://www.loftfar.is/ <http://www.loftfar.is/>
// s. 895-6688