Return-Path: X-Original-To: apmail-cayenne-user-archive@www.apache.org Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AFFFE187B0 for ; Sun, 14 Feb 2016 14:35:01 +0000 (UTC) Received: (qmail 17557 invoked by uid 500); 14 Feb 2016 14:35:01 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 17532 invoked by uid 500); 14 Feb 2016 14:35:01 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 17521 invoked by uid 99); 14 Feb 2016 14:35:01 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 14 Feb 2016 14:35:01 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id DBDD21A04C6 for ; Sun, 14 Feb 2016 14:35:00 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.999 X-Spam-Level: X-Spam-Status: No, score=0.999 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id EhkSuwbYfJt6 for ; Sun, 14 Feb 2016 14:34:59 +0000 (UTC) Received: from mail-03.1984.is (mail-03.1984.is [93.95.224.70]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with ESMTPS id 2E8C45FAD8 for ; Sun, 14 Feb 2016 14:34:58 +0000 (UTC) Received: from localhost by mail-03.1984.is with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.84) (envelope-from ) id 1aUxlJ-0003QP-Bu for user@cayenne.apache.org; Sun, 14 Feb 2016 14:34:57 +0000 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) Subject: Re: Reuse of code. Global ServerRuntime? From: Hugi Thordarson In-Reply-To: Date: Sun, 14 Feb 2016 14:34:51 +0000 Content-Transfer-Encoding: quoted-printable Message-Id: <136FDEE2-D9ED-417E-8587-9CBFEDC9EDB2@karlmenn.is> References: <1B23B717-FCC9-454F-AC82-0FA966439FDD@karlmenn.is> <9DEEB6DA-C0C9-458C-8B33-AE9E448146CB@karlmenn.is> To: user@cayenne.apache.org X-Mailer: Apple Mail (2.3112) Yes. I did, in fact, see that, the first time you wrote it. I=E2=80=99m = going to get myself some more coffee now. Thanks, - hugi > On 14. feb. 2016, at 14:31, John Huss wrote: >=20 > The second method I gave. Just bind the injector to the thread = earlier. > On Sun, Feb 14, 2016 at 8:20 AM Hugi Thordarson = wrote: >=20 >> How would you go about creating a new ObjectContext in framework = code; >> i.e. in code that doesn=E2=80=99t know anything about the environment = it=E2=80=99s running >> in? >>=20 >>=20 >>> On 14. feb. 2016, at 14:16, John Huss wrote: >>>=20 >>> It's your responsibility. >>>=20 >>> You can't get the runtime that way, but you probably don't need to >> (except >>> for your existing code!). The injector has everything you'd need = except >> the >>> runtime itself. >>> On Sun, Feb 14, 2016 at 8:02 AM Hugi Thordarson >> wrote: >>>=20 >>>> Thanks John, I had no idea getThreadObjectContext() existed. Does >> Cayenne >>>> somehow manage this variable or is it solely my responsibility? >>>>=20 >>>> It isn=E2=80=99t quite exactly what I=E2=80=99m looking for, = though=E2=80=94ideally, the method >>>> I=E2=80=99m looking for would be named getProcessServerRuntime(). >>>>=20 >>>> Would CayenneRuntime.getThreadInjector().getInstance( >> ServerRuntime.class >>>> ) return the currently operating ServerRuntime? >>>>=20 >>>> - hugi >>>>=20 >>>>=20 >>>>=20 >>>>> On 14. feb. 2016, at 13:50, John Huss wrote: >>>>>=20 >>>>> If you just need a context and not the runtime you can use: >>>>>=20 >>>>> BaseContext.getThreadObjectContext(); >>>>>=20 >>>>> as long as you bind one to the thread earlier. >>>>>=20 >>>>> Or you can use the thread injector to get other stuff like an >>>>> EntityResolver, or also a context, if you've bound it to a thread: >>>>> public static ObjectContext newObjectContext() { >>>>> ObjectContextFactory factory =3D >>>>>=20 >>>>=20 >> = CayenneRuntime.getThreadInjector().getInstance(ObjectContextFactory.class)= ; >>>>> return factory.createContext(); >>>>> } >>>>>=20 >>>>>=20 >>>>> On Sun, Feb 14, 2016 at 7:19 AM Hugi Thordarson >>>> wrote: >>>>>=20 >>>>>> Hi all. >>>>>>=20 >>>>>> In almost all of my Cayenne projects, I have a class that holds = my >>>>>> ServerRuntime and creates ObjectContexts from that ServerRuntime; = sort >>>> of a >>>>>> central station for my DB connectivity. Works fine, but it means = my >>>> code is >>>>>> somewhat tied to that runtime and calls to >>>>>> MyCentralCayenneClass.newContext() are littered all over the = place. >>>>>>=20 >>>>>> I=E2=80=99m currently writing a lot of unit tests and these use a = separate >>>>>> ServerRuntime with a different configuration that connects to a = test >>>>>> database. But since some of my code is directly creating contexts = from >>>> the >>>>>> other ServerRuntime (which connects to the production DB) I end = up >> with >>>>>> unintended consequences. Same thing for framework code that=E2=80=99= s supposed >>>> to >>>>>> be reusable=E2=80=94how should it obtain ObjectContexts without = knowing >> anything >>>>>> about the currently active ServerRuntime? >>>>>>=20 >>>>>> I see several solutions to the situtation but I wanted to start = out by >>>>>> checking if there=E2=80=99s a canonical method of solving the = problem? (I >>>> believe >>>>>> I=E2=80=99m actually asking if there=E2=80=99s a way to obtain = something like a global >>>>>> currently active ServerRuntime, that can be used by reusable code = and >>>>>> tests. But I didn=E2=80=99t want to frame the question in the = wrong way >>>> creating an >>>>>> XY Problem) >>>>>>=20 >>>>>> Cheers, >>>>>> - hugi >>>>=20 >>>>=20 >>=20 >>=20