cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Huss <johnth...@gmail.com>
Subject Re: Cayenne and threading
Date Thu, 18 Jun 2015 21:48:36 GMT
No, I don't think that's a good idea.  The same context shouldn't be shared
among threads.  If you're doing pure SQL operations (like SQLTemplate) and
not touching DataObjects you may be ok, but otherwise not.

But yes, Executors are great, especially with lambdas.

John

On Thu, Jun 18, 2015 at 4:31 PM Hugi Thordarson <hugi@karlmenn.is> wrote:

> 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
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message